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) (img
: Image<Gray, float32
>) (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
.RBCMaxRadius)
33 KdTree.maxX
= e
.Cx + (e
.A + config
.RBCMaxRadius)
34 KdTree.minY
= e
.Cy - (e
.A + config
.RBCMaxRadius)
35 KdTree.maxY
= e
.Cy + (e
.A + config
.RBCMaxRadius) }
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.5f)
41 cx - a, cy
- a, cx + a, cy
+ a
48 // Return 'true' if the point 'p' is owned by e.
49 // The lines represents all intersections with other ellipses.
50 let pixelOwnedByE (p
: PointD) (e: Ellipse) (others
: (Ellipse * Line) list) =
53 let c = PointD(e.Cx, e.Cy)
54 for e', d1 in others do
55 let d2 = Utils.lineFromTwoPoints c p
56 let c' = PointD(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'
61 let p' = Utils.pointFromTwoLines
d1 d2
62 // Yield 'false' when the point is owned by another ellipse.
65 yield sign
(c.X - p.X) <> sign
(c.X - p'.X) || Utils.squaredDistanceTwoPoints c p' > Utils.squaredDistanceTwoPoints
c p
67 yield sign
(c.X - p.X) = sign
(c.X - p'.X) && Utils.squaredDistanceTwoPoints c p' < Utils.squaredDistanceTwoPoints
c p
72 let ellipses = ellipses |> List.map
EllipseFlaggedKd
74 // 1) Associate touching ellipses with each ellipses and remove ellipse with more than two intersections.
75 let tree = KdTree.Tree.BuildTree ellipses
76 let neighbors (e: EllipseFlaggedKd) : (EllipseFlaggedKd * PointD * PointD) list =
79 tree.Search (searchRegion e)
80 // We only keep the ellipses touching 'e'.
81 |> List.choose
(fun otherE
->
84 match EEOver.EEOverlapArea e otherE with
85 | Some (_
, px
, _
) when px
.Length > 2 ->
86 otherE.Removed <- true
88 | Some (area
, px
, py
) when area
> 0.f
&& px
.Length = 2 ->
89 Some (otherE, PointD(px
.[0], py
.[0]), PointD(px
.[1], py
.[1]))
97 // We reverse the list to get the lower score ellipses first.
98 let ellipsesWithNeigbors = ellipses |> List.map
(fun e -> e, neighbors e) |> List.rev
101 // 2) Remove ellipses touching the edges.
103 if e.isOutside
w_f h_f then e.Removed <- true
105 // 3) Remove ellipses with a high standard deviation (high contrast).
106 let imgData = img
.Data
107 let globalStdDeviation = MathNet.Numerics.Statistics.Statistics.PopulationStandardDeviation(seq
{
108 for y
in 0 .. h - 1 do
109 for x
in 0 .. w - 1 do
110 yield
float imgData.[y
, x
, 0] })
115 let shrinkedE = e.Scale 0.9f
116 let minX, minY
, maxX
, maxY
= ellipseWindow shrinkedE
118 let stdDeviation = MathNet.Numerics.Statistics.Statistics.StandardDeviation (seq
{
119 for y
in (if minY
< 0 then 0 else minY) .. (if maxY
>= h then h - 1 else maxY) do
120 for x
in (if minX < 0 then 0 else minX) .. (if maxX
>= w then w - 1 else maxX) do
121 if shrinkedE.Contains (float32 x
) (float32 y
)
123 yield
float imgData.[y
, x
, 0] })
125 if stdDeviation > globalStdDeviation * config
.Parameters.standardDeviationMaxRatio
then
129 let imgData = img.Data
130 let stdDeviations = [
134 let shrinkedE = e.Scale 0.9f
135 let minX, minY, maxX, maxY = ellipseWindow shrinkedE
137 let stdDeviation = float32 <| MathNet.Numerics.Statistics.Statistics.StandardDeviation (seq {
138 for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
139 for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
140 if shrinkedE.Contains (float32 x) (float32 y)
142 yield float imgData.[y, x, 0] })
144 e.StdDeviation <- stdDeviation
147 // We use Otsu and eliminate some cells only if the curve may be bimodal.
148 // See https://en.wikipedia.org/wiki/Multimodal_distribution#Bimodality_coefficient
149 let skewness, kurtosis = MathNet.Numerics.Statistics.Statistics.PopulationSkewnessKurtosis (stdDeviations |> List.map float)
150 let n = float stdDeviations.Length
151 let bimodalityCoefficient = (skewness ** 2. + 1.) / (kurtosis + 3. * (n - 1.) ** 2. / ((n - 2.) * (n - 3.)))
153 if bimodalityCoefficient > 5. / 9.
155 let hist = ImgTools.histogram stdDeviations 200
156 let thresh, _, _ = ImgTools.otsu hist
158 if not e.Removed && e.StdDeviation > thresh
159 then e.Removed <- true
162 // 4) Remove ellipses with little area.
163 let minArea = config
.RBCMinArea
164 for e, neighbors in ellipsesWithNeigbors do
167 let minX, minY, maxX, maxY = ellipseWindow e
170 for y
in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
171 for x
in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
172 let p = PointD(float32 x
, float32 y
)
173 if pixelOwnedByE p e (neighbors |> List.choose
(fun (otherE, p1
, p2
) -> if otherE.Removed then None else Some (otherE :> Ellipse, Utils.lineFromTwoPoints
p1 p2
)))
177 if area < int minArea
181 // 5) Define pixels associated to each ellipse and create the cells.
183 |> List.choose
(fun (e, neighbors) ->
188 let minX, minY, maxX, maxY = ellipseWindow e
190 let infectedPixels = List<Point>()
191 let mutable stainPixels = 0
192 let mutable darkStainPixels = 0
193 let mutable nbElement = 0
195 let elements = new Matrix<byte
>(maxY - minY + 1, maxX - minX + 1)
196 for y
in minY .. maxY do
197 for x
in minX .. maxX do
198 let p = PointD(float32 x
, float32 y
)
199 if pixelOwnedByE p e (neighbors |> List.choose
(fun (otherE, p1, p2
) -> if otherE.Removed then None else Some (otherE :> Ellipse, Utils.lineFromTwoPoints
p1 p2
)))
201 elements.[y
-minY, x
-minX] <- 1uy
202 nbElement <- nbElement + 1
204 if infection.Data.[y
, x
, 0] > 0uy
206 infectedPixels.Add(Point(x
, y
))
208 if parasites
.stain
.Data.[y
, x
, 0] > 0uy
210 stainPixels <- stainPixels + 1
212 if parasites
.darkStain
.Data.[y
, x
, 0] > 0uy
214 darkStainPixels <- darkStainPixels + 1
217 if float darkStainPixels > config
.Parameters.maxDarkStainRatio
* (float nbElement) ||
218 float stainPixels > config
.Parameters.maxStainRatio
* (float nbElement)
221 elif
infectedPixels.Count >= 1
223 let infectionToRemove = ImgTools.connectedComponents
parasites.stain
infectedPixels
224 for p in infectionToRemove do
225 infection.Data.[p.Y, p.X, 0] <- 0uy
230 Some { cellClass = cellClass
231 center
= Point(roundInt
e.Cx, roundInt
e.Cy)
232 stainArea
= stainPixels
233 elements = elements })