Remove the removing of cells bases on standard deviation.
[master-thesis.git] / Parasitemia / ParasitemiaCore / Classifier.fs
index f4b0844..0012a4f 100644 (file)
@@ -19,7 +19,7 @@ type private EllipseFlaggedKd (e: Ellipse) =
         member this.X = this.Cx
         member this.Y = this.Cy
 
-let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: Image<Gray, float32>) (config: Config.Config) : Cell list =
+let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (width: int) (height: int) (config: Config.Config) : Cell list =
     if ellipses.IsEmpty
     then
         []
@@ -36,11 +36,6 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
             let a = int (e.A + 0.5f)
             cx - a, cy - a, cx + a, cy + a
 
-        let w = img.Width
-        let w_f = float32 w
-        let h = img.Height
-        let h_f = float32 h
-
         // Return 'true' if the point 'p' is owned by e.
         // The lines represents all intersections with other ellipses.
         let pixelOwnedByE (p: PointF) (e: Ellipse) (others: (Ellipse * Line) list) =
@@ -100,11 +95,13 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
         let ellipsesWithNeigbors = ellipses |> List.map (fun e -> e, neighbors e) |> List.rev
 
         // 2) Remove ellipses touching the edges.
+        let widthF, heightF = float32 width, float32 height
         for e in ellipses do
-            if e.isOutside w_f h_f then e.Removed <- true
+            if e.isOutside widthF heightF then e.Removed <- true
 
         // 3) Remove ellipses with a high standard deviation (high contrast).
-        let imgData = img.Data
+        // Obsolete. It was useful when the ellipses result quality wasn't good.
+        (* let imgData = img.Data
         let globalStdDeviation = MathNet.Numerics.Statistics.Statistics.PopulationStandardDeviation(seq {
             for y in 0 .. h - 1 do
                 for x in 0 .. w - 1 do
@@ -124,7 +121,7 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                                 yield float imgData.[y, x, 0] })
 
                 if stdDeviation > globalStdDeviation * config.Parameters.standardDeviationMaxRatio then
-                    e.Removed <- true
+                    e.Removed <- true *)
 
         // 4) Remove ellipses with little area.
         let minArea = config.RBCRadius.MinArea
@@ -134,8 +131,8 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                 let minX, minY, maxX, maxY = ellipseWindow e
 
                 let mutable area = 0
-                for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
-                    for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
+                for y in (if minY < 0 then 0 else minY) .. (if maxY >= height then height - 1 else maxY) do
+                    for x in (if minX < 0 then 0 else minX) .. (if maxX >= width then width - 1 else maxX) do
                         let p = PointF(float32 x, float32 y)
                         if pixelOwnedByE p e (neighbors |> List.choose (fun (otherE, p1, p2) -> if otherE.Removed then None else Some (otherE :> Ellipse, Utils.lineFromTwoPoints p1 p2)))
                         then
@@ -146,7 +143,7 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                     e.Removed <- true
 
         // 5) Define pixels associated to each ellipse and create the cells.
-        let perimeterParasiteSquared = (2.f * config.RBCRadius.ParasiteRadius) ** 2.f |> roundInt
+        let diameterParasiteSquared = (2.f * config.RBCRadius.ParasiteRadius) ** 2.f |> roundInt
         let minimumParasiteArea = config.RBCRadius.MinimumParasiteArea |> roundInt
 
         let nucleusData = parasites.nucleus.Copy().Data // Will be modified thus the copy.
@@ -192,7 +189,7 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img:
                 if nucleusPixels.Count > 0
                 then
                     for parasitePixel in parasitePixels do
-                        if nucleusPixels.Exists(fun p -> pown (p.X - parasitePixel.X) 2 + pown (p.Y - parasitePixel.Y) 2 <= perimeterParasiteSquared)
+                        if nucleusPixels.Exists(fun p -> pown (p.X - parasitePixel.X) 2 + pown (p.Y - parasitePixel.Y) 2 <= diameterParasiteSquared)
                         then
                             parasiteArea <- parasiteArea + 1