X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FClassifier.fs;h=96532f8701214b00dfe3d19b9bdce91c217f30cc;hb=3b645f8ff5259f88a33ffbd9a63b10a8640c439f;hp=479ba59c65457e043f3121b4a82d75fd81638c20;hpb=97c24aa168f06f507fdff79429038d78a2c33326;p=master-thesis.git diff --git a/Parasitemia/ParasitemiaCore/Classifier.fs b/Parasitemia/ParasitemiaCore/Classifier.fs index 479ba59..96532f8 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,7 +19,6 @@ 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 @@ -57,13 +55,13 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: let c' = PointF(e'.Cx, e'.Cy) let v = pointFromTwoLines d1 (lineFromTwoPoints c c') let case1 = sign (v.X - c.X) <> sign (v.X - c'.X) || Utils.squaredDistanceTwoPoints v c > Utils.squaredDistanceTwoPoints v c' - if d2.Valid + if not (Single.IsInfinity d2.A) 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 +128,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 +148,8 @@ 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 ellipsesWithNeigbors |> List.choose (fun (e, neighbors) -> if e.Removed @@ -160,7 +159,9 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: let minX, minY, maxX, maxY = ellipseWindow e let infectedPixels = List() - let mutable stainPixels = 0 + let stainPixels = List() + + //let mutable stainPixels = 0 let mutable darkStainPixels = 0 let mutable nbElement = 0 @@ -173,34 +174,49 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: elements.[y-minY, x-minX] <- 1uy nbElement <- nbElement + 1 - if infection.Data.[y, x, 0] > 0uy + let infected = infection.Data.[y, x, 0] > 0uy + let stain = parasites.stain.Data.[y, x, 0] > 0uy + let darkStain = parasites.darkStain.Data.[y, x, 0] > 0uy + + if infected then infectedPixels.Add(Point(x, y)) - if parasites.stain.Data.[y, x, 0] > 0uy + if stain then - stainPixels <- stainPixels + 1 + stainPixels.Add(Point(x, y)) - if parasites.darkStain.Data.[y, x, 0] > 0uy + if darkStain then darkStainPixels <- darkStainPixels + 1 + let mutable stainArea = 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) + then + stainArea <- stainArea + 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) + //|| float stainPixels > config.Parameters.maxStainRatio * (float nbElement) then Peculiar - elif infectedPixels.Count >= 1 + + elif infectedPixels.Count > 0 && stainArea >= minimumParasiteArea then let infectionToRemove = ImgTools.connectedComponents parasites.stain infectedPixels for p in infectionToRemove do infection.Data.[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 + infectedArea = if cellClass = InfectedRBC then infectedPixels.Count else 0 + stainArea = stainArea elements = elements })