62273b579f25b7ce51eb08e9956fefa2a10a70c3
[master-thesis.git] / Parasitemia / Parasitemia / Ellipse.fs
1 module Ellipse
2
3 open System
4 open System.Collections.Generic
5
6 open Emgu.CV
7 open Emgu.CV.Structure
8
9 open Utils
10 open MatchingEllipses
11
12
13 type private SearchExtremum = Minimum | Maximum
14
15 let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float) (xmax: float) (searchExtremum: SearchExtremum) : (float * float) =
16 let gr = 1.0 / 1.6180339887498948482
17 let mutable a = xmin
18 let mutable b = xmax
19 let mutable c = b - gr * (b - a)
20 let mutable d = a + gr * (b - a)
21
22 for i in 1 .. nbIter do
23 let mutable fc = f c
24 let mutable fd = f d
25
26 if searchExtremum = Maximum
27 then
28 let tmp = fc
29 fc <- fd
30 fd <- tmp
31
32 if fc < fd
33 then
34 b <- d;
35 d <- c;
36 c <- b - gr * (b - a);
37 else
38 a <- c;
39 c <- d;
40 d <- a + gr * (b - a);
41
42 let x = (b + a) / 2.0
43 x, f x
44
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;
48
49 // p3 as the referencial.
50 let p1x = p1x - p3x
51 let p1y = p1y - p3y
52
53 let p2x = p2x - p3x
54 let p2y = p2y - p3y
55
56 // Convert to polar coordinates.
57 let alpha1 = atan m1
58 let alpha2 = atan m2
59
60 let r1 = sqrt (p1x**2.0 + p1y**2.0)
61 let theta1 = atan2 p1y p1x
62
63 let r2 = sqrt (p2x**2.0 + p2y**2.0)
64 let theta2 = atan2 p2y p2x
65
66 let valid =
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
70
71 if valid
72 then
73 let r theta =
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)
76
77 let rabs = r >> abs
78
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
81 let rTan = r thetaTan
82
83 let PTanx = rTan * cos thetaTan
84 let PTany = rTan * sin thetaTan
85
86 let d1a = tan alpha1
87 let d1b = -d1a * p1x + p1y
88
89 let d2a = tan alpha2
90 let d2b = -d2a * p2x + p2y
91
92 let d3a = -1.0 / tan thetaTan
93 let d3b = -d3a * PTanx + PTany
94
95 let Ux = -(d1b - d2b) / (d1a - d2a)
96 let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
97
98 let Vx = -(d1b - d3b) / (d1a - d3a)
99 let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
100
101 let Wx = p1x + (p2x - p1x) / 2.0
102 let Wy = p1y + (p2y - p1y) / 2.0
103
104 let Zx = p1x + (PTanx - p1x) / 2.0
105 let Zy = p1y + (PTany - p1y) / 2.0
106
107 let va = -(-Vy + Zy) / (Vx - Zx)
108 let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
109
110 let ua = -(-Uy + Wy) / (Ux - Wx)
111 let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
112
113 let cx = -(vb - ub) / (va - ua)
114 let cy = -(ua * vb - va * ub) / (va - ua)
115
116 let rc = sqrt (cx**2.0 + cy**2.0)
117 let psi = atan2 cy cx
118
119 let rellipse theta =
120 sqrt (
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))
125
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
129
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))
134 if alpha < 0.0
135 then
136 alpha <- alpha + Math.PI
137
138 // Ride off the p3 referential.
139 let cx = cx + p3x
140 let cy = cy + p3y
141
142 Some { cx = cx; cy = cy; a = r1e; b = r2e; alpha = alpha }
143 else
144 None
145
146
147 let private vectorRotation (p1x: float) (p1y: float) (v1x: float) (v1y: float) (px: float) (py: float) : float =
148 let mutable rotation = 1.0
149 if p1y > py
150 then
151 if v1x > 0.0
152 then
153 rotation <- -1.0
154 elif p1y < py
155 then
156 if v1x < 0.0
157 then
158 rotation <- -1.0
159 elif p1x > px
160 then
161 if v1y < 0.0
162 then
163 rotation <- -1.0
164 elif p1x < px
165 then
166 if v1y > 0.0
167 then
168 rotation <- -1.0
169 rotation
170
171
172 let private areVectorsValid (p1x: float) (p1y: float) (p2x: float) (p2y: float) (v1x: float) (v1y: float) (v2x: float) (v2y: float) : (float * float) option =
173 let m1 = -v1x / v1y
174 let m2 = -v2x / v2y
175
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))
180
181 let rot1 = vectorRotation p1x p1y v1x v1y px py
182 let rot2 = vectorRotation p2x p2y v2x v2y px py
183
184 if rot1 = rot2 || rot1 * atan2 (p1y - py) (p1x - px) + rot2 * atan2 (p2y - py) (p2x - px) <= 0.0
185 then
186 None
187 else
188 Some (m1, m2)
189
190
191 let find (edges: Matrix<byte>)
192 (xDir: Image<Gray, float>)
193 (yDir: Image<Gray, float>)
194 (radiusRange: float * float)
195 (windowSize: int)
196 (factorNbPick: float) : Types.Ellipse list =
197
198 let increment = windowSize / 4;
199
200 let r1, r2 = radiusRange
201 let radiusTolerance = (r2 - r1) * 0.2
202
203 let minimumDistance = (r2 / 1.5) ** 2.0;
204 let squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2.0 + (y1 - y2) ** 2.0;
205
206 let h = edges.Height
207 let w = edges.Width
208
209 let mutable last_i, last_j = Int32.MaxValue, Int32.MaxValue
210
211 let currentElements = List<(int * int)>()
212
213 let edgesData = edges.Data
214 let xDirData = xDir.Data
215 let yDirData = yDir.Data
216
217 let rng = Random()
218
219 let ellipses = MatchingEllipses ()
220
221 for window_i in -windowSize + increment .. increment .. h - increment do
222 for window_j in -windowSize + increment .. increment .. w - increment do
223
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
228
229 // Remove old elements.
230 let indexFirstElement = currentElements.FindIndex(fun (_, pj) -> pj >= window_j)
231 if indexFirstElement > 0
232 then currentElements.RemoveRange(0, indexFirstElement)
233
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))
239
240 if currentElements.Count >= 10
241 then
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
248 then
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
256 then
257 match areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0] with
258 | Some (m1, m2) ->
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 ->
262 ellipses.Add e
263 | _ -> ()
264 | _ -> ()
265
266 currentElements.Clear()
267
268 ellipses.Ellipses
269