Remove the parasite detection function from Ma.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ParasitesMarker.fs
index 73f9fe1..e3c3ca2 100644 (file)
@@ -9,61 +9,23 @@ open Emgu.CV.Structure
 open Utils
 
 type Result = {
-    darkStain: Image<Gray, byte>
-    infection: Image<Gray, byte>
-    stain: Image<Gray, byte> }
+    darkStain: Image<Gray, byte> // Colored pixel, it's independent of the size of the areas. It corresponds to white cells, schizontes, gametocytes, throphozoites.
+    nucleus: Image<Gray, byte> // Parasite nucleus. It may contain some debris. It shouldn't contain thrombocytes or larger elements.
+    cytoplasm: Image<Gray, byte> } // Parasite cytoplasm.
 
-// Create three binary markers :
-// * '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 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 = 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))
-    darkStain._And(fg)
-
-    let fgFloat = (fg / 255.0).Convert<Gray, float32>()
-    use greenWithoutBg = ImgTools.gaussianFilter green 1.0
-    greenWithoutBg.SetValue(Gray(0.0), fg.Not())
-
-    let findSmears (sigma: float) (level: float) : Image<Gray, byte> =
-        use greenWithoutBgSmoothed = ImgTools.gaussianFilter greenWithoutBg sigma
-        use fgSmoothed = ImgTools.gaussianFilter fgFloat sigma
-        let smears = (greenWithoutBg.Mul(fgSmoothed)).Cmp(greenWithoutBgSmoothed.Mul(level), CvEnum.CmpType.LessThan)
-        smears._And(fg)
-        smears
-
-    let tmp = filteredGreen.Convert<Gray, byte>()
-
-    { darkStain = darkStain;
-      stain = findSmears 10. 0.9
-      infection = findSmears 2.2 0.87 },
-    tmp,
-    tmp
-
-// Create three binary markers :
-// * '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 (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 imgFilteredParasite = ImgTools.gaussianFilter img config.LPFStandardDeviationParasite
 
-    (*
-    let filteredGreenWithoutStain = filteredGreenWithoutInfection.Copy()
-    ImgTools.areaCloseF filteredGreenWithoutStain (int config.RBCRadius.StainArea) *)
+    let filteredGreenWithoutNucleus = imgFilteredParasite.Copy()
+    ImgTools.areaCloseF filteredGreenWithoutNucleus (roundInt config.RBCRadius.NucleusArea)
 
     let darkStain =
         // We use the filtered image to find the dark stain.
         let _, mean_fg, mean_bg =
-            let hist = ImgTools.histogramImg filteredGreenWithoutInfection 300
+            let hist = ImgTools.histogramImg filteredGreenWithoutNucleus 300
             ImgTools.otsu hist
-        filteredGreenWithoutInfection.Cmp(-(float mean_bg) * config.Parameters.darkStainLevel + (float mean_fg), CvEnum.CmpType.LessThan)
+        filteredGreenWithoutNucleus.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()
@@ -72,13 +34,9 @@ let find (img: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gr
         diff._ThresholdBinary(Gray(0.0), Gray(255.))
         diff.Convert<Gray, byte>()
 
-    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 nucleusMarker = marker imgFilteredParasite filteredGreenWithoutNucleus (1. / config.Parameters.infectionSensitivity)
 
-    let filteredGreenWithoutStain = imgFilteredStain.CopyBlank()
+    let filteredGreenWithoutCytoplasm = imgFilteredParasite.CopyBlank()
     let kernelSize =
         let size = roundInt (config.RBCRadius.Pixel / 5.f)
         if size % 2 = 0 then size + 1 else size
@@ -89,18 +47,13 @@ let find (img: Image<Gray, float32>) (config: Config.Config) : Result * Image<Gr
         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"*)
+    CvInvoke.MorphologyEx(imgFilteredParasite, filteredGreenWithoutCytoplasm, CvEnum.MorphOp.Close, kernel, Point(-1, -1), 1, CvEnum.BorderType.Replicate, MCvScalar())
+    let cytoplasmMarker = marker imgFilteredParasite filteredGreenWithoutCytoplasm (1. / config.Parameters.cytoplasmSensitivity)
 
     { darkStain = darkStain
-      infection = infectionMarker
-      stain = stainMarker },
-    filteredGreenWithoutStain,
-    filteredGreenWithoutInfection
+      nucleus = nucleusMarker
+      cytoplasm = cytoplasmMarker },
+    filteredGreenWithoutCytoplasm,
+    filteredGreenWithoutNucleus