1
module ParasitemiaCore.KMedians
3 open System.Collections.Generic
13 d_fg
: Image<Gray, float32
> } // Euclidean distances of the foreground to median_fg.
15 let kmedians (img
: Image<Gray, float32
>) : Result =
20 let min = ref [| 0.0
|]
21 let minLocation = ref <| [| Point() |]
22 let max = ref [| 0.0
|]
23 let maxLocation = ref <| [| Point() |]
24 img
.MinMax(min, max, minLocation, maxLocation)
26 let mutable median_bg = (!max).[0] - ((!max).[0] - (!min).[0]) / 4.0
27 let mutable median_fg = (!min).[0] + ((!max).[0] - (!min).[0]) / 4.0
28 use mutable d_bg = new Image<Gray, float32
>(img
.Size)
29 let mutable d_fg = new Image<Gray, float32
>(img
.Size)
30 let mutable fg = new Image<Gray, byte
>(img
.Size)
32 for i
in 1 .. nbIteration do
33 d_bg <- img
.AbsDiff(Gray(median_bg))
34 d_fg <- img
.AbsDiff(Gray(median_fg))
36 CvInvoke.Compare(d_fg, d_bg, fg, CvEnum.CmpType.LessThan)
38 let bg_values = List<float>()
39 let fg_values = List<float>()
41 for i
in 0 .. h - 1 do
42 for j
in 0 .. w - 1 do
43 if fg.Data.[i
, j
, 0] > 0uy
44 then fg_values.Add(float img.Data.[i
, j
, 0])
45 else bg_values.Add(float img.Data.[i
, j
, 0])
47 median_bg <- MathNet.Numerics.Statistics.Statistics.Median(bg_values)
48 median_fg <- MathNet.Numerics.Statistics.Statistics.Median(fg_values)
50 { fg = fg; median_bg = median_bg; median_fg = median_fg; d_fg = d_fg }