73771bee58873a88448b4ad471a3a6288f44d93f
[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 Config
11 open MatchingEllipses
12
13
14 type private SearchExtremum = Minimum | Maximum
15
16 let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float) (xmax: float) (searchExtremum: SearchExtremum) : (float * float) =
17 let gr = 1.0 / 1.6180339887498948482
18 let mutable a = xmin
19 let mutable b = xmax
20 let mutable c = b - gr * (b - a)
21 let mutable d = a + gr * (b - a)
22
23 for i in 1 .. nbIter do
24 let mutable fc = f c
25 let mutable fd = f d
26
27 if searchExtremum = Maximum
28 then
29 let tmp = fc
30 fc <- fd
31 fd <- tmp
32
33 if fc < fd
34 then
35 b <- d
36 d <- c
37 c <- b - gr * (b - a)
38 else
39 a <- c
40 c <- d
41 d <- a + gr * (b - a)
42
43 let x = (b + a) / 2.0
44 x, f x
45
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 = 8 // 3
50 let accuracy_extremum_search_2 = 8 // 4
51
52 // p3 as the referencial.
53 let p1x = p1x - p3x
54 let p1y = p1y - p3y
55
56 let p2x = p2x - p3x
57 let p2y = p2y - p3y
58
59 // Convert to polar coordinates.
60 let alpha1 = atan m1
61 let alpha2 = atan m2
62
63 let r1 = sqrt (p1x ** 2.0 + p1y ** 2.0)
64 let theta1 = atan2 p1y p1x
65
66 let r2 = sqrt (p2x**2.0 + p2y**2.0)
67 let theta2 = atan2 p2y p2x
68
69 let valid =
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
73
74 if valid
75 then
76 let r theta =
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)
79
80 let rabs = r >> abs
81
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
84 let rTan = r thetaTan
85
86 let PTanx = rTan * cos thetaTan
87 let PTany = rTan * sin thetaTan
88
89 let d1a = tan alpha1
90 let d1b = -d1a * p1x + p1y
91
92 let d2a = tan alpha2
93 let d2b = -d2a * p2x + p2y
94
95 let d3a = -1.0 / tan thetaTan
96 let d3b = -d3a * PTanx + PTany
97
98 let Ux = -(d1b - d2b) / (d1a - d2a)
99 let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
100
101 let Vx = -(d1b - d3b) / (d1a - d3a)
102 let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
103
104 let Wx = p1x + (p2x - p1x) / 2.0
105 let Wy = p1y + (p2y - p1y) / 2.0
106
107 let Zx = p1x + (PTanx - p1x) / 2.0
108 let Zy = p1y + (PTany - p1y) / 2.0
109
110 let va = -(-Vy + Zy) / (Vx - Zx)
111 let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
112
113 let ua = -(-Uy + Wy) / (Ux - Wx)
114 let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
115
116 let cx = -(vb - ub) / (va - ua)
117 let cy = -(ua * vb - va * ub) / (va - ua)
118
119 let rc = sqrt (cx**2.0 + cy**2.0)
120 let psi = atan2 cy cx
121
122 let rellipse theta =
123 sqrt (
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))
128
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
132
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))
137 if alpha < 0.0
138 then
139 alpha <- alpha + Math.PI
140
141 // Ride off the p3 referential.
142 let cx = cx + p3x
143 let cy = cy + p3y
144
145 Some (Types.Ellipse(cx, cy, r1e, r2e, alpha))
146 else
147 None
148
149
150 let private vectorRotation (p1x: float) (p1y: float) (v1x: float) (v1y: float) (px: float) (py: float) : float =
151 let mutable rotation = 1.0
152 if p1y > py
153 then
154 if v1x > 0.0
155 then
156 rotation <- -1.0
157 elif p1y < py
158 then
159 if v1x < 0.0
160 then
161 rotation <- -1.0
162 elif p1x > px
163 then
164 if v1y < 0.0
165 then
166 rotation <- -1.0
167 elif p1x < px
168 then
169 if v1y > 0.0
170 then
171 rotation <- -1.0
172 rotation
173
174
175 let private areVectorsValid (p1x: float) (p1y: float) (p2x: float) (p2y: float) (v1x: float) (v1y: float) (v2x: float) (v2y: float) : (float * float) option =
176 let m1 = -v1x / v1y
177 let m2 = -v2x / v2y
178
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))
183
184 let rot1 = vectorRotation p1x p1y v1x v1y px py
185 let rot2 = vectorRotation p2x p2y v2x v2y px py
186
187 if rot1 = rot2
188 then
189 None
190 else
191 let alpha1 = atan2 (p1y - py) (p1x - px)
192 let alpha2 = atan2 (p2y - py) (p2x - px)
193
194 let alpha1' = if alpha1 < 0.0 then 2.0 * Math.PI + alpha1 else alpha1
195 let alpha2' = if alpha2 < 0.0 then 2.0 * Math.PI + alpha2 else alpha2
196
197 let diff = rot1 * alpha1' + rot2 * alpha2'
198
199 if diff > Math.PI || (diff < 0.0 && diff > -Math.PI)
200 then
201 None
202 else
203 Some (m1, m2)
204
205
206 let find (edges: Matrix<byte>)
207 (xGradient: Image<Gray, float>)
208 (yGradient: Image<Gray, float>)
209 (config: Config) : MatchingEllipses =
210
211 let r1, r2 = config.RBCMinRadius, config.RBCMaxRadius
212 let windowSize = roundInt (config.Parameters.factorWindowSize * r2)
213 let factorNbPick = config.Parameters.factorNbPick
214
215 let increment = windowSize / 4
216
217 let radiusTolerance = (r2 - r1) * 0.2
218
219 let minimumDistance = (r2 / 1.5) ** 2.0
220 let squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2.0 + (y1 - y2) ** 2.0
221
222 let h = edges.Height
223 let w = edges.Width
224
225 let mutable last_i, last_j = Int32.MaxValue, Int32.MaxValue
226
227 let currentElements = List<(int * int)>()
228
229 let edgesData = edges.Data
230 let xDirData = xGradient.Data
231 let yDirData = yGradient.Data
232
233 let rng = Random(42)
234
235 let ellipses = MatchingEllipses(r1)
236
237 for window_i in -windowSize + increment .. increment .. h - increment do
238 for window_j in -windowSize + increment .. increment .. w - increment do
239
240 let window_i_begin = if window_i < 0 then 0 else window_i
241 let window_i_end = if window_i + windowSize - 1 >= h then h - 1 else window_i + windowSize - 1
242 let window_j_begin = if window_j < 0 then 0 else window_j
243 let window_j_end = if window_j + windowSize - 1 >= w then w - 1 else window_j + windowSize - 1
244
245 // Remove old elements.
246 let indexFirstElement = currentElements.FindIndex(fun (_, pj) -> pj >= window_j)
247 if indexFirstElement > 0
248 then currentElements.RemoveRange(0, indexFirstElement)
249
250 // Add the new elements.
251 for j in window_j + windowSize - increment .. window_j + windowSize - 1 do
252 for i in window_i_begin .. window_i_end do
253 if j >= 0 && j < w && edgesData.[i, j] = 1uy
254 then currentElements.Add((i, j))
255
256 if currentElements.Count >= 10
257 then
258 let mutable nbOfPicks = (float currentElements.Count) * factorNbPick |> int
259 while nbOfPicks > 0 do
260 let (p1y, p1x) as p1 = currentElements.[rng.Next(currentElements.Count)]
261 let (p2y, p2x) as p2 = currentElements.[rng.Next(currentElements.Count)]
262 let (p3y, p3x) as p3 = currentElements.[rng.Next(currentElements.Count)]
263 if p1 <> p2 && p1 <> p3 && p2 <> p3
264 then
265 nbOfPicks <- nbOfPicks - 1
266 let p1yf, p1xf = float p1y, float p1x
267 let p2yf, p2xf = float p2y, float p2x
268 let p3yf, p3xf = float p3y, float p3x
269 if squaredDistance p1xf p1yf p2xf p2yf >= minimumDistance &&
270 squaredDistance p1xf p1yf p3xf p3yf >= minimumDistance &&
271 squaredDistance p2xf p2yf p3xf p3yf >= minimumDistance
272 then
273 match areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0] with
274 | Some (m1, m2) ->
275 match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf p3yf with
276 | Some e when e.Cx > 0.0 && e.Cx < (float w) - 1.0 && e.Cy > 0.0 && e.Cy < (float h) - 1.0 &&
277 e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance ->
278
279 let prout = areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0]
280 ellipses.Add e
281 | _ -> ()
282 | _ -> ()
283
284 currentElements.Clear()
285
286 ellipses
287