b01a0412deceecaa31da61da83e78210f8a35a24
1
module ParasitemiaCore.Classifier
4 open System.Collections.Generic
12 type CellState = RBC = 1 | Removed = 2 | Peculiar = 3
14 type private EllipseFlaggedKd (e
: Ellipse) =
15 inherit Ellipse (e
.Cx, e
.Cy, e
.A, e
.B, e
.Alpha)
17 member val State = CellState.RBC with get
, set
19 interface KdTree.I2DCoords with
20 member this
.X = this
.Cx
21 member this
.Y = this
.Cy
23 let findCells (ellipses
: Ellipse list) (parasites
: ParasitesMarker.Result) (width
: int) (height
: int) (config
: Config.Config) : Cell list =
24 if ellipses
.IsEmpty then
27 // This is the minimum window size to check if other ellipses touch 'e'.
28 let searchRegion (e
: Ellipse) =
30 KdTree.minX
= e
.Cx - (e
.A + config
.RBCRadius.Max)
31 KdTree.maxX
= e
.Cx + (e
.A + config
.RBCRadius.Max)
32 KdTree.minY
= e
.Cy - (e
.A + config
.RBCRadius.Max)
33 KdTree.maxY
= e
.Cy + (e
.A + config
.RBCRadius.Max)
36 // The minimum window to contain a given ellipse.
37 let ellipseWindow (e
: Ellipse) =
38 let cx, cy
= roundInt
e.Cx, roundInt
e.Cy
39 let a = int (e.A + 0.5f)
40 cx - a, cy
- a, cx + a, cy
+ a
42 // Return 'true' if the point 'p' is owned by e.
43 // The lines represents all intersections with other ellipses.
44 let pixelOwnedByE (p
: PointF) (e : EllipseFlaggedKd) (neighbors
: (EllipseFlaggedKd * PointF * PointF) list) =
47 let c = PointF (e.Cx, e.Cy)
52 fun (otherE, p1, p2) ->
53 if otherE.State = CellState.Removed then
56 Some (otherE, Utils.lineFromTwoPoints p1 p2)
58 if e'.State = e.State then // Peculiar vs peculiar or RBC vs RBC.
59 let d2 = lineFromTwoPoints
c p
60 let c' = PointF (e'.Cx, e'.Cy)
61 let v = pointFromTwoLines d1 (lineFromTwoPoints c c')
62 let case1 = sign
(v.X - c.X) <> sign
(v.X - c'.X) || Utils.squaredDistanceTwoPoints v c > Utils.squaredDistanceTwoPoints v c'
63 if not
(Single.IsInfinity d2.A) then
64 let p' = Utils.pointFromTwoLines d1 d2
66 let dx1, dx2
= (c.X - p.X), (c.X - p'.X)
67 // To avoid rounding issue.
68 if abs dx1 < 0.01f || abs dx2 < 0.01f then c.Y - p.Y, c.Y - p'.Y else dx1, dx2
70 // Yield 'false' when the point is owned by another ellipse.
72 sign
delta <> sign
delta' || Utils.squaredDistanceTwoPoints c p' > Utils.squaredDistanceTwoPoints
c p
74 sign
delta = sign
delta' && Utils.squaredDistanceTwoPoints c p' < Utils.squaredDistanceTwoPoints
c p
78 elif
e.State = CellState.Peculiar then // A peculiar always win against a RBC.
81 not
<| e'.Contains p.X p.Y
85 let ellipses = ellipses |> List.map EllipseFlaggedKd
87 // 1) Associate touching ellipses with each ellipses and remove ellipse with more than two intersections.
88 let tree = KdTree.Tree.BuildTree ellipses
89 let neighbors (e : EllipseFlaggedKd) : (EllipseFlaggedKd * PointF * PointF) list =
90 if e.State <> CellState.Removed then
91 tree.Search (searchRegion e)
92 // We only keep the ellipses touching 'e'.
96 match EEOver.EEOverlapArea e otherE with
97 | Some (_, px, _) when px.Length > 2 ->
98 otherE.State <- CellState.Removed
100 | Some (area, px, py) when area > 0.f && px.Length = 2 ->
101 Some (otherE, PointF (px.[0], py.[0]), PointF (px.[1], py.[1]))
111 let ellipsesWithNeigbors = ellipses |> List.map (fun e -> e, neighbors e)
113 // 2) Remove ellipses touching the edges.
114 let widthF, heightF = float32 width, float32 height
116 if e.IsOutside widthF heightF then e.State <- CellState.Removed
118 // 3) Remove ellipses with a high standard deviation (high contrast).
119 // Obsolete. It was useful when the ellipses result quality wasn't
good.
120 (* let imgData = img.Data
121 let globalStdDeviation = MathNet.Numerics.Statistics.Statistics.PopulationStandardDeviation (seq {
122 for y in 0 .. h - 1 do
123 for x in 0 .. w - 1 do
124 float imgData.[y, x, 0] })
127 if not e.Removed then
128 let shrinkedE = e.Scale 0.9f
129 let minX, minY, maxX, maxY = ellipseWindow shrinkedE
131 let stdDeviation = MathNet.Numerics.Statistics.Statistics.StandardDeviation (seq {
132 for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
133 for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
134 if shrinkedE.Contains (float32 x) (float32 y) then
135 float imgData.[y, x, 0] })
137 if stdDeviation > globalStdDeviation * config.Parameters.standardDeviationMaxRatio then
140 // 4) Remove ellipses with little area.
141 let minArea = config
.RBCRadius.MinArea
142 for e, neighbors in ellipsesWithNeigbors do
143 if e.State <> CellState.Removed then
144 let minX, minY, maxX, maxY = ellipseWindow e
147 for y
= (if minY < 0 then 0 else minY) to (if maxY >= height
then height
- 1 else maxY) do
148 for x
= (if minX < 0 then 0 else minX) to (if maxX >= width
then width
- 1 else maxX) do
149 let p = PointF (float32 x
, float32 y
)
150 if pixelOwnedByE p e neighbors then
153 if area < int minArea then
154 e.State <- CellState.Removed
156 // 5) Define non-rbc (peculiar) cells.
157 let darkStainData = parasites
.darkStain
.Data
160 fun (e, neighbors) ->
161 if e.State = CellState.Removed then
164 let mutable darkStainPixels = 0
165 let mutable nbElement = 0
166 let minX, minY, maxX, maxY = ellipseWindow e
167 for y
= minY to maxY do
168 for x
= minX to maxX do
169 let p = PointF (float32 x
, float32 y
)
170 if pixelOwnedByE p e neighbors then
171 nbElement <- nbElement + 1
172 if darkStainData.[y
, x
, 0] > 0uy then
173 darkStainPixels <- darkStainPixels + 1
175 if float darkStainPixels > config
.Parameters.maxDarkStainRatio
* (float nbElement) then Some e else None
178 // We do not change the state during the process to avoid to have peculiar neighbors which change the behavior of 'pixelOwnedByE'.
179 |> List.iter
(fun e -> e.State <- CellState.Peculiar)
181 // 5) Define pixels associated to each ellipse and create the cells.
182 let diameterParasiteSquared = (2.f
* config
.RBCRadius.ParasiteRadius) ** 2.f
|> roundInt
183 let minimumParasiteArea = config
.RBCRadius.MinimumParasiteArea |> roundInt
185 let nucleusData = parasites
.nucleus
.Copy().Data // Will be modified thus the copy.
186 let parasiteData = parasites
.parasite
.Data
187 let darkStainData = parasites
.darkStain
.Data
191 fun (e, neighbors) ->
192 if e.State = CellState.Removed then
195 let minX, minY, maxX, maxY = ellipseWindow e
197 let nucleusPixels = List<Point> ()
198 let parasitePixels = List<Point> ()
200 let mutable nbElement = 0
202 let elements = new Matrix<byte
> (maxY - minY + 1, maxX - minX + 1)
203 for y
= minY to maxY do
204 for x
= minX to maxX do
205 let p = PointF (float32 x
, float32 y
)
206 if pixelOwnedByE p e neighbors then
207 elements.[y
- minY, x
- minX] <- 1uy
208 nbElement <- nbElement + 1
210 if nucleusData.[y
, x
, 0] > 0uy then
211 nucleusPixels.Add (Point (x
, y
))
213 if parasiteData.[y
, x
, 0] > 0uy then
214 parasitePixels.Add (Point (x
, y
))
217 if nucleusPixels.Count > 0 then
219 for parasitePixel
in parasitePixels do
220 if nucleusPixels.Exists (fun p -> pown
(p.X - parasitePixel
.X) 2 + pown
(p.Y - parasitePixel
.Y) 2 <= diameterParasiteSquared) then
227 if e.State = CellState.Peculiar then
230 elif
nucleusPixels.Count > 0 && parasiteArea >= minimumParasiteArea then
231 let infectionToRemove = Morpho.connectedComponents
parasites.parasite
nucleusPixels
232 for p in infectionToRemove do
233 nucleusData.[p.Y, p.X, 0] <- 0uy
241 cellClass = cellClass
242 center
= Point (roundInt
e.Cx, roundInt
e.Cy)
243 nucleusArea
= if cellClass = InfectedRBC then nucleusPixels.Count else 0
244 parasiteArea = parasiteArea