52e7dc6ad880e6dd53f97d914fd9eee8d748b1dd
4 open System.Collections.Generic
14 type private EllipseFlaggedKd (e
: Ellipse) =
15 inherit Ellipse (e
.Cx, e
.Cy, e
.A, e
.B, e
.Alpha)
17 member val Removed = false with get
, set
19 interface KdTree.I2DCoords with
20 member this
.X = this
.Cx
21 member this
.Y = this
.Cy
24 let findCells (ellipses
: Ellipse list) (parasites
: ParasitesMarker.Result) (fg
: Image<Gray, byte
>) (config
: Config.Config) : Cell list =
29 let infection = parasites
.infection.Copy() // To avoid to modify the parameter.
31 // This is the minimum window size to check if other ellipses touch 'e'.
32 let searchRegion (e
: Ellipse) = { KdTree.minX
= e
.Cx - (e
.A + config
.maxRBCSize
) * config
.scale
33 KdTree.maxX
= e
.Cx + (e
.A + config
.maxRBCSize
) * config
.scale
34 KdTree.minY
= e
.Cy - (e
.A + config
.maxRBCSize
) * config
.scale
35 KdTree.maxY
= e
.Cy + (e
.A + config
.maxRBCSize
) * config
.scale
}
37 // The minimum window to contain a given ellipse.
38 let ellipseWindow (e
: Ellipse) =
39 let cx, cy
= roundInt
e.Cx, roundInt
e.Cy
40 let a = int (e.A + 0.5)
41 cx - a, cy
- a, cx + a, cy
+ a
43 // 1) Remove ellipses touching the edges.
44 let w = float fg.Width
45 let h = float fg.Height
46 let ellipsesInside = ellipses |> List.map
(fun e -> EllipseFlaggedKd (e, Removed = e.isOutside
w h))
48 // 2) Associate touching ellipses with each ellipses.
49 let tree = KdTree.Tree.BuildTree ellipsesInside
50 let neighbors (e: Ellipse) : (EllipseFlaggedKd * PointD * PointD) list =
51 tree.Search (searchRegion e)
52 // We only keep the ellipses touching 'e'.
53 |> List.choose
(fun otherE
->
54 match EEOver.EEOverlapArea e otherE with
55 | Some (area
, px
, py
) when area
> 0.0 && px
.Length >= 2 && py
.Length >= 2 ->
56 Some (otherE, PointD(px
.[0], py
.[0]), PointD(px
.[1], py
.[1]))
60 let ellipsesWithNeigbors = ellipsesInside |> List.choose
(fun e -> if e.Removed then None else Some (e, neighbors e))
62 // 3) Remove ellipse with a lower percentage of foreground.
63 for e, neighbors in ellipsesWithNeigbors do
64 let minX, minY
, maxX
, maxY
= ellipseWindow e
66 let mutable totalElement = 0
67 let mutable fgElement = 0
69 for y
in minY
.. maxY
do
70 for x
in minX .. maxX
do
71 let yf, xf
= float y, float x
72 if e.Contains xf yf && neighbors |> List.forall
(fun (otherE, _, _) -> not
<| otherE.Contains xf yf)
74 totalElement <- totalElement + 1
75 if fg.Data.[y, x, 0] > 0uy
77 fgElement <- fgElement + 1
79 if totalElement < config
.minimumCellArea
|| (float fgElement) / (float totalElement) < config
.percentageOfFgValidCell
83 // 3) Define pixels associated to each ellipse and create the cells.
85 // Return 'true' if the point 'p' is owned by e.
86 // The lines represents all intersections with other ellipses.
87 let pixelOwnedByE (p
: PointD) (e: Ellipse) (lines: Line list) =
90 let c = PointD(e.Cx, e.Cy)
92 let d2 = Utils.lineFromTwoPoints
c p
93 let p' = Utils.pointFromTwoLines d1 d2
94 yield sign (c.X - p.X) <> sign (c.X - p'.X) || Utils.squaredDistanceTwoPoints
c p' > Utils.squaredDistanceTwoPoints c p // 'false' -> the point is owned by another ellipse.
98 |> List.choose (fun (e, neighbors) ->
103 let minX, minY, maxX, maxY = ellipseWindow e
105 let infectedPixels = List<Point>()
106 let mutable stainPixels = 0
107 let mutable darkStainPixels = 0
108 let mutable nbElement = 0
110 let mutable stain_x = 0.0
111 let mutable stain_x2 = 0.0
112 let mutable stain_y = 0.0
113 let mutable stain_y2 = 0.0
115 let mutable sumCoords_y = 0
116 let mutable sumCoords_x = 0
118 let elements = new Matrix<byte>(maxY - minY + 1, maxX - minX + 1)
119 for y in minY .. maxY do
120 for x in minX .. maxX do
121 let p = PointD(float x, float y)
122 if pixelOwnedByE p e (neighbors |> List.choose (fun (otherE, p1, p2) -> if otherE.Removed then None else Some (Utils.lineFromTwoPoints p1 p2))) &&
123 fg.Data.[y, x, 0] > 0uy
125 elements.[y-minY, x-minX] <- 1uy
126 nbElement <- nbElement + 1
127 sumCoords_y <- sumCoords_y + y
128 sumCoords_x <- sumCoords_x + x
130 if infection.Data.[y, x, 0] > 0uy
132 infectedPixels.Add(Point(x, y))
134 if parasites.stain.Data.[y, x, 0] > 0uy
136 stainPixels <- stainPixels + 1
137 stain_x <- stain_x + (float x)
138 stain_x2 <- stain_x2 + (float x) ** 2.0
139 stain_y <- stain_y + (float y)
140 stain_y2 <- stain_y2 + (float y) ** 2.0
142 if parasites.darkStain.Data.[y, x, 0] > 0uy
144 darkStainPixels <- darkStainPixels + 1
147 if float darkStainPixels > config.MaxDarkStainRatio * (float nbElement) (* ||
148 sqrt (((float sumCoords_x) / (float nbElement) - e.Cx) ** 2.0 + ((float sumCoords_y) / (float nbElement) - e.Cy) ** 2.0) > e.A * config.maxOffcenter *)
151 elif infectedPixels.Count > config.infectionPixelsRequired
153 let infectionToRemove = ImgTools.connectedComponents parasites.stain infectedPixels
154 for p in infectionToRemove do
155 infection.Data.[p.Y, p.X, 0] <- 0uy
160 Some { cellClass = cellClass
161 center = Point(roundInt e.Cx, roundInt e.Cy)
162 elements = elements })