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