1
module ParasitemiaCore.Ellipse
4 open System.Collections.Generic
7 open MathNet.Numerics.LinearAlgebra
10 open Emgu.CV.Structure
17 // This is a ratio of the biggest radius.
18 let minimumDistanceBetweenDrawnPoints = 0.6
21 /// Try to build an ellipse from three points and two tangents passing by the first and the second point.
22 /// 'Ellipse.A' is always equal or greater than Ellipse.B.
23 /// 'Ellipse.Alpha' is between 0 and Pi.
25 let ellipse (p1x
: float) (p1y
: float) (m1
: float) (p2x
: float) (p2y
: float) (m2
: float) (p3x
: float) (p3y
: float) : Types.Ellipse option =
26 let p0 = pointFromTwoLines
(Types.Line(float32 m1
, float32
(p1y
- m1
* p1x
))) (Types.Line(float32 m2
, float32
(p2y
- m2
* p2x
)))
27 let p0x, p0y
= float p0.X, float p0.Y
29 let s = matrix
[[ 1.; 0.; 0. ]
33 let v0 = matrix
[[ 1.; p0x; p0y
]]
34 let v1 = matrix
[[ 1.; p1x
; p1y
]]
35 let v2 = matrix
[[ 1.; p2x
; p2y
]]
36 let v3 = matrix
[[ 1.; p3x
; p3y
]]
38 let p = (v3.Stack(v1).Stack(v2).Determinant() * v0).Stack(v0.Stack(v3).Stack(v2).Determinant() * v1).Stack(v0.Stack(v1).Stack(v3).Determinant() * v2).Transpose()
39 let conicMat = p * s.Inverse() * p.Transpose()
40 let a = conicMat.[0, 0]
41 let b = conicMat.[0, 1]
42 let c = conicMat.[1, 1]
43 let d = conicMat.[0, 2]
44 let e = conicMat.[1, 2]
45 let f = conicMat.[2, 2]
51 let at = c * f - e ** 2. + (e * d - b * f) * cx + (b * e - c * d) * cy
56 let q = (-1. / at) * (matrix
[[ a * f - d ** 2.0; b * d - a * e ]; [ b * d - a * e; a * c - b ** 2.0 ]])
58 let eigenValues = eigen.EigenValues
59 let lambda = eigenValues.[1].Real
60 let mu = eigenValues.[0].Real
62 if lambda <= 0. || mu <= 0.
66 let r1, r2
= 1. / (sqrt
lambda), 1. / (sqrt
mu)
68 let eigenVectors = eigen.EigenVectors
69 let v_a = eigenVectors.[0, 0]
70 let v_b = eigenVectors.[1, 0] // [0, 1]
72 // Angle against the longest axis.
73 let phi = (if r2
> r1 then atan
(v_b / v_a) else atan (v_a / v_b))
75 let phi' = if phi < 0. then phi + Math.PI else phi
76 let majorAxis, minorAxis = if r1 > r2 then r1, r2 else r2, r1
78 Some (Types.Ellipse(float32 cx, float32 cy, float32 majorAxis, float32 minorAxis, float32 phi'))
81 let private vectorRotation
(p1x
: float32
) (p1y
: float32
) (v1x
: float32
) (v1y
: float32
) (px
: float32
) (py
: float32
) : float32
=
84 if v1x
> 0.f then -1.f else 1.f
87 if v1x
< 0.f then -1.f else 1.f
90 if v1y
< 0.f then -1.f else 1.f
92 if v1y
> 0.f then -1.f else 1.f
94 let private areVectorsValid
(p1x
: float32
) (p1y
: float32
) (p2x
: float32
) (p2y
: float32
) (v1x
: float32
) (v1y
: float32
) (v2x
: float32
) (v2y
: float32
) : (float32
* float32
) option =
98 let b1 = -m1 * p1x
+ p1y
99 let b2 = -m2 * p2x
+ p2y
100 let px = -((b1 - b2) / (m1 - m2))
101 let py = -((m2 * b1 - m1 * b2) / (m1 - m2))
103 let rot1 = vectorRotation p1x p1y v1x v1y
px py
104 let rot2 = vectorRotation p2x p2y v2x v2y
px py
110 let alpha1 = atan2
(p1y
- py) (p1x
- px)
111 let alpha2 = atan2
(p2y
- py) (p2x
- px)
113 let alpha1' = if alpha1 < 0.f then 2.f * PI + alpha1 else alpha1
114 let alpha2' = if alpha2 < 0.f then 2.f * PI + alpha2 else alpha2
116 let diff = rot1 * alpha1' + rot2 * alpha2'
118 if diff > PI || (diff < 0.f && diff > -PI)
124 let find (edges
: Matrix<byte
>)
125 (xGradient
: Matrix<float32
>)
126 (yGradient
: Matrix<float32
>)
127 (config
: Config) : MatchingEllipses =
129 let r1, r2 = config
.RBCRadius.Min, config
.RBCRadius.Max
130 let incrementWindowDivisor = 4.f
132 // We choose a window size for which the biggest ellipse can always be fitted in.
133 let windowSize = roundInt
(2.f * r2)
134 let factorNbPick = config
.Parameters.factorNbPick
136 let increment = windowSize / (int incrementWindowDivisor)
138 let radiusTolerance = (r2 - r1) * 0.2f
140 let squaredMinimumDistance = (float config.RBCRadius.Pixel * minimumDistanceBetweenDrawnPoints) ** 2.
141 let inline squaredDistance
x1 y1 x2 y2
= (x1 - x2
) ** 2. + (y1
- y2
) ** 2.
148 let mutable last_i, last_j
= Int32.MaxValue, Int32.MaxValue
150 let currentElements = List<Point>()
152 let edgesData = edges
.Data
153 let xDirData = xGradient
.Data
154 let yDirData = yGradient
.Data
158 let ellipses = MatchingEllipses(config.RBCRadius.Pixel)
160 for window_i
in -windowSize + increment .. increment .. h - increment do
161 for window_j
in -windowSize + increment .. increment .. w - increment do
163 let window_i_begin = if window_i
< 0 then 0 else window_i
164 let window_i_end = if window_i + windowSize - 1 >= h then h - 1 else window_i + windowSize - 1
165 let window_j_begin = if window_j
< 0 then 0 else window_j
166 let window_j_end = if window_j + windowSize - 1 >= w then w - 1 else window_j + windowSize - 1
168 // Remove old elements.
169 let indexFirstElement = currentElements.FindIndex(fun p -> p.X >= window_j_begin)
170 if indexFirstElement > 0
171 then currentElements.RemoveRange(0, indexFirstElement)
173 // Add the new elements.
174 let newElemsBegin_j = window_j + windowSize - increment
175 let newElemsEnd_j = window_j + windowSize - 1
176 for j
in (if newElemsBegin_j < 0 then 0 else newElemsBegin_j) .. (if newElemsEnd_j >= w then w - 1 else newElemsEnd_j) do
177 for i
in window_i_begin .. window_i_end do
178 if edgesData.[i
, j
] = 1uy
179 then currentElements.Add(Point(j
, i
))
181 if currentElements.Count >= 10
183 let mutable nbOfPicks = (float currentElements.Count) * factorNbPick |> int
184 while nbOfPicks > 0 do
185 let p1 = currentElements.[rng.Next(currentElements.Count)]
186 let p2 = currentElements.[rng.Next(currentElements.Count)]
187 let p3 = currentElements.[rng.Next(currentElements.Count)]
188 if p1 <> p2 && p1 <> p3 && p2 <> p3
190 nbOfPicks <- nbOfPicks - 1
191 let p1yf, p1xf
= float p1.Y, float p1.X
192 let p2yf, p2xf
= float p2.Y, float p2.X
193 let p3yf, p3xf
= float p3.Y, float p3.X
194 if squaredDistance
p1xf p1yf p2xf
p2yf >= squaredMinimumDistance &&
195 squaredDistance
p1xf p1yf p3xf
p3yf >= squaredMinimumDistance &&
196 squaredDistance
p2xf p2yf p3xf
p3yf >= squaredMinimumDistance
198 match areVectorsValid
(float32
p1xf) (float32
p1yf) (float32
p2xf) (float32
p2yf) -xDirData.[p1.Y, p1.X] -yDirData.[p1.Y, p1.X] -xDirData.[p2.Y, p2.X] -yDirData.[p2.Y, p2.X] with
200 match ellipse p1xf p1yf (float m1) p2xf p2yf (float m2) p3xf
p3yf with
201 | Some e when e.Cx > 0.f && e.Cx < w_f - 1.f && e.Cy > 0.f && e.Cy < h_f - 1.f &&
202 e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance ->
207 currentElements.Clear()