Version 1.0.10
[master-thesis.git] / Parasitemia / ParasitemiaCore / KMedians.fs
1 module ParasitemiaCore.KMedians
2
3 open System.Collections.Generic
4 open System.Drawing
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 type Result =
10 {
11 fg : Image<Gray, byte>
12 median_bg : float
13 median_fg : float
14 d_fg : Image<Gray, float32> // Euclidean distances of the foreground to median_fg.
15 }
16
17 let kmedians (img : Image<Gray, float32>) : Result =
18 let nbIteration = 4
19 let w = img.Width
20 let h = img.Height
21
22 let min = ref [| 0.0 |]
23 let minLocation = ref <| [| Point() |]
24 let max = ref [| 0.0 |]
25 let maxLocation = ref <| [| Point() |]
26 img.MinMax(min, max, minLocation, maxLocation)
27
28 let mutable median_bg = (!max).[0] - ((!max).[0] - (!min).[0]) / 4.0
29 let mutable median_fg = (!min).[0] + ((!max).[0] - (!min).[0]) / 4.0
30 use mutable d_bg = new Image<Gray, float32>(img.Size)
31 let mutable d_fg = new Image<Gray, float32>(img.Size)
32 let mutable fg = new Image<Gray, byte>(img.Size)
33
34 for i = 1 to nbIteration do
35 d_bg <- img.AbsDiff(Gray(median_bg))
36 d_fg <- img.AbsDiff(Gray(median_fg))
37
38 CvInvoke.Compare(d_fg, d_bg, fg, CvEnum.CmpType.LessThan)
39
40 let bg_values = List<float>()
41 let fg_values = List<float>()
42
43 for i = 0 to h - 1 do
44 for j = 0 to w - 1 do
45 if fg.Data.[i, j, 0] > 0uy then
46 fg_values.Add(float img.Data.[i, j, 0])
47 else
48 bg_values.Add(float img.Data.[i, j, 0])
49
50 median_bg <- MathNet.Numerics.Statistics.Statistics.Median(bg_values)
51 median_fg <- MathNet.Numerics.Statistics.Statistics.Median(fg_values)
52
53 { fg = fg; median_bg = median_bg; median_fg = median_fg; d_fg = d_fg }
54
55
56
57