X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FClassifier.fs;h=0012a4f1a691346e15d647e6f6cecd29f73bb9a0;hb=ec96e4c38dd6beaf22b4e2a2ebb87248fea6f209;hp=2b6f3bee0ab729f6c401ffceca5f58c7b260c0ef;hpb=828e126c88524d3dc123abc966a132532aed118b;p=master-thesis.git diff --git a/Parasitemia/ParasitemiaCore/Classifier.fs b/Parasitemia/ParasitemiaCore/Classifier.fs index 2b6f3be..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.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) 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) = @@ -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,8 +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 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. + let parasiteData = parasites.parasite.Data + let darkStainData = parasites.darkStain.Data + ellipsesWithNeigbors |> List.choose (fun (e, neighbors) -> if e.Removed @@ -158,10 +158,9 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: else let minX, minY, maxX, maxY = ellipseWindow e - let infectedPixels = List() - let cytoplasmPixels = List() + let nucleusPixels = List() + let parasitePixels = List() - //let mutable stainPixels = 0 let mutable darkStainPixels = 0 let mutable nbElement = 0 @@ -171,45 +170,39 @@ 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 - let infected = infection.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 + if nucleusData.[y, x, 0] > 0uy then - infectedPixels.Add(Point(x, y)) + nucleusPixels.Add(Point(x, y)) - if stain + if parasiteData.[y, x, 0] > 0uy then - cytoplasmPixels.Add(Point(x, y)) + parasitePixels.Add(Point(x, y)) - if darkStain + if darkStainData.[y, x, 0] > 0uy then darkStainPixels <- darkStainPixels + 1 - let mutable cytoplasmArea = 0 - if infectedPixels.Count > 0 + let mutable parasiteArea = 0 + if nucleusPixels.Count > 0 then - for cytoplasmPixel in cytoplasmPixels do - if infectedPixels.Exists(fun p -> pown (p.X - cytoplasmPixel.X) 2 + pown (p.Y - cytoplasmPixel.Y) 2 <= perimeterParasiteSquared) + for parasitePixel in parasitePixels do + if nucleusPixels.Exists(fun p -> pown (p.X - parasitePixel.X) 2 + pown (p.Y - parasitePixel.Y) 2 <= diameterParasiteSquared) then - cytoplasmArea <- cytoplasmArea + 1 - + parasiteArea <- parasiteArea + 1 let cellClass = if float darkStainPixels > config.Parameters.maxDarkStainRatio * (float nbElement) - //|| float stainPixels > config.Parameters.maxStainRatio * (float nbElement) then Peculiar - elif infectedPixels.Count > 0 && cytoplasmArea >= minimumParasiteArea + elif nucleusPixels.Count > 0 && parasiteArea >= minimumParasiteArea then - let infectionToRemove = ImgTools.connectedComponents parasites.cytoplasm 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 @@ -217,6 +210,6 @@ let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: Some { cellClass = cellClass center = Point(roundInt e.Cx, roundInt e.Cy) - nucleusArea = if cellClass = InfectedRBC then infectedPixels.Count else 0 - parasiteArea = cytoplasmArea + nucleusArea = if cellClass = InfectedRBC then nucleusPixels.Count else 0 + parasiteArea = parasiteArea elements = elements })