Add functions to apply an area opening and to compute granulometry.
[master-thesis.git] / Parasitemia / Parasitemia / Granulometry.fs
diff --git a/Parasitemia/Parasitemia/Granulometry.fs b/Parasitemia/Parasitemia/Granulometry.fs
new file mode 100644 (file)
index 0000000..a9a102a
--- /dev/null
@@ -0,0 +1,38 @@
+module Granulometry
+
+open System
+open System.Drawing
+
+open Emgu.CV
+open Emgu.CV.Structure
+
+open Utils
+
+// 'range': a minimum and maximum radius.
+// 'scale': <= 1.0, to speed up the process.
+let findRadius (img: Image<Gray, byte>)  (range: int * int) (scale: float) : int =
+    use scaledImg = if scale = 1.0 then img else img.Resize(scale, CvEnum.Inter.Area)
+
+    let r1, r2 = range
+    let r1', r2' = roundInt (float r1 * scale), roundInt (float r2 * scale)
+
+    let patternSpectrum = Array.zeroCreate (r2' - r1')
+    let intensityImg = scaledImg.GetSum().Intensity
+
+    let mutable previous_n = Double.NaN
+    for r in r1' .. r2' do
+        let se = CvInvoke.GetStructuringElement(CvEnum.ElementShape.Ellipse, Size(2 * r, 2 * r), Point(-1, -1))
+        use closed = scaledImg.MorphologyEx(CvEnum.MorphOp.Close, se, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar(0.0))
+
+        let n = 1.0 - closed.GetSum().Intensity / intensityImg
+
+        if not (Double.IsNaN previous_n)
+        then
+            patternSpectrum.[r - r1' - 1] <- abs (n - previous_n)
+        previous_n <- n
+
+    let max = patternSpectrum |> Array.indexed |> Array.fold (fun (iMax, sMax) (i, s) -> if s > sMax then (i, s) else (iMax, sMax)) (0, Double.MinValue)
+
+    0
+
+