62273b579f25b7ce51eb08e9956fefa2a10a70c3
4 open System.Collections.Generic
13 type private SearchExtremum = Minimum | Maximum
15 let private goldenSectionSearch
(f
: float -> float) (nbIter
: int) (xmin
: float) (xmax
: float) (searchExtremum
: SearchExtremum) : (float * float) =
16 let gr = 1.0 / 1.6180339887498948482
19 let mutable c = b - gr * (b - a)
20 let mutable d = a + gr * (b - a)
22 for i
in 1 .. nbIter
do
26 if searchExtremum
= Maximum
36 c <- b - gr * (b - a);
40 d <- a + gr * (b - a);
45 let ellipse (p1x
: float) (p1y
: float) (m1
: float) (p2x
: float) (p2y
: float) (m2
: float) (p3x
: float) (p3y
: float) : Types.Ellipse option =
46 let accuracy_extremum_search_1 = 4;
47 let accuracy_extremum_search_2 = 3;
49 // p3 as the referencial.
56 // Convert to polar coordinates.
60 let r1 = sqrt
(p1x**2.0 + p1y**2.0)
61 let theta1 = atan2
p1y p1x
63 let r2 = sqrt
(p2x**2.0 + p2y**2.0)
64 let theta2 = atan2
p2y p2x
67 4.0 * sin
(alpha1 - theta1) * (-r1 * sin
(alpha1 - theta1) + r2 * sin
(alpha1 - theta2)) *
68 sin
(alpha2 - theta2) * (-r1 * sin
(alpha2 - theta1) + r2 * sin
(alpha2 - theta2)) +
69 r1 * r2 * sin
(alpha1 - alpha2) **2.0 * sin
(theta1 - theta2) **2.0 < 0.0
74 (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)) /
75 (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)
79 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
80 let thetaTan, _
= goldenSectionSearch
rabs accuracy_extremum_search_1 0.0 Math.PI Maximum
83 let PTanx = rTan * cos
thetaTan
84 let PTany = rTan * sin
thetaTan
87 let d1b = -d1a * p1x + p1y
90 let d2b = -d2a * p2x + p2y
92 let d3a = -1.0 / tan
thetaTan
93 let d3b = -d3a * PTanx + PTany
95 let Ux = -(d1b - d2b) / (d1a - d2a)
96 let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
98 let Vx = -(d1b - d3b) / (d1a - d3a)
99 let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
101 let Wx = p1x + (p2x - p1x) / 2.0
102 let Wy = p1y + (p2y - p1y) / 2.0
104 let Zx = p1x + (PTanx - p1x) / 2.0
105 let Zy = p1y + (PTany - p1y) / 2.0
107 let va = -(-Vy + Zy) / (Vx - Zx)
108 let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
110 let ua = -(-Uy + Wy) / (Ux - Wx)
111 let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
113 let cx = -(vb - ub) / (va - ua)
114 let cy = -(ua * vb - va * ub) / (va - ua)
116 let rc = sqrt
(cx**2.0 + cy**2.0)
117 let psi = atan2
cy cx
121 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) /
122 (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 -
123 (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)) /
124 (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))
126 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
127 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.
128 let r2eTheta, r2e
= goldenSectionSearch
rellipse accuracy_extremum_search_2 0.0 (Math.PI / 2.0) Minimum
130 let rr1e = r r1eTheta
131 let r1ex = rr1e * cos
r1eTheta
132 let r1ey = rr1e * sin
r1eTheta
133 let mutable alpha = atan
((r1ey - cy) / (r1ex - cx))
136 alpha <- alpha + Math.PI
138 // Ride off the p3 referential.
142 Some { cx = cx; cy = cy; a = r1e
; b = r2e
; alpha = alpha }
147 let private vectorRotation
(p1x: float) (p1y: float) (v1x
: float) (v1y
: float) (px
: float) (py
: float) : float =
148 let mutable rotation = 1.0
172 let private areVectorsValid
(p1x: float) (p1y: float) (p2x: float) (p2y: float) (v1x
: float) (v1y
: float) (v2x
: float) (v2y
: float) : (float * float) option =
176 let b1 = -m1 * p1x + p1y
177 let b2 = -m2 * p2x + p2y
178 let px = -((b1 - b2)/(m1 - m2))
179 let py = -((m2 * b1 - m1 * b2)/(m1 - m2))
181 let rot1 = vectorRotation
p1x p1y v1x v1y
px py
182 let rot2 = vectorRotation
p2x p2y v2x v2y
px py
184 if rot1 = rot2 || rot1 * atan2
(p1y - py) (p1x - px) + rot2 * atan2
(p2y - py) (p2x - px) <= 0.0
191 let find (edges
: Matrix<byte
>)
192 (xDir
: Image<Gray, float>)
193 (yDir
: Image<Gray, float>)
194 (radiusRange
: float * float)
196 (factorNbPick
: float) : Types.Ellipse list =
198 let increment = windowSize
/ 4;
200 let r1, r2 = radiusRange
201 let radiusTolerance = (r2 - r1) * 0.2
203 let minimumDistance = (r2 / 1.5) ** 2.0;
204 let squaredDistance x1 y1 x2 y2
= (x1
- x2
) ** 2.0 + (y1
- y2
) ** 2.0;
209 let mutable last_i, last_j
= Int32.MaxValue, Int32.MaxValue
211 let currentElements = List<(int * int)>()
213 let edgesData = edges
.Data
214 let xDirData = xDir
.Data
215 let yDirData = yDir
.Data
219 let ellipses = MatchingEllipses ()
221 for window_i
in -windowSize
+ increment .. increment .. h - increment do
222 for window_j
in -windowSize
+ increment .. increment .. w - increment do
224 let window_i_begin = if window_i
< 0 then 0 else window_i
225 let window_i_end = if window_i + windowSize
- 1 >= h then h - 1 else window_i + windowSize
- 1
226 let window_j_begin = if window_j
< 0 then 0 else window_j
227 let window_j_end = if window_j + windowSize
- 1 >= w then w - 1 else window_j + windowSize
- 1
229 // Remove old elements.
230 let indexFirstElement = currentElements.FindIndex(fun (_
, pj
) -> pj
>= window_j)
231 if indexFirstElement > 0
232 then currentElements.RemoveRange(0, indexFirstElement)
234 // Add the new elements.
235 for j
in window_j + windowSize
- increment .. window_j + windowSize
- 1 do
236 for i
in window_i_begin .. window_i_end do
237 if j
>= 0 && j
< w && edgesData.[i
, j
] = 1uy
238 then currentElements.Add((i
, j
))
240 if currentElements.Count >= 10
242 let mutable nbOfPicks = (float currentElements.Count) * factorNbPick
|> int
243 while nbOfPicks > 0 do
244 let (p1y, p1x) as p1 = currentElements.[rng.Next(currentElements.Count)]
245 let (p2y, p2x) as p2 = currentElements.[rng.Next(currentElements.Count)]
246 let (p3y
, p3x
) as p3 = currentElements.[rng.Next(currentElements.Count)]
247 if p1 <> p2 && p1 <> p3 && p2 <> p3
249 nbOfPicks <- nbOfPicks - 1
250 let p1yf, p1xf
= float p1y, float p1x
251 let p2yf, p2xf
= float p2y, float p2x
252 let p3yf, p3xf
= float p3y, float p3x
253 if squaredDistance p1xf p1yf p2xf
p2yf >= minimumDistance &&
254 squaredDistance p1xf p1yf p3xf
p3yf >= minimumDistance &&
255 squaredDistance p2xf p2yf p3xf
p3yf >= minimumDistance
257 match areVectorsValid
p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0] with
259 match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf
p3yf with
260 | Some e
when e
.cx > 0.0 && e
.cx < (float w) - 1.0 && e
.cy > 0.0 && e
.cy < (float h) - 1.0 &&
261 e
.a >= r1 - radiusTolerance && e
.a <= r2 + radiusTolerance && e
.b >= r1 - radiusTolerance && e
.b <= r2 + radiusTolerance ->
266 currentElements.Clear()