The main process is now complete.
[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 // Ellipse.A is always equal or greater than Ellipse.B.
46 // Ellipse.Alpha is between 0 and Pi.
47 let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2: float) (p3x: float) (p3y: float) : Types.Ellipse option =
48 let accuracy_extremum_search_1 = 4
49 let accuracy_extremum_search_2 = 3
50
51 // p3 as the referencial.
52 let p1x = p1x - p3x
53 let p1y = p1y - p3y
54
55 let p2x = p2x - p3x
56 let p2y = p2y - p3y
57
58 // Convert to polar coordinates.
59 let alpha1 = atan m1
60 let alpha2 = atan m2
61
62 let r1 = sqrt (p1x ** 2.0 + p1y ** 2.0)
63 let theta1 = atan2 p1y p1x
64
65 let r2 = sqrt (p2x**2.0 + p2y**2.0)
66 let theta2 = atan2 p2y p2x
67
68 let valid =
69 4.0 * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) *
70 sin (alpha2 - theta2) * (-r1 * sin (alpha2 - theta1) + r2 * sin (alpha2 - theta2)) +
71 r1 * r2 * sin (alpha1 - alpha2) ** 2.0 * sin (theta1 - theta2) ** 2.0 < 0.0
72
73 if valid
74 then
75 let r theta =
76 (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)) /
77 (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)
78
79 let rabs = r >> abs
80
81 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
82 let thetaTan, _ = goldenSectionSearch rabs accuracy_extremum_search_1 0.0 Math.PI Maximum
83 let rTan = r thetaTan
84
85 let PTanx = rTan * cos thetaTan
86 let PTany = rTan * sin thetaTan
87
88 let d1a = tan alpha1
89 let d1b = -d1a * p1x + p1y
90
91 let d2a = tan alpha2
92 let d2b = -d2a * p2x + p2y
93
94 let d3a = -1.0 / tan thetaTan
95 let d3b = -d3a * PTanx + PTany
96
97 let Ux = -(d1b - d2b) / (d1a - d2a)
98 let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
99
100 let Vx = -(d1b - d3b) / (d1a - d3a)
101 let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
102
103 let Wx = p1x + (p2x - p1x) / 2.0
104 let Wy = p1y + (p2y - p1y) / 2.0
105
106 let Zx = p1x + (PTanx - p1x) / 2.0
107 let Zy = p1y + (PTany - p1y) / 2.0
108
109 let va = -(-Vy + Zy) / (Vx - Zx)
110 let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
111
112 let ua = -(-Uy + Wy) / (Ux - Wx)
113 let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
114
115 let cx = -(vb - ub) / (va - ua)
116 let cy = -(ua * vb - va * ub) / (va - ua)
117
118 let rc = sqrt (cx**2.0 + cy**2.0)
119 let psi = atan2 cy cx
120
121 let rellipse theta =
122 sqrt (
123 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) /
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) ** 2.0 -
125 (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)) /
126 (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))
127
128 // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
129 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.
130 let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0.0 (Math.PI / 2.0) Minimum
131
132 let rr1e = r r1eTheta
133 let r1ex = rr1e * cos r1eTheta
134 let r1ey = rr1e * sin r1eTheta
135 let mutable alpha = atan ((r1ey - cy) / (r1ex - cx))
136 if alpha < 0.0
137 then
138 alpha <- alpha + Math.PI
139
140 // Ride off the p3 referential.
141 let cx = cx + p3x
142 let cy = cy + p3y
143
144 Some (Types.Ellipse(cx, cy, r1e, r2e, alpha))
145 else
146 None
147
148
149 let private vectorRotation (p1x: float) (p1y: float) (v1x: float) (v1y: float) (px: float) (py: float) : float =
150 let mutable rotation = 1.0
151 if p1y > py
152 then
153 if v1x > 0.0
154 then
155 rotation <- -1.0
156 elif p1y < py
157 then
158 if v1x < 0.0
159 then
160 rotation <- -1.0
161 elif p1x > px
162 then
163 if v1y < 0.0
164 then
165 rotation <- -1.0
166 elif p1x < px
167 then
168 if v1y > 0.0
169 then
170 rotation <- -1.0
171 rotation
172
173
174 let private areVectorsValid (p1x: float) (p1y: float) (p2x: float) (p2y: float) (v1x: float) (v1y: float) (v2x: float) (v2y: float) : (float * float) option =
175 let m1 = -v1x / v1y
176 let m2 = -v2x / v2y
177
178 let b1 = -m1 * p1x + p1y
179 let b2 = -m2 * p2x + p2y
180 let px = -((b1 - b2)/(m1 - m2))
181 let py = -((m2 * b1 - m1 * b2)/(m1 - m2))
182
183 let rot1 = vectorRotation p1x p1y v1x v1y px py
184 let rot2 = vectorRotation p2x p2y v2x v2y px py
185
186 if rot1 = rot2 || rot1 * atan2 (p1y - py) (p1x - px) + rot2 * atan2 (p2y - py) (p2x - px) <= 0.0
187 then
188 None
189 else
190 Some (m1, m2)
191
192 let find (edges: Matrix<byte>)
193 (xDir: Image<Gray, float>)
194 (yDir: Image<Gray, float>)
195 (radiusRange: float * float)
196 (windowSize: int)
197 (factorNbPick: float) : Types.Ellipse list =
198
199 let increment = windowSize / 4
200
201 let r1, r2 = radiusRange
202 let radiusTolerance = (r2 - r1) * 0.2
203
204 let minimumDistance = (r2 / 1.5) ** 2.0
205 let squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2.0 + (y1 - y2) ** 2.0
206
207 let h = edges.Height
208 let w = edges.Width
209
210 let mutable last_i, last_j = Int32.MaxValue, Int32.MaxValue
211
212 let currentElements = List<(int * int)>()
213
214 let edgesData = edges.Data
215 let xDirData = xDir.Data
216 let yDirData = yDir.Data
217
218 let rng = Random(42)
219
220 let ellipses = MatchingEllipses(r1)
221
222 for window_i in -windowSize + increment .. increment .. h - increment do
223 for window_j in -windowSize + increment .. increment .. w - increment do
224
225 let window_i_begin = if window_i < 0 then 0 else window_i
226 let window_i_end = if window_i + windowSize - 1 >= h then h - 1 else window_i + windowSize - 1
227 let window_j_begin = if window_j < 0 then 0 else window_j
228 let window_j_end = if window_j + windowSize - 1 >= w then w - 1 else window_j + windowSize - 1
229
230 // Remove old elements.
231 let indexFirstElement = currentElements.FindIndex(fun (_, pj) -> pj >= window_j)
232 if indexFirstElement > 0
233 then currentElements.RemoveRange(0, indexFirstElement)
234
235 // Add the new elements.
236 for j in window_j + windowSize - increment .. window_j + windowSize - 1 do
237 for i in window_i_begin .. window_i_end do
238 if j >= 0 && j < w && edgesData.[i, j] = 1uy
239 then currentElements.Add((i, j))
240
241 if currentElements.Count >= 10
242 then
243 let mutable nbOfPicks = (float currentElements.Count) * factorNbPick |> int
244 while nbOfPicks > 0 do
245 let (p1y, p1x) as p1 = currentElements.[rng.Next(currentElements.Count)]
246 let (p2y, p2x) as p2 = currentElements.[rng.Next(currentElements.Count)]
247 let (p3y, p3x) as p3 = currentElements.[rng.Next(currentElements.Count)]
248 if p1 <> p2 && p1 <> p3 && p2 <> p3
249 then
250 nbOfPicks <- nbOfPicks - 1
251 let p1yf, p1xf = float p1y, float p1x
252 let p2yf, p2xf = float p2y, float p2x
253 let p3yf, p3xf = float p3y, float p3x
254 if squaredDistance p1xf p1yf p2xf p2yf >= minimumDistance &&
255 squaredDistance p1xf p1yf p3xf p3yf >= minimumDistance &&
256 squaredDistance p2xf p2yf p3xf p3yf >= minimumDistance
257 then
258 match areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0] with
259 | Some (m1, m2) ->
260 match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf p3yf with
261 | Some e when e.Cx > 0.0 && e.Cx < (float w) - 1.0 && e.Cy > 0.0 && e.Cy < (float h) - 1.0 &&
262 e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance ->
263 ellipses.Add e
264 | _ -> ()
265 | _ -> ()
266
267 currentElements.Clear()
268
269 ellipses.Ellipses
270