55b899d7a48d7eb27ba35fee3aaf25d3da4c3386
3 open System.Collections.Generic
14 d_fg
: Image<Gray, float32
> } // Euclidean distances of the foreground to mean_fg.
16 let kmeans (img
: Image<Gray, float32
>) : Result =
21 let min = ref [| 0.0
|]
22 let minLocation = ref <| [| Point() |]
23 let max = ref [| 0.0
|]
24 let maxLocation = ref <| [| Point() |]
25 img
.MinMax(min, max, minLocation, maxLocation)
27 let minf = float32
(!min).[0]
28 let maxf = float32
(!max).[0]
30 let mutable mean_bg = maxf - (maxf - minf) / 4.f
31 let mutable mean_fg = minf + (maxf - minf) / 4.f
32 use mutable d_bg : Image<Gray, float32
> = null
33 let mutable d_fg : Image<Gray, float32
> = null
34 let fg = new Image<Gray, byte
>(img
.Size)
36 let imgData = img
.Data
39 for i
in 1 .. nbIteration do
45 // EmGu doesn't import the in-place version of 'AbsDiff' so we have to create two images for each iteration.
46 d_bg <- img
.AbsDiff(Gray(float mean_bg))
47 d_fg <- img
.AbsDiff(Gray(float mean_fg))
49 CvInvoke.Compare(d_fg, d_bg, fg, CvEnum.CmpType.LessThan)
51 let mutable bg_total = 0.f
54 let mutable fg_total = 0.f
57 for i
in 0 .. h - 1 do
58 for j
in 0 .. w - 1 do
59 if fgData.[i
, j
, 0] > 0uy
61 fg_total <- fg_total + imgData.[i
, j
, 0]
64 bg_total <- bg_total + imgData.[i
, j
, 0]
67 mean_bg <- bg_total / float32
bg_nb
68 mean_fg <- fg_total / float32
fg_nb
70 { fg = fg; mean_bg = mean_bg; mean_fg = mean_fg; d_fg = d_fg }