module KMedians open Emgu.CV open Emgu.CV.Structure open System.Drawing let kmedians (mat: Matrix) (fgFactor: float) : Matrix = let nbIteration = 3 let min = ref 0.0 let minLocation = ref <| Point() let max = ref 0.0 let maxLocation = ref <| Point() mat.MinMax(min, max, minLocation, maxLocation) let mutable bgValue = !max - (!max - !min) / 4.0 let mutable fgValue = !min + (!max - !min) / 4.0 let mutable d_bg = new Matrix(mat.Size) let mutable d_fg = new Matrix(mat.Size) for i in 1..3 do CvInvoke.Pow(mat - bgValue, 2.0, d_bg) CvInvoke.Pow(mat - fgValue, 2.0, d_fg) let fg = (d_fg * fgFactor).Cmp(d_bg, CvEnum.CmpType.LessThan) printfn "test" (*d_bg = (imgFloat - color_bg) .^ 2.0; d_fg = (imgFloat - color_fg) .^ 2.0; fg = d_fg * Background_weight < d_bg; imgFilteredFg = imgFloat; imgFilteredFg(~fg) = nan; color_fg = median(reshape(imgFilteredFg, [], 1), 'omitnan'); imgFilteredBg = imgFloat; imgFilteredBg(fg) = nan; color_bg = median(reshape(imgFilteredBg, [], 1), 'omitnan'); *) new Matrix(mat.Size)