Remove the parasite detection function from Ma.
authorGreg Burri <greg.burri@gmail.com>
Mon, 25 Jan 2016 16:12:09 +0000 (17:12 +0100)
committerGreg Burri <greg.burri@gmail.com>
Mon, 25 Jan 2016 16:12:09 +0000 (17:12 +0100)
Parasitemia/ParasitemiaCore/Classifier.fs
Parasitemia/ParasitemiaCore/Config.fs
Parasitemia/ParasitemiaCore/MainAnalysis.fs
Parasitemia/ParasitemiaCore/ParasitesMarker.fs
Parasitemia/ParasitemiaCore/Types.fs
Parasitemia/ParasitemiaUI/State.fs

index 96532f8..2b6f3be 100644 (file)
@@ -24,7 +24,7 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
     then
         []
     else
-        let infection = parasites.infection.Copy() // To avoid to modify the parameter.
+        let infection = parasites.nucleus.Copy() // To avoid to modify the parameter.
 
         // This is the minimum window size to check if other ellipses touch 'e'.
         let searchRegion (e: Ellipse) = { KdTree.minX = e.Cx - (e.A + config.RBCRadius.Max)
@@ -159,7 +159,7 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                 let minX, minY, maxX, maxY = ellipseWindow e
 
                 let infectedPixels = List<Point>()
-                let stainPixels = List<Point>()
+                let cytoplasmPixels = List<Point>()
 
                 //let mutable stainPixels = 0
                 let mutable darkStainPixels = 0
@@ -175,7 +175,7 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                             nbElement <- nbElement + 1
 
                             let infected = infection.Data.[y, x, 0] > 0uy
-                            let stain = parasites.stain.Data.[y, x, 0] > 0uy
+                            let stain = parasites.cytoplasm.Data.[y, x, 0] > 0uy
                             let darkStain = parasites.darkStain.Data.[y, x, 0] > 0uy
 
                             if infected
@@ -184,19 +184,19 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
 
                             if stain
                             then
-                                stainPixels.Add(Point(x, y))
+                                cytoplasmPixels.Add(Point(x, y))
 
                             if darkStain
                             then
                                 darkStainPixels <- darkStainPixels + 1
 
-                let mutable stainArea = 0
+                let mutable cytoplasmArea = 0
                 if infectedPixels.Count > 0
                 then
-                    for stainPixel in stainPixels do
-                        if infectedPixels.Exists(fun p -> pown (p.X - stainPixel.X) 2 + pown (p.Y - stainPixel.Y) 2 <= perimeterParasiteSquared)
+                    for cytoplasmPixel in cytoplasmPixels do
+                        if infectedPixels.Exists(fun p -> pown (p.X - cytoplasmPixel.X) 2 + pown (p.Y - cytoplasmPixel.Y) 2 <= perimeterParasiteSquared)
                         then
-                            stainArea <- stainArea + 1
+                            cytoplasmArea <- cytoplasmArea + 1
 
 
                 let cellClass =
@@ -205,9 +205,9 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                     then
                         Peculiar
 
-                    elif infectedPixels.Count > 0 && stainArea >= minimumParasiteArea
+                    elif infectedPixels.Count > 0 && cytoplasmArea >= minimumParasiteArea
                     then
-                        let infectionToRemove = ImgTools.connectedComponents parasites.stain infectedPixels
+                        let infectionToRemove = ImgTools.connectedComponents parasites.cytoplasm infectedPixels
                         for p in infectionToRemove do
                             infection.Data.[p.Y, p.X, 0] <- 0uy
                         InfectedRBC
@@ -217,6 +217,6 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
 
                 Some { cellClass = cellClass
                        center = Point(roundInt e.Cx, roundInt e.Cy)
-                       infectedArea = if cellClass = InfectedRBC then infectedPixels.Count else 0
-                       stainArea = stainArea
+                       nucleusArea = if cellClass = InfectedRBC then infectedPixels.Count else 0
+                       parasiteArea = cytoplasmArea
                        elements = elements })
index 1110096..3089953 100644 (file)
@@ -13,8 +13,9 @@ type Parameters = {
     rbcDiameter: float<μm>
     resolution: float<ppi>
 
-    colorContribution_BG_RBC: float * float * float // (R, G, B).
-    colorContribution_RBC_parasite: float * float * float // (R, G, B).
+    averageColor_BG: float32 * float32 * float32 // R * G * B.
+    averageColor_RBC: float32 * float32 * float32 // R * G * B.
+    averageColor_Parasite: float32 * float32 * float32 // R * G * B.
 
     ratioAreaPaleCenter: float32 // The area of the second opening is 'ratioSecondAreaOpen' * mean RBC area. It's applied only if greater than 'initialAreaOpen'.
 
@@ -24,7 +25,6 @@ type Parameters = {
     maxRbcRadius: float32 // Factor of the mean RBC radius.
 
     LPFStandardDeviationParasite: float<μm> // Sigma parameter of the gaussian to remove the high frequency noise.
-    LPFStandardDeviationStain: float<μm>
     LPFStandardDeviationRBC: float<μm>
 
     // Ellipse.
@@ -35,8 +35,8 @@ type Parameters = {
     maxDarkStainRatio: float // When a cell must own less than this ratio to be a RBC.
 
     parasiteRadiusRatio: float32 // The ratio of the parasite radius of the RBC radius.
-
     minimumParasiteAreaRatio: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
+
     cytoplasmSensitivity: float // between 0 (the least sensitive) and 1 (the most sensitive).
 
     nucleusAreaRatio: float32 // Factor of a RBC area. 0.5 means the half of RBC area.
@@ -50,8 +50,13 @@ let defaultParameters = {
     rbcDiameter = 8.<μm>
     resolution = 220.e3<ppi> // 220.e3<ppi> Correspond to 50X.
 
-    colorContribution_BG_RBC = (* 0., 1., 0. *) 0.16, 0.44, 0.4
-    colorContribution_RBC_parasite = (* 1., 0., 0. *) 0.54, 0.41, 0.05
+    averageColor_BG = 113.3f, 135.3f, 150.3f
+    averageColor_RBC = 94.7f, 80.7f, 99.3f
+    averageColor_Parasite = 76.f, 58.f, 94.f
+
+    (*averageColor_BG = 179.f, 148.f, 121.f
+    averageColor_RBC = 141.f, 96.f, 83.f
+    averageColor_Parasite = 123.f, 89.f, 83.f*)
 
     ratioAreaPaleCenter = 2.f / 5.f // The ratio between an RBC area and the area of the its pale center.
 
@@ -61,12 +66,11 @@ let defaultParameters = {
     maxRbcRadius = 0.3f
 
     LPFStandardDeviationParasite = 0.15<μm>
-    LPFStandardDeviationStain = 0.15<μm> // 0.12
-    LPFStandardDeviationRBC = 0.2<μm> // 8.5e-6<inch>. // 0.2<μm>
+    LPFStandardDeviationRBC = 0.2<μm>
 
     factorNbPick = 1.0
 
-    darkStainLevel = 0.25 // 0.3
+    darkStainLevel = 0.25
     maxDarkStainRatio = 0.1 // 10 %
 
     parasiteRadiusRatio = 0.5f // 40 %
@@ -124,10 +128,6 @@ type Config (param: Parameters) =
         let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationParasite) * parameters.resolution
         float stdDeviation
 
-    member this.LPFStandardDeviationStain =
-        let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationStain) * parameters.resolution
-        float stdDeviation
-
     member this.LPFStandardDeviationRBC =
         let stdDeviation: float<px> = (μmToInch parameters.LPFStandardDeviationRBC) * parameters.resolution
         float stdDeviation
index cae6427..5a8e16d 100644 (file)
@@ -53,11 +53,11 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportPr
         use img_float = img.Convert<Bgr, float32>()
 
         // use img_RBC = mergeChannels img_float config.Parameters.colorContribution_BG_RBC
-        use img_RBC = mergeChannelsWithProjection img_float (94.7f, 80.7f, 99.3f) (113.3f, 135.3f, 150.3f) 255.
+        use img_RBC = mergeChannelsWithProjection img_float config.Parameters.averageColor_RBC config.Parameters.averageColor_BG 255.
         let img_RBC_filtered = gaussianFilter img_RBC config.LPFStandardDeviationRBC
 
         //use img_parasites = mergeChannels img_float config.Parameters.colorContribution_RBC_parasite
-        use img_parasites = mergeChannelsWithProjection img_float (76.f, 58.f, 94.f) (94.7f, 80.7f, 99.3f) 255.
+        use img_parasites = mergeChannelsWithProjection img_float config.Parameters.averageColor_Parasite config.Parameters.averageColor_RBC 255.
 
         logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution)
 
@@ -114,8 +114,8 @@ let doAnalysis (img: Image<Bgr, byte>) (name: string) (config: Config) (reportPr
                 saveMat (edges * 255.0) (buildFileName " - edges.png")
 
                 saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png")
-                saveImg parasites.stain (buildFileName " - parasites - stain.png")
-                saveImg parasites.infection (buildFileName " - parasites - infection.png")
+                saveImg parasites.cytoplasm (buildFileName " - parasites - stain.png")
+                saveImg parasites.nucleus (buildFileName " - parasites - infection.png")
 
                 let imgAllEllipses = img.Copy()
                 drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04
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
 
 
index 946c2c7..8f6c34a 100644 (file)
@@ -50,8 +50,8 @@ type CellClass = HealthyRBC | InfectedRBC | Peculiar
 type Cell = {
     cellClass: CellClass
     center: Point
-    infectedArea: int
-    stainArea: int
+    nucleusArea: int
+    parasiteArea: int
     elements: Matrix<byte> }
 
 [<Struct>]
index e15dc9b..5b6a91c 100644 (file)
@@ -142,7 +142,7 @@ type State (defaultConfig: ParasitemiaCore.Config.Config) =
 
         sourceImage.rbcs <- cells
             |> List.filter (fun cell -> match cell.cellClass with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
-            |> List.sortByDescending (fun cell -> cell.infectedArea, (w - cell.center.X) + (h - cell.center.Y))
+            |> List.sortByDescending (fun cell -> cell.nucleusArea, (w - cell.center.X) + (h - cell.center.Y))
             |> List.mapi (fun i cell ->
                 let center = Point(float cell.center.X, float cell.center.Y)
                 let infected, setManually =
@@ -155,7 +155,7 @@ type State (defaultConfig: ParasitemiaCore.Config.Config) =
                   setManually = setManually
                   center = center
                   size = Size(float cell.elements.Width, float cell.elements.Height)
-                  infectedArea = cell.infectedArea })
+                  infectedArea = cell.nucleusArea })
 
         alteredSinceLastSave <- true