X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FClassifier.fs;h=f4b08446f9f6dc6b4762fc215f2c392152ec97d0;hb=3f8b0d281b3058faf23dbd0363de440bd04c6574;hp=c523790e9f70c4c80317cbfbede44e696c1f41a0;hpb=170abb893b9e6babbf93276e39e8c984cedbc68f;p=master-thesis.git diff --git a/Parasitemia/ParasitemiaCore/Classifier.fs b/Parasitemia/ParasitemiaCore/Classifier.fs index c523790..f4b0844 100644 --- a/Parasitemia/ParasitemiaCore/Classifier.fs +++ b/Parasitemia/ParasitemiaCore/Classifier.fs @@ -10,7 +10,6 @@ open Emgu.CV.Structure open Types open Utils - type private EllipseFlaggedKd (e: Ellipse) = inherit Ellipse (e.Cx, e.Cy, e.A, e.B, e.Alpha) @@ -20,14 +19,11 @@ 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) (config: Config.Config) : Cell list = if ellipses.IsEmpty then [] else - let infection = parasites.infection.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) KdTree.maxX = e.Cx + (e.A + config.RBCRadius.Max) @@ -61,9 +57,9 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: then let p' = Utils.pointFromTwoLines d1 d2 let delta, delta' = - let d = c.X - p.X - // To avoid rounding. - if abs d < 0.001f then c.Y - p.Y, c.Y - p'.Y else d, c.X - p'.X + let dx1, dx2 = (c.X - p.X), (c.X - p'.X) + // To avoid rounding issue. + if abs dx1 < 0.01f || abs dx2 < 0.01f then c.Y - p.Y, c.Y - p'.Y else dx1, dx2 // Yield 'false' when the point is owned by another ellipse. if case1 @@ -130,7 +126,6 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: if stdDeviation > globalStdDeviation * config.Parameters.standardDeviationMaxRatio then e.Removed <- true - // 4) Remove ellipses with little area. let minArea = config.RBCRadius.MinArea for e, neighbors in ellipsesWithNeigbors do @@ -151,6 +146,13 @@ 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 minimumParasiteArea = config.RBCRadius.MinimumParasiteArea |> roundInt + + let nucleusData = parasites.nucleus.Copy().Data // Will be modified thus the copy. + let parasiteData = parasites.parasite.Data + let darkStainData = parasites.darkStain.Data + ellipsesWithNeigbors |> List.choose (fun (e, neighbors) -> if e.Removed @@ -159,8 +161,9 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: else let minX, minY, maxX, maxY = ellipseWindow e - let infectedPixels = List() - let mutable stainPixels = 0 + let nucleusPixels = List() + let parasitePixels = List() + let mutable darkStainPixels = 0 let mutable nbElement = 0 @@ -170,37 +173,46 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: 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 - elements.[y-minY, x-minX] <- 1uy + elements.[y - minY, x - minX] <- 1uy nbElement <- nbElement + 1 - if infection.Data.[y, x, 0] > 0uy + if nucleusData.[y, x, 0] > 0uy then - infectedPixels.Add(Point(x, y)) + nucleusPixels.Add(Point(x, y)) - if parasites.stain.Data.[y, x, 0] > 0uy + if parasiteData.[y, x, 0] > 0uy then - stainPixels <- stainPixels + 1 + parasitePixels.Add(Point(x, y)) - if parasites.darkStain.Data.[y, x, 0] > 0uy + if darkStainData.[y, x, 0] > 0uy then darkStainPixels <- darkStainPixels + 1 + let mutable parasiteArea = 0 + 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) + then + parasiteArea <- parasiteArea + 1 + let cellClass = - if float darkStainPixels > config.Parameters.maxDarkStainRatio * (float nbElement) || - float stainPixels > config.Parameters.maxStainRatio * (float nbElement) + if float darkStainPixels > config.Parameters.maxDarkStainRatio * (float nbElement) then Peculiar - elif infectedPixels.Count >= 1 + + elif nucleusPixels.Count > 0 && parasiteArea >= minimumParasiteArea then - let infectionToRemove = ImgTools.connectedComponents parasites.stain infectedPixels + let infectionToRemove = Morpho.connectedComponents parasites.parasite nucleusPixels for p in infectionToRemove do - infection.Data.[p.Y, p.X, 0] <- 0uy + nucleusData.[p.Y, p.X, 0] <- 0uy InfectedRBC + else HealthyRBC Some { cellClass = cellClass center = Point(roundInt e.Cx, roundInt e.Cy) - infectedArea = infectedPixels.Count - stainArea = stainPixels + nucleusArea = if cellClass = InfectedRBC then nucleusPixels.Count else 0 + parasiteArea = parasiteArea elements = elements })