ea3f9ddeb3fe9b0ec3c1de31a7ff8f011fe14d94
1
module ParasitemiaCore.Classifier
4 open System.Collections.Generic
13 type CellState = RBC = 1 | Removed = 2 | Peculiar = 3
15 type private EllipseFlaggedKd (e
: Ellipse) =
16 inherit Ellipse (e
.Cx, e
.Cy, e
.A, e
.B, e
.Alpha)
18 member val State = CellState.RBC with get
, set
20 interface KdTree.I2DCoords with
21 member this
.X = this
.Cx
22 member this
.Y = this
.Cy
24 let findCells (ellipses
: Ellipse list) (parasites
: ParasitesMarker.Result) (width
: int) (height
: int) (config
: Config.Config) : Cell list =
29 // This is the minimum window size to check if other ellipses touch 'e'.
30 let searchRegion (e
: Ellipse) = { 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) }
35 // The minimum window to contain a given ellipse.
36 let ellipseWindow (e
: Ellipse) =
37 let cx, cy
= roundInt
e.Cx, roundInt
e.Cy
38 let a = int (e.A + 0.5f)
39 cx - a, cy
- a, cx + a, cy
+ a
41 // Return 'true' if the point 'p' is owned by e.
42 // The lines represents all intersections with other ellipses.
43 let pixelOwnedByE (p
: PointF) (e: EllipseFlaggedKd) (neighbors
: (EllipseFlaggedKd * PointF * PointF) list) =
46 let c = PointF(e.Cx, e.Cy)
48 for e', d1 in neighbors
49 |> List.choose (fun (otherE, p1, p2) ->
50 if otherE.State = CellState.Removed
52 else Some (otherE, Utils.lineFromTwoPoints p1 p2)) do
53 if e'.State = e.State // Peculiar vs peculiar or RBC vs RBC.
55 let d2 = lineFromTwoPoints
c p
56 let c' = PointF(e'.Cx, e'.Cy)
57 let v = pointFromTwoLines d1 (lineFromTwoPoints c c')
58 let case1 = sign
(v.X - c.X) <> sign
(v.X - c'.X) || Utils.squaredDistanceTwoPoints v c > Utils.squaredDistanceTwoPoints v c'
59 if not
(Single.IsInfinity d2.A)
61 let p' = Utils.pointFromTwoLines d1 d2
63 let dx1, dx2
= (c.X - p.X), (c.X - p'.X)
64 // To avoid rounding issue.
65 if abs dx1 < 0.01f || abs dx2 < 0.01f then c.Y - p.Y, c.Y - p'.Y else dx1, dx2
67 // Yield 'false' when the point is owned by another ellipse.
70 yield sign
delta <> sign
delta' || Utils.squaredDistanceTwoPoints c p' > Utils.squaredDistanceTwoPoints
c p
72 yield sign
delta = sign
delta' && Utils.squaredDistanceTwoPoints c p' < Utils.squaredDistanceTwoPoints
c p
76 elif
e.State = CellState.Peculiar // A peculiar always win against a RBC.
80 yield not
<| e'.Contains p.X p.Y
84 let ellipses = ellipses |> List.map EllipseFlaggedKd
86 // 1) Associate touching ellipses with each ellipses and remove ellipse with more than two intersections.
87 let tree = KdTree.Tree.BuildTree ellipses
88 let neighbors (e: EllipseFlaggedKd) : (EllipseFlaggedKd * PointF * PointF) list =
89 if e.State <> CellState.Removed
91 tree.Search (searchRegion e)
92 // We only keep the ellipses touching 'e'.
93 |> List.choose (fun otherE ->
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]))
109 let ellipsesWithNeigbors = ellipses |> List.map (fun e -> e, neighbors e)
111 // 2) Remove ellipses touching the edges.
112 let widthF, heightF = float32 width, float32 height
114 if e.isOutside widthF heightF then e.State <- CellState.Removed
116 // 3) Remove ellipses with a high standard deviation (high contrast).
117 // Obsolete. It was useful when the ellipses result quality wasn't
good.
118 (* let imgData = img.Data
119 let globalStdDeviation = MathNet.Numerics.Statistics.Statistics.PopulationStandardDeviation(seq {
120 for y in 0 .. h - 1 do
121 for x in 0 .. w - 1 do
122 yield float imgData.[y, x, 0] })
127 let shrinkedE = e.Scale 0.9f
128 let minX, minY, maxX, maxY = ellipseWindow shrinkedE
130 let stdDeviation = MathNet.Numerics.Statistics.Statistics.StandardDeviation (seq {
131 for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
132 for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
133 if shrinkedE.Contains (float32 x) (float32 y)
135 yield 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
145 let minX, minY, maxX, maxY = ellipseWindow e
148 for y
in (if minY < 0 then 0 else minY) .. (if maxY >= height
then height
- 1 else maxY) do
149 for x
in (if minX < 0 then 0 else minX) .. (if maxX >= width
then width
- 1 else maxX) do
150 let p = PointF(float32 x
, float32 y
)
151 if pixelOwnedByE p e neighbors
155 if area < int minArea
157 e.State <- CellState.Removed
159 // 5) Define non-rbc (peculiar) cells.
160 let darkStainData = parasites
.darkStain
.Data
162 |> List.choose
(fun (e, neighbors) ->
163 if e.State = CellState.Removed
167 let mutable darkStainPixels = 0
168 let mutable nbElement = 0
169 let minX, minY, maxX, maxY = ellipseWindow e
170 for y
in minY .. maxY do
171 for x
in minX .. maxX do
172 let p = PointF(float32 x
, float32 y
)
173 if pixelOwnedByE p e neighbors
175 nbElement <- nbElement + 1
176 if darkStainData.[y
, x
, 0] > 0uy
178 darkStainPixels <- darkStainPixels + 1
180 if float darkStainPixels > config
.Parameters.maxDarkStainRatio
* (float nbElement) then Some e else None)
181 |> List.iter
(fun e -> e.State <- CellState.Peculiar)
183 // 5) Define pixels associated to each ellipse and create the cells.
184 let diameterParasiteSquared = (2.f
* config
.RBCRadius.ParasiteRadius) ** 2.f
|> roundInt
185 let minimumParasiteArea = config
.RBCRadius.MinimumParasiteArea |> roundInt
187 let nucleusData = parasites
.nucleus
.Copy().Data // Will be modified thus the copy.
188 let parasiteData = parasites
.parasite
.Data
189 let darkStainData = parasites
.darkStain
.Data
192 |> List.choose
(fun (e, neighbors) ->
193 if e.State = CellState.Removed
197 let minX, minY, maxX, maxY = ellipseWindow e
199 let nucleusPixels = List<Point>()
200 let parasitePixels = List<Point>()
202 let mutable nbElement = 0
204 let elements = new Matrix<byte
>(maxY - minY + 1, maxX - minX + 1)
205 for y
in minY .. maxY do
206 for x
in minX .. maxX do
207 let p = PointF(float32 x
, float32 y
)
208 if pixelOwnedByE p e neighbors
210 elements.[y
- minY, x
- minX] <- 1uy
211 nbElement <- nbElement + 1
213 if nucleusData.[y
, x
, 0] > 0uy
215 nucleusPixels.Add(Point(x
, y
))
217 if parasiteData.[y
, x
, 0] > 0uy
219 parasitePixels.Add(Point(x
, y
))
222 if nucleusPixels.Count > 0
225 for parasitePixel
in parasitePixels do
226 if nucleusPixels.Exists(fun p -> pown
(p.X - parasitePixel
.X) 2 + pown
(p.Y - parasitePixel
.Y) 2 <= diameterParasiteSquared)
227 then yield
1 } |> Seq.sum
232 if e.State = CellState.Peculiar
236 elif
nucleusPixels.Count > 0 && parasiteArea >= minimumParasiteArea
238 let infectionToRemove = Morpho.connectedComponents
parasites.parasite
nucleusPixels
239 for p in infectionToRemove do
240 nucleusData.[p.Y, p.X, 0] <- 0uy
246 Some { cellClass = cellClass
247 center
= Point(roundInt
e.Cx, roundInt
e.Cy)
248 nucleusArea
= if cellClass = InfectedRBC then nucleusPixels.Count else 0
249 parasiteArea = parasiteArea
250 elements = elements })