X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FEllipse.fs;h=be359401a53131eeaeb2ec2d328935bbf26b47d7;hp=ef167b2e6e7e3946ed36576b5188164b239a9e6e;hb=05be8164d308447b916544ae3ce4211500dfd8da;hpb=044b0ae69df3ac565432545b2fa934589016f9bd diff --git a/Parasitemia/Parasitemia/Ellipse.fs b/Parasitemia/Parasitemia/Ellipse.fs index ef167b2..be35940 100644 --- a/Parasitemia/Parasitemia/Ellipse.fs +++ b/Parasitemia/Parasitemia/Ellipse.fs @@ -14,8 +14,8 @@ open Const type private SearchExtremum = Minimum | Maximum -let private goldenSectionSearch (f: float32 -> float32) (nbIter: int) (xmin: float32) (xmax: float32) (searchExtremum: SearchExtremum) : (float32 * float32) = - let gr = 1.f / 1.6180339887498948482f +let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float) (xmax: float) (searchExtremum: SearchExtremum) : (float * float) = + let gr = 1. / 1.6180339887498948482 let mutable a = xmin let mutable b = xmax let mutable c = b - gr * (b - a) @@ -41,14 +41,14 @@ let private goldenSectionSearch (f: float32 -> float32) (nbIter: int) (xmin: flo c <- d d <- a + gr * (b - a) - let x = (b + a) / 2.f + let x = (b + a) / 2. x, f x // Ellipse.A is always equal or greater than Ellipse.B. // Ellipse.Alpha is between 0 and Pi. -let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: float32) (m2: float32) (p3x: float32) (p3y: float32) : Types.Ellipse option = - let accuracy_extremum_search_1 = 8 // 3 - let accuracy_extremum_search_2 = 8 // 4 +let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2: float) (p3x: float) (p3y: float) : Types.Ellipse option = + let accuracy_extremum_search_1 = 10 // 3 + let accuracy_extremum_search_2 = 10 // 4 // p3 as the referencial. let p1x = p1x - p3x @@ -61,27 +61,27 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let alpha1 = atan m1 let alpha2 = atan m2 - let r1 = sqrt (p1x ** 2.f + p1y ** 2.f) + let r1 = sqrt (p1x ** 2. + p1y ** 2.) let theta1 = atan2 p1y p1x - let r2 = sqrt (p2x ** 2.f + p2y ** 2.f) + let r2 = sqrt (p2x ** 2. + p2y ** 2.) let theta2 = atan2 p2y p2x let valid = - 4.f * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) * + 4. * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) * sin (alpha2 - theta2) * (-r1 * sin (alpha2 - theta1) + r2 * sin (alpha2 - theta2)) + - r1 * r2 * sin (alpha1 - alpha2) ** 2.f * sin (theta1 - theta2) ** 2.f < 0.f + r1 * r2 * sin (alpha1 - alpha2) ** 2. * sin (theta1 - theta2) ** 2. < 0. if valid then let r theta = (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)) / - (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) + (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.) let rabs = r >> abs // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval. - let thetaTan, _ = goldenSectionSearch rabs accuracy_extremum_search_1 0.f PI Maximum + let thetaTan, _ = goldenSectionSearch rabs accuracy_extremum_search_1 0. Math.PI Maximum let rTan = r thetaTan let PTanx = rTan * cos thetaTan @@ -93,7 +93,7 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let d2a = tan alpha2 let d2b = -d2a * p2x + p2y - let d3a = -1.f / tan thetaTan + let d3a = -1. / tan thetaTan let d3b = -d3a * PTanx + PTany let Ux = -(d1b - d2b) / (d1a - d2a) @@ -102,11 +102,11 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let Vx = -(d1b - d3b) / (d1a - d3a) let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a) - let Wx = p1x + (p2x - p1x) / 2.f - let Wy = p1y + (p2y - p1y) / 2.f + let Wx = p1x + (p2x - p1x) / 2. + let Wy = p1y + (p2y - p1y) / 2. - let Zx = p1x + (PTanx - p1x) / 2.f - let Zy = p1y + (PTany - p1y) / 2.f + let Zx = p1x + (PTanx - p1x) / 2. + let Zy = p1y + (PTany - p1y) / 2. let va = -(-Vy + Zy) / (Vx - Zx) let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx) @@ -117,33 +117,33 @@ let ellipse (p1x: float32) (p1y: float32) (m1: float32) (p2x: float32) (p2y: flo let cx = -(vb - ub) / (va - ua) let cy = -(ua * vb - va * ub) / (va - ua) - let rc = sqrt (cx ** 2.f + cy ** 2.f) + let rc = sqrt (cx ** 2. + cy ** 2.) let psi = atan2 cy cx let rellipse theta = sqrt ( - 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) / - (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 - - (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)) / - (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)) + 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.) / + (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. - + (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)) / + (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.)) // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval. - 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. - let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0.f (PI / 2.f) Minimum + let r1eTheta, r1e = goldenSectionSearch rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Maximum // Pi/2 and not pi because the period is Pi. + let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Minimum let rr1e = r r1eTheta let r1ex = rr1e * cos r1eTheta let r1ey = rr1e * sin r1eTheta let mutable alpha = atan ((r1ey - cy) / (r1ex - cx)) - if alpha < 0.f + if alpha < 0. then - alpha <- alpha + PI + alpha <- alpha + Math.PI // Ride off the p3 referential. let cx = cx + p3x let cy = cy + p3y - Some (Types.Ellipse(cx, cy, r1e, r2e, alpha)) + Some (Types.Ellipse(float32 cx, float32 cy, float32 r1e, float32 r2e, float32 alpha)) else None @@ -209,7 +209,7 @@ let find (edges: Matrix) (yGradient: Image) (config: Config) : MatchingEllipses = - let r1, r2 = config.RBCMinRadius, config.RBCMaxRadius + let r1, r2 = config.RBCRadius.Min, config.RBCRadius.Max let incrementWindowDivisor = 4.f // We choose a window size for which the biggest ellipse can always be fitted in. @@ -220,8 +220,8 @@ let find (edges: Matrix) let radiusTolerance = (r2 - r1) * 0.2f - let squaredMinimumDistance = (r2 / 1.5f) ** 2.f - let squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2.f + (y1 - y2) ** 2.f + let squaredMinimumDistance = (float r2 / 1.5) ** 2. + let inline squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2. + (y1 - y2) ** 2. let h = edges.Height let w = edges.Width @@ -271,16 +271,16 @@ let find (edges: Matrix) if p1 <> p2 && p1 <> p3 && p2 <> p3 then nbOfPicks <- nbOfPicks - 1 - let p1yf, p1xf = float32 p1.Y, float32 p1.X - let p2yf, p2xf = float32 p2.Y, float32 p2.X - let p3yf, p3xf = float32 p3.Y, float32 p3.X + let p1yf, p1xf = float p1.Y, float p1.X + let p2yf, p2xf = float p2.Y, float p2.X + let p3yf, p3xf = float p3.Y, float p3.X if squaredDistance p1xf p1yf p2xf p2yf >= squaredMinimumDistance && squaredDistance p1xf p1yf p3xf p3yf >= squaredMinimumDistance && squaredDistance p2xf p2yf p3xf p3yf >= squaredMinimumDistance then - 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 + 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 | Some (m1, m2) -> - match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf p3yf with + match ellipse p1xf p1yf (float m1) p2xf p2yf (float m2) p3xf p3yf with | Some e when e.Cx > 0.f && e.Cx < w_f - 1.f && e.Cy > 0.f && e.Cy < h_f - 1.f && e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance -> ellipses.Add e