X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FClassifier.fs;h=0012a4f1a691346e15d647e6f6cecd29f73bb9a0;hb=ec96e4c38dd6beaf22b4e2a2ebb87248fea6f209;hp=9f4fdd051d5553765bb2ba113e42d5e67ddadc2f;hpb=074d6b0377f51e868deb1c427891c722d0270deb;p=master-thesis.git diff --git a/Parasitemia/ParasitemiaCore/Classifier.fs b/Parasitemia/ParasitemiaCore/Classifier.fs index 9f4fdd0..0012a4f 100644 --- a/Parasitemia/ParasitemiaCore/Classifier.fs +++ b/Parasitemia/ParasitemiaCore/Classifier.fs @@ -19,13 +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 = +let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (width: int) (height: int) (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) @@ -38,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) = @@ -59,9 +52,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 @@ -102,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 @@ -126,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 @@ -136,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 @@ -148,6 +143,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 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. + let parasiteData = parasites.parasite.Data + let darkStainData = parasites.darkStain.Data + ellipsesWithNeigbors |> List.choose (fun (e, neighbors) -> if e.Removed @@ -156,8 +158,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 @@ -167,37 +170,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 <= diameterParasiteSquared) + 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 })