c8d44b7303231fde3d7e784082d594713dd0a1bb
4 open System.Collections.Generic
15 type private SearchExtremum = Minimum | Maximum
17 let private goldenSectionSearch
(f
: float -> float) (nbIter
: int) (xmin
: float) (xmax
: float) (searchExtremum
: SearchExtremum) : (float * float) =
18 let gr = 1. / 1.6180339887498948482
21 let mutable c = b - gr * (b - a)
22 let mutable d = a + gr * (b - a)
24 for i
in 1 .. nbIter
do
28 if searchExtremum
= Maximum
47 // Ellipse.A is always equal or greater than Ellipse.B.
48 // Ellipse.Alpha is between 0 and Pi.
49 let ellipse (p1x
: float) (p1y
: float) (m1
: float) (p2x
: float) (p2y
: float) (m2
: float) (p3x
: float) (p3y
: float) : Types.Ellipse option =
50 let accuracy_extremum_search_1 = 10 // 3
51 let accuracy_extremum_search_2 = 10 // 4
53 // p3 as the referencial.
60 // Convert to polar coordinates.
64 let r1 = sqrt
(p1x ** 2. + p1y ** 2.)
65 let theta1 = atan2
p1y p1x
67 let r2 = sqrt
(p2x ** 2. + p2y ** 2.)
68 let theta2 = atan2
p2y p2x
71 4. * sin
(alpha1 - theta1) * (-r1 * sin
(alpha1 - theta1) + r2 * sin
(alpha1 - theta2)) *
72 sin
(alpha2 - theta2) * (-r1 * sin
(alpha2 - theta1) + r2 * sin
(alpha2 - theta2)) +
73 r1 * r2 * sin
(alpha1 - alpha2) ** 2. * sin
(theta1 - theta2) ** 2. < 0.
78 (r1 * r2 * (r1 * (cos
(alpha2 + theta
- theta1 - theta2) - cos
(alpha2 - theta
) * cos
(theta1 - theta2)) * sin
(alpha1 - theta1) + r2 * (-cos
(alpha1 + theta
- theta1 - theta2) + cos
(alpha1 - theta
) * cos
(theta1 - theta2)) * sin
(alpha2 - theta2)) * sin
(theta1 - theta2)) /
79 (sin
(alpha1 - theta1) * sin
(alpha2 - theta2) * (r1 * sin
(theta
- theta1) - r2 * sin
(theta
- theta2)) ** 2. - r1 * r2 * sin
(alpha1 - theta
) * sin
(alpha2 - theta
) * sin
(theta1 - theta2) ** 2.)
83 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
84 let thetaTan, _
= goldenSectionSearch
rabs accuracy_extremum_search_1 0. Math.PI Maximum
87 let PTanx = rTan * cos
thetaTan
88 let PTany = rTan * sin
thetaTan
91 let d1b = -d1a * p1x + p1y
94 let d2b = -d2a * p2x + p2y
96 let d3a = -1. / tan
thetaTan
97 let d3b = -d3a * PTanx + PTany
99 let Ux = -(d1b - d2b) / (d1a - d2a)
100 let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
102 let Vx = -(d1b - d3b) / (d1a - d3a)
103 let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
105 let Wx = p1x + (p2x - p1x) / 2.
106 let Wy = p1y + (p2y - p1y) / 2.
108 let Zx = p1x + (PTanx - p1x) / 2.
109 let Zy = p1y + (PTany - p1y) / 2.
111 let va = -(-Vy + Zy) / (Vx - Zx)
112 let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
114 let ua = -(-Uy + Wy) / (Ux - Wx)
115 let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
117 let cx = -(vb - ub) / (va - ua)
118 let cy = -(ua * vb - va * ub) / (va - ua)
120 let rc = sqrt
(cx ** 2. + cy ** 2.)
121 let psi = atan2
cy cx
125 rc ** 2. + (r1 ** 2. * r2 ** 2. * (r1 * (cos
(alpha2 + theta
- theta1 - theta2) - cos
(alpha2 - theta
) * cos
(theta1 - theta2)) * sin
(alpha1 - theta1) + r2 * (-cos
(alpha1 + theta
- theta1 - theta2) + cos
(alpha1 - theta
) * cos
(theta1 - theta2)) * sin
(alpha2 - theta2)) ** 2. * sin
(theta1 - theta2) ** 2.) /
126 (sin
(alpha1 - theta1) * sin
(alpha2 - theta2) * (r1 * sin
(theta
- theta1) - r2 * sin
(theta
- theta2)) ** 2. - r1 * r2 * sin
(alpha1 - theta
) * sin
(alpha2 - theta
) * sin
(theta1 - theta2) ** 2.) ** 2. -
127 (2. * r1 * r2 * rc * cos
(theta
- psi) * (r1 * (cos
(alpha2 + theta
- theta1 - theta2) - cos
(alpha2 - theta
) * cos
(theta1 - theta2)) * sin
(alpha1 - theta1) + r2 * (-cos
(alpha1 + theta
- theta1 - theta2) + cos
(alpha1 - theta
) * cos
(theta1 - theta2)) * sin
(alpha2 - theta2)) * sin
(theta1 - theta2)) /
128 (sin
(alpha1 - theta1) * sin
(alpha2 - theta2) * (r1 * sin
(theta
- theta1) - r2 * sin
(theta
- theta2)) ** 2. - r1 * r2 * sin
(alpha1 - theta
) * sin
(alpha2 - theta
) * sin
(theta1 - theta2) ** 2.))
130 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
131 let r1eTheta, r1e
= goldenSectionSearch
rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Maximum // Pi/2 and not pi because the period is Pi.
132 let r2eTheta, r2e
= goldenSectionSearch
rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Minimum
134 let rr1e = r r1eTheta
135 let r1ex = rr1e * cos
r1eTheta
136 let r1ey = rr1e * sin
r1eTheta
137 let mutable alpha = atan
((r1ey - cy) / (r1ex - cx))
140 alpha <- alpha + Math.PI
142 // Ride off the p3 referential.
146 Some (Types.Ellipse(float32
cx, float32
cy, float32 r1e
, float32 r2e
, float32
alpha))
151 let private vectorRotation
(p1x: float32
) (p1y: float32
) (v1x
: float32
) (v1y
: float32
) (px
: float32
) (py
: float32
) : float32
=
152 let mutable rotation = 1.f
176 let private areVectorsValid
(p1x: float32
) (p1y: float32
) (p2x: float32
) (p2y: float32
) (v1x
: float32
) (v1y
: float32
) (v2x
: float32
) (v2y
: float32
) : (float32
* float32
) option =
180 let b1 = -m1 * p1x + p1y
181 let b2 = -m2 * p2x + p2y
182 let px = -((b1 - b2) / (m1 - m2))
183 let py = -((m2 * b1 - m1 * b2) / (m1 - m2))
185 let rot1 = vectorRotation
p1x p1y v1x v1y
px py
186 let rot2 = vectorRotation
p2x p2y v2x v2y
px py
192 let alpha1 = atan2
(p1y - py) (p1x - px)
193 let alpha2 = atan2
(p2y - py) (p2x - px)
195 let alpha1' = if alpha1 < 0.f then 2.f * PI + alpha1 else alpha1
196 let alpha2' = if alpha2 < 0.f
then 2.f
* PI + alpha2 else alpha2
198 let diff = rot1 * alpha1' + rot2 * alpha2'
200 if diff > PI || (diff < 0.f
&& diff > -PI)
207 let find (edges
: Matrix<byte
>)
208 (xGradient
: Image<Gray, float32
>)
209 (yGradient
: Image<Gray, float32
>)
210 (config
: Config) : MatchingEllipses =
212 let r1, r2 = config
.RBCMinRadius, config
.RBCMaxRadius
213 let incrementWindowDivisor = 4.f
215 // We choose a window size for which the biggest ellipse can always be fitted in.
216 let windowSize = roundInt
(2.f
* r2 / (incrementWindowDivisor - 1.f
) * incrementWindowDivisor)
217 let factorNbPick = config
.Parameters.factorNbPick
219 let increment = windowSize / (int incrementWindowDivisor)
221 let radiusTolerance = (r2 - r1) * 0.2f
223 let squaredMinimumDistance = (float r2 / 1.5) ** 2.
224 let inline squaredDistance
x1 y1 x2 y2
= (x1 - x2
) ** 2. + (y1
- y2
) ** 2.
231 let mutable last_i, last_j
= Int32.MaxValue, Int32.MaxValue
233 let currentElements = List<Point>()
235 let edgesData = edges
.Data
236 let xDirData = xGradient
.Data
237 let yDirData = yGradient
.Data
241 let ellipses = MatchingEllipses(r1)
243 for window_i
in -windowSize + increment .. increment .. h - increment do
244 for window_j
in -windowSize + increment .. increment .. w - increment do
246 let window_i_begin = if window_i
< 0 then 0 else window_i
247 let window_i_end = if window_i + windowSize - 1 >= h then h - 1 else window_i + windowSize - 1
248 let window_j_begin = if window_j
< 0 then 0 else window_j
249 let window_j_end = if window_j + windowSize - 1 >= w then w - 1 else window_j + windowSize - 1
251 // Remove old elements.
252 let indexFirstElement = currentElements.FindIndex(fun p
-> p
.X >= window_j_begin)
253 if indexFirstElement > 0
254 then currentElements.RemoveRange(0, indexFirstElement)
256 // Add the new elements.
257 let newElemsBegin_j = window_j + windowSize - increment
258 let newElemsEnd_j = window_j + windowSize - 1
259 for j
in (if newElemsBegin_j < 0 then 0 else newElemsBegin_j) .. (if newElemsEnd_j >= w then w - 1 else newElemsEnd_j) do
260 for i
in window_i_begin .. window_i_end do
261 if edgesData.[i
, j
] = 1uy
262 then currentElements.Add(Point(j
, i
))
264 if currentElements.Count >= 10
266 let mutable nbOfPicks = (float currentElements.Count) * factorNbPick |> int
267 while nbOfPicks > 0 do
268 let p1 = currentElements.[rng.Next(currentElements.Count)]
269 let p2 = currentElements.[rng.Next(currentElements.Count)]
270 let p3 = currentElements.[rng.Next(currentElements.Count)]
271 if p1 <> p2 && p1 <> p3 && p2 <> p3
273 nbOfPicks <- nbOfPicks - 1
274 let p1yf, p1xf
= float p1.Y, float p1.X
275 let p2yf, p2xf
= float p2.Y, float p2.X
276 let p3yf, p3xf
= float p3.Y, float p3.X
277 if squaredDistance
p1xf p1yf p2xf
p2yf >= squaredMinimumDistance &&
278 squaredDistance
p1xf p1yf p3xf
p3yf >= squaredMinimumDistance &&
279 squaredDistance
p2xf p2yf p3xf
p3yf >= squaredMinimumDistance
281 match areVectorsValid
(float32
p1xf) (float32
p1yf) (float32
p2xf) (float32
p2yf) -xDirData.[p1.Y, p1.X, 0] -yDirData.[p1.Y, p1.X, 0] -xDirData.[p2.Y, p2.X, 0] -yDirData.[p2.Y, p2.X, 0] with
283 match ellipse p1xf p1yf (float m1) p2xf p2yf (float m2) p3xf
p3yf with
284 | Some e
when e
.Cx > 0.f
&& e
.Cx < w_f - 1.f
&& e
.Cy > 0.f
&& e
.Cy < h_f - 1.f
&&
285 e
.A >= r1 - radiusTolerance && e
.A <= r2 + radiusTolerance && e
.B >= r1 - radiusTolerance && e
.B <= r2 + radiusTolerance ->
290 currentElements.Clear()