7c18120b65fe76bccab00288a983a311fc4651ef
3 open System.Collections.Generic
13 d_fg
: Image<Gray, float32
> } // Euclidean distances of the foreground to mean_fg.
15 let kmeans (img
: Image<Gray, float32
>) (fgFactor
: float) : 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 mean_bg = (!max).[0] - ((!max).[0] - (!min).[0]) / 4.0
27 let mutable mean_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(mean_bg))
34 d_fg <- img
.AbsDiff(Gray(mean_fg))
36 CvInvoke.Compare(d_fg, d_bg, fg, CvEnum.CmpType.LessThan)
38 let mutable bg_total = 0.0
41 let mutable fg_total = 0.0
44 for i
in 0 .. h - 1 do
45 for j
in 0 .. w - 1 do
46 if fg.Data.[i
, j
, 0] > 0uy
48 fg_total <- fg_total + float img.Data.[i
, j
, 0]
51 bg_total <- bg_total + float img.Data.[i
, j
, 0]
54 mean_bg <- bg_total / float bg_nb
55 mean_fg <- fg_total / float fg_nb
57 { fg = fg; mean_bg = mean_bg; mean_fg = mean_fg; d_fg = d_fg }