Project the colors to have the best contrast for RBCs and parasites analyze.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ParasitesMarker.fs
index 22ea041..73f9fe1 100644 (file)
@@ -6,8 +6,6 @@ open System.Linq
 open Emgu.CV
 open Emgu.CV.Structure
 
-open Logger
-
 open Utils
 
 type Result = {
@@ -21,7 +19,7 @@ type Result = {
 // * '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 = Log.LogWithTime("Finding fg/bg (k-medians)", Severity.USER, (fun () -> KMedians.kmedians filteredGreen))
+    let kmediansResults = KMedians.kmedians filteredGreen
     let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults
     let darkStain = d_fg.Cmp(median_bg * float config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan)
     darkStain._And(filteredGreen.Cmp(median_fg, CvEnum.CmpType.LessThan))
@@ -50,12 +48,15 @@ let findMa (green: Image<Gray, float32>) (filteredGreen: Image<Gray, float32>) (
 // * 'Dark stain' corresponds to the colored pixel, it's independent of the size of the areas.
 // * '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.RBCRadius.InfectionArea)
+let find (img: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gray, float32> * Image<Gray, float32> =
+
+    let imgFilteredInfection = ImgTools.gaussianFilter img config.LPFStandardDeviationParasite
+    let filteredGreenWithoutInfection = imgFilteredInfection.Copy()
+    ImgTools.areaCloseF filteredGreenWithoutInfection (roundInt config.RBCRadius.NucleusArea)
 
+    (*
     let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
-    ImgTools.areaCloseF filteredGreenWithoutStain (int config.RBCRadius.StainArea)
+    ImgTools.areaCloseF filteredGreenWithoutStain (int config.RBCRadius.StainArea) *)
 
     let darkStain =
         // We use the filtered image to find the dark stain.
@@ -71,19 +72,35 @@ let find (filteredGreen: Image<Gray, float32>) (config: Config.Config) : Result
         diff._ThresholdBinary(Gray(0.0), Gray(255.))
         diff.Convert<Gray, byte>()
 
-    let infectionMarker = marker filteredGreen filteredGreenWithoutInfection (1. / config.Parameters.infectionSensitivity)
-    let stainMarker = marker filteredGreenWithoutInfection filteredGreenWithoutStain (1. / config.Parameters.stainSensitivity)
+    let infectionMarker = marker imgFilteredInfection filteredGreenWithoutInfection (1. / config.Parameters.infectionSensitivity)
 
-    // TODO: comprendre pourquoi des valeurs sont negatives!?!?
-    (*
-    let blackTopHat = filteredGreen.CopyBlank()
-    CvInvoke.Subtract(filteredGreenWithoutInfection, filteredGreen, blackTopHat)
-    ImgTools.saveImg (ImgTools.normalizeAndConvert blackTopHat) "BottomHat.png"
-    *)
+    let imgFilteredStain = ImgTools.gaussianFilter img config.LPFStandardDeviationStain
+    let areaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter
+    //ImgTools.areaOpenF imgFilteredStain areaOpening
+
+    let filteredGreenWithoutStain = imgFilteredStain.CopyBlank()
+    let kernelSize =
+        let size = roundInt (config.RBCRadius.Pixel / 5.f)
+        if size % 2 = 0 then size + 1 else size
+    use kernel =
+        if kernelSize <= 3
+        then
+            CvInvoke.GetStructuringElement(CvEnum.ElementShape.Rectangle, Size(3, 3), Point(-1, -1))
+        else
+            CvInvoke.GetStructuringElement(CvEnum.ElementShape.Ellipse, Size(kernelSize, kernelSize), Point(-1, -1))
+
+    CvInvoke.MorphologyEx(imgFilteredStain, filteredGreenWithoutStain, CvEnum.MorphOp.Close, kernel, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar())
+    let stainMarker = marker (*filteredGreenWithoutInfection*) imgFilteredStain filteredGreenWithoutStain (1. / config.Parameters.cytoplasmSensitivity)
+
+    //
+    (*let blackTopHat = filteredGreenWithoutStain.CopyBlank()
+    CvInvoke.Subtract(filteredGreenWithoutStain, imgFilteredStain, blackTopHat)
+    ImgTools.saveImg blackTopHat "blackTopHat.png"*)
 
     { darkStain = darkStain
       infection = infectionMarker
       stain = stainMarker },
-    filteredGreenWithoutStain
+    filteredGreenWithoutStain,
+    filteredGreenWithoutInfection