* Try another approach to remove false ellipses without success (commented).
[master-thesis.git] / Parasitemia / Parasitemia / ParasitesMarker.fs
index f1a94e1..720488d 100644 (file)
@@ -18,7 +18,6 @@ type Result = {
 // * 'Stain' corresponds to the stain around the parasites.
 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
 let findMa (green: Image<Gray, float32>) (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, byte> * Image<Gray, byte> =
-
     // We use the filtered image to find the dark stain.
     let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians filteredGreen)
     let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults
@@ -51,7 +50,6 @@ let findMa (green: Image<Gray, float32>) (filteredGreen: Image<Gray, float32>) (
 // * 'Stain' corresponds to the stain around the parasites.
 // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes.
 let find (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, float32> =
-
     use filteredGreenWithoutInfection = filteredGreen.Copy()
     ImgTools.areaCloseF filteredGreenWithoutInfection (int config.InfectionArea)
 
@@ -59,20 +57,14 @@ let find (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result
     ImgTools.areaCloseF filteredGreenWithoutStain (int config.StainArea)
 
     // We use the filtered image to find the dark stain.
+    let _, mean_fg, mean_bg =
+        let hist = ImgTools.histogramImg filteredGreenWithoutInfection 300
+        ImgTools.otsu hist
 
-    // With K-Means.
-    let kmeansResults = logTime "Finding fg/bg (k-means)" (fun () -> KMeans.kmeans (filteredGreenWithoutInfection))
-    let { KMeans.mean_bg = value_bg; KMeans.mean_fg = value_fg; KMeans.d_fg = d_fg } = kmeansResults
-
-    // With K-Medians.
-    (* let kmediansResults = logTime "Finding fg/bg (k-medians)" (fun () -> KMedians.kmedians (filteredGreenWithoutInfection)) // FIXME: avoid converting this again in MainAnalysis
-    let { KMedians.median_bg = value_bg; KMedians.median_fg = value_fg; KMedians.d_fg = d_fg } = kmediansResults *)
-
-    let darkStain = d_fg.Cmp((float value_bg) * config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
-    darkStain._And(filteredGreenWithoutInfection.Cmp(float value_fg, CvEnum.CmpType.LessThan))
+    let darkStain = filteredGreenWithoutInfection.Cmp(-(float mean_bg) * config.Parameters.darkStainLevel + (float mean_fg), CvEnum.CmpType.LessThan)
 
     let marker (img: Image<Gray, float32>) (closed: Image<Gray, float32>) (level: float) : Image<Gray, byte> =
-        let diff = img.Copy() // closed - (img * level)
+        let diff = img.Copy()
         diff._Mul(level)
         CvInvoke.Subtract(closed, diff, diff)
         diff._ThresholdBinary(Gray(0.0), Gray(255.))