4 open System.Collections.Generic
14 type private SearchExtremum = Minimum | Maximum
16 let private goldenSectionSearch
(f
: float -> float) (nbIter
: int) (xmin
: float) (xmax
: float) (searchExtremum
: SearchExtremum) : (float * float) =
17 let gr = 1.0 / 1.6180339887498948482
20 let mutable c = b - gr * (b - a)
21 let mutable d = a + gr * (b - a)
23 for i
in 1 .. nbIter
do
27 if searchExtremum
= Maximum
46 // Ellipse.A is always equal or greater than Ellipse.B.
47 // Ellipse.Alpha is between 0 and Pi.
48 let ellipse (p1x
: float) (p1y
: float) (m1
: float) (p2x
: float) (p2y
: float) (m2
: float) (p3x
: float) (p3y
: float) : Types.Ellipse option =
49 let accuracy_extremum_search_1 = 4
50 let accuracy_extremum_search_2 = 3
52 // p3 as the referencial.
59 // Convert to polar coordinates.
63 let r1 = sqrt
(p1x ** 2.0 + p1y ** 2.0)
64 let theta1 = atan2
p1y p1x
66 let r2 = sqrt
(p2x**2.0 + p2y**2.0)
67 let theta2 = atan2
p2y p2x
70 4.0 * sin
(alpha1 - theta1) * (-r1 * sin
(alpha1 - theta1) + r2 * sin
(alpha1 - theta2)) *
71 sin
(alpha2 - theta2) * (-r1 * sin
(alpha2 - theta1) + r2 * sin
(alpha2 - theta2)) +
72 r1 * r2 * sin
(alpha1 - alpha2) ** 2.0 * sin
(theta1 - theta2) ** 2.0 < 0.0
77 (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)) /
78 (sin
(alpha1 - theta1) * sin
(alpha2 - theta2) * (r1 * sin
(theta
- theta1) - r2 * sin
(theta
- theta2)) ** 2.0 - r1 * r2 * sin
(alpha1 - theta
) * sin
(alpha2 - theta
) * sin
(theta1 - theta2) ** 2.0)
82 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
83 let thetaTan, _
= goldenSectionSearch
rabs accuracy_extremum_search_1 0.0 Math.PI Maximum
86 let PTanx = rTan * cos
thetaTan
87 let PTany = rTan * sin
thetaTan
90 let d1b = -d1a * p1x + p1y
93 let d2b = -d2a * p2x + p2y
95 let d3a = -1.0 / tan
thetaTan
96 let d3b = -d3a * PTanx + PTany
98 let Ux = -(d1b - d2b) / (d1a - d2a)
99 let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
101 let Vx = -(d1b - d3b) / (d1a - d3a)
102 let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
104 let Wx = p1x + (p2x - p1x) / 2.0
105 let Wy = p1y + (p2y - p1y) / 2.0
107 let Zx = p1x + (PTanx - p1x) / 2.0
108 let Zy = p1y + (PTany - p1y) / 2.0
110 let va = -(-Vy + Zy) / (Vx - Zx)
111 let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
113 let ua = -(-Uy + Wy) / (Ux - Wx)
114 let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
116 let cx = -(vb - ub) / (va - ua)
117 let cy = -(ua * vb - va * ub) / (va - ua)
119 let rc = sqrt
(cx**2.0 + cy**2.0)
120 let psi = atan2
cy cx
124 rc ** 2.0 + (r1 ** 2.0 * r2 ** 2.0 * (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.0 * sin
(theta1 - theta2) ** 2.0) /
125 (sin
(alpha1 - theta1) * sin
(alpha2 - theta2) * (r1 * sin
(theta
- theta1) - r2 * sin
(theta
- theta2)) ** 2.0 - r1 * r2 * sin
(alpha1 - theta
) * sin
(alpha2 - theta
) * sin
(theta1 - theta2) ** 2.0) ** 2.0 -
126 (2.0 * 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)) /
127 (sin
(alpha1 - theta1) * sin
(alpha2 - theta2) * (r1 * sin
(theta
- theta1) - r2 * sin
(theta
- theta2)) ** 2.0 - r1 * r2 * sin
(alpha1 - theta
) * sin
(alpha2 - theta
) * sin
(theta1 - theta2) ** 2.0))
129 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
130 let r1eTheta, r1e
= goldenSectionSearch
rellipse accuracy_extremum_search_2 0.0 (Math.PI / 2.0) Maximum // Pi/2 and not pi because the period is Pi.
131 let r2eTheta, r2e
= goldenSectionSearch
rellipse accuracy_extremum_search_2 0.0 (Math.PI / 2.0) Minimum
133 let rr1e = r r1eTheta
134 let r1ex = rr1e * cos
r1eTheta
135 let r1ey = rr1e * sin
r1eTheta
136 let mutable alpha = atan
((r1ey - cy) / (r1ex - cx))
139 alpha <- alpha + Math.PI
141 // Ride off the p3 referential.
145 Some (Types.Ellipse(cx, cy, r1e
, r2e
, alpha))
150 let private vectorRotation
(p1x: float) (p1y: float) (v1x
: float) (v1y
: float) (px
: float) (py
: float) : float =
151 let mutable rotation = 1.0
175 let private areVectorsValid
(p1x: float) (p1y: float) (p2x: float) (p2y: float) (v1x
: float) (v1y
: float) (v2x
: float) (v2y
: float) : (float * float) option =
179 let b1 = -m1 * p1x + p1y
180 let b2 = -m2 * p2x + p2y
181 let px = -((b1 - b2)/(m1 - m2))
182 let py = -((m2 * b1 - m1 * b2)/(m1 - m2))
184 let rot1 = vectorRotation
p1x p1y v1x v1y
px py
185 let rot2 = vectorRotation
p2x p2y v2x v2y
px py
187 if rot1 = rot2 || rot1 * atan2
(p1y - py) (p1x - px) + rot2 * atan2
(p2y - py) (p2x - px) <= 0.0
193 let find (edges
: Matrix<byte
>)
194 (xDir
: Image<Gray, float>)
195 (yDir
: Image<Gray, float>)
196 (config
: Config) : MatchingEllipses =
198 let r1, r2 = config
.scale
* config
.minRBCSize
, config
.scale
* config
.maxRBCSize
199 let windowSize = roundInt
(config
.factorWindowSize
* r2)
200 let factorNbPick = config
.factorNbPick
202 let increment = windowSize / 4
204 let radiusTolerance = (r2 - r1) * 0.2
206 let minimumDistance = (r2 / 1.5) ** 2.0
207 let squaredDistance x1 y1 x2 y2
= (x1
- x2
) ** 2.0 + (y1
- y2
) ** 2.0
212 let mutable last_i, last_j
= Int32.MaxValue, Int32.MaxValue
214 let currentElements = List<(int * int)>()
216 let edgesData = edges
.Data
217 let xDirData = xDir
.Data
218 let yDirData = yDir
.Data
222 let ellipses = MatchingEllipses(r1)
224 for window_i
in -windowSize + increment .. increment .. h - increment do
225 for window_j
in -windowSize + increment .. increment .. w - increment do
227 let window_i_begin = if window_i
< 0 then 0 else window_i
228 let window_i_end = if window_i + windowSize - 1 >= h then h - 1 else window_i + windowSize - 1
229 let window_j_begin = if window_j
< 0 then 0 else window_j
230 let window_j_end = if window_j + windowSize - 1 >= w then w - 1 else window_j + windowSize - 1
232 // Remove old elements.
233 let indexFirstElement = currentElements.FindIndex(fun (_
, pj
) -> pj
>= window_j)
234 if indexFirstElement > 0
235 then currentElements.RemoveRange(0, indexFirstElement)
237 // Add the new elements.
238 for j
in window_j + windowSize - increment .. window_j + windowSize - 1 do
239 for i
in window_i_begin .. window_i_end do
240 if j
>= 0 && j
< w && edgesData.[i
, j
] = 1uy
241 then currentElements.Add((i
, j
))
243 if currentElements.Count >= 10
245 let mutable nbOfPicks = (float currentElements.Count) * factorNbPick |> int
246 while nbOfPicks > 0 do
247 let (p1y, p1x) as p1 = currentElements.[rng.Next(currentElements.Count)]
248 let (p2y, p2x) as p2 = currentElements.[rng.Next(currentElements.Count)]
249 let (p3y
, p3x
) as p3 = currentElements.[rng.Next(currentElements.Count)]
250 if p1 <> p2 && p1 <> p3 && p2 <> p3
252 nbOfPicks <- nbOfPicks - 1
253 let p1yf, p1xf
= float p1y, float p1x
254 let p2yf, p2xf
= float p2y, float p2x
255 let p3yf, p3xf
= float p3y, float p3x
256 if squaredDistance p1xf p1yf p2xf
p2yf >= minimumDistance &&
257 squaredDistance p1xf p1yf p3xf
p3yf >= minimumDistance &&
258 squaredDistance p2xf p2yf p3xf
p3yf >= minimumDistance
260 match areVectorsValid
p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0] with
262 match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf
p3yf with
263 | Some e
when e
.Cx > 0.0 && e
.Cx < (float w) - 1.0 && e
.Cy > 0.0 && e
.Cy < (float h) - 1.0 &&
264 e
.A >= r1 - radiusTolerance && e
.A <= r2 + radiusTolerance && e
.B >= r1 - radiusTolerance && e
.B <= r2 + radiusTolerance ->
269 currentElements.Clear()