Use float32 to reduce memory footprint.
[master-thesis.git] / Parasitemia / Parasitemia / KMedians.fs
index 5ba15db..f7f2e54 100644 (file)
@@ -1,45 +1,53 @@
 module KMedians
 
+open System.Collections.Generic
+open System.Drawing
+
 open Emgu.CV
 open Emgu.CV.Structure
 
-open System.Drawing
+type Result = {
+    fg: Image<Gray, byte>
+    median_bg: float
+    median_fg: float
+    d_fg: Image<Gray, float32> } // Euclidean distances of the foreground to median_fg.
 
-let kmedians (mat: Matrix<float32>) (fgFactor: float) : Matrix<byte> =
+let kmedians (img: Image<Gray, float32>) : Result =
     let nbIteration = 3
+    let w = img.Width
+    let h = img.Height
+
+    let min = ref [| 0.0 |]
+    let minLocation = ref <| [| Point() |]
+    let max = ref [| 0.0 |]
+    let maxLocation = ref <| [| Point() |]
+    img.MinMax(min, max, minLocation, maxLocation)
+
+    let mutable median_bg = (!max).[0] - ((!max).[0] - (!min).[0]) / 4.0
+    let mutable median_fg = (!min).[0] + ((!max).[0] - (!min).[0]) / 4.0
+    use mutable d_bg = new Image<Gray, float32>(img.Size)
+    let mutable d_fg = new Image<Gray, float32>(img.Size)
+    let mutable fg = new Image<Gray, byte>(img.Size)
+
+    for i in 1 .. nbIteration do
+        d_bg <- img.AbsDiff(Gray(median_bg))
+        d_fg <- img.AbsDiff(Gray(median_fg))
+
+        CvInvoke.Compare(d_fg, d_bg, fg, CvEnum.CmpType.LessThan)
+
+        let bg_values = List<float>()
+        let fg_values = List<float>()
+
+        for i in 0 .. h - 1 do
+            for j in 0 .. w - 1 do
+                if fg.Data.[i, j, 0] > 0uy
+                then fg_values.Add(float img.Data.[i, j, 0])
+                else bg_values.Add(float img.Data.[i, j, 0])
+
+        median_bg <- MathNet.Numerics.Statistics.Statistics.Median(bg_values)
+        median_fg <- MathNet.Numerics.Statistics.Statistics.Median(fg_values)
 
-    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<float32>(mat.Size)
-    let mutable d_fg = new Matrix<float32>(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<byte>(mat.Size)
+    { fg = fg; median_bg = median_bg; median_fg = median_fg; d_fg = d_fg }