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