Add a way to detect the membrane of a parasite in the ring stage.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ParasitesMarker.fs
index ffe0d14..f4d9f35 100644 (file)
@@ -48,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()
+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 (int config.RBCRadius.InfectionArea)
 
+    (*
     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.
@@ -69,19 +72,34 @@ 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)
-
-    // TODO: comprendre pourquoi des valeurs sont negatives!?!?
-    (*
-    let blackTopHat = filteredGreen.CopyBlank()
-    CvInvoke.Subtract(filteredGreenWithoutInfection, filteredGreen, blackTopHat)
-    ImgTools.saveImg (ImgTools.normalizeAndConvert blackTopHat) "BottomHat.png"
-    *)
+    let infectionMarker = marker imgFilteredInfection filteredGreenWithoutInfection (1. / config.Parameters.infectionSensitivity)
+
+    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.stainSensitivity)
+
+    //
+    (*let blackTopHat = filteredGreenWithoutStain.CopyBlank()
+    CvInvoke.Subtract(filteredGreenWithoutStain, imgFilteredStain, blackTopHat)
+    ImgTools.saveImg blackTopHat "blackTopHat.png"*)
 
     { darkStain = darkStain
       infection = infectionMarker
       stain = stainMarker },
-    filteredGreenWithoutStain
+    filteredGreenWithoutStain,
+    filteredGreenWithoutInfection