Remove the removing of cells bases on standard deviation.
[master-thesis.git] / Parasitemia / ParasitemiaCore / Ellipse.fs
index e65100b..01e1bec 100644 (file)
@@ -14,142 +14,15 @@ open Config
 open MatchingEllipses
 open Const
 
-type private SearchExtremum = Minimum | Maximum
-
-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)
-    let mutable d = a + gr * (b - a)
-
-    for i in 1 .. nbIter do
-        let mutable fc = f c
-        let mutable fd = f d
-
-        if searchExtremum = Maximum
-        then
-            let tmp = fc
-            fc <- fd
-            fd <- tmp
-
-        if fc < fd
-        then
-            b <- d
-            d <- c
-            c <- b - gr * (b - a)
-        else
-            a <- c
-            c <- d
-            d <- a + gr * (b - a)
-
-    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.
+// This is a ratio of the biggest radius.
+let minimumDistanceBetweenDrawnPoints = 0.6
+
+/// <summary>
+/// Try to build an ellipse from three points and two tangents passing by the first and the second point.
+/// 'Ellipse.A' is always equal or greater than Ellipse.B.
+/// 'Ellipse.Alpha' is between 0 and Pi.
+/// </summary>
 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
-    let p1y = p1y - p3y
-
-    let p2x = p2x - p3x
-    let p2y = p2y - p3y
-
-    // Convert to polar coordinates.
-    let alpha1 = atan m1
-    let alpha2 = atan m2
-
-    let r1 = sqrt (p1x ** 2. + p1y ** 2.)
-    let theta1 = atan2 p1y p1x
-
-    let r2 = sqrt (p2x ** 2. + p2y ** 2.)
-    let theta2 = atan2 p2y p2x
-
-    let valid =
-        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. * 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. - 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. Math.PI Maximum
-        let rTan = r thetaTan
-
-        let PTanx = rTan * cos thetaTan
-        let PTany = rTan * sin thetaTan
-
-        let d1a = tan alpha1
-        let d1b = -d1a * p1x + p1y
-
-        let d2a = tan alpha2
-        let d2b = -d2a * p2x + p2y
-
-        let d3a = -1. / tan thetaTan
-        let d3b = -d3a * PTanx + PTany
-
-        let Ux = -(d1b - d2b) / (d1a - d2a)
-        let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
-
-        let Vx = -(d1b - d3b) / (d1a - d3a)
-        let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
-
-        let Wx = p1x + (p2x - p1x) / 2.
-        let Wy = p1y + (p2y - p1y) / 2.
-
-        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)
-
-        let ua = -(-Uy + Wy) / (Ux - Wx)
-        let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
-
-        let cx = -(vb - ub) / (va - ua)
-        let cy = -(ua * vb - va * ub) / (va - ua)
-
-        let rc = sqrt (cx ** 2. + cy ** 2.)
-        let psi = atan2 cy cx
-
-        let rellipse theta =
-            sqrt (
-                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. (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.
-        then
-           alpha <- alpha + Math.PI
-
-        // Ride off the p3 referential.
-        let cx = cx + p3x
-        let cy = cy + p3y
-
-        Some (Types.Ellipse(float32 cx, float32 cy, float32 r1e, float32 r2e, float32 alpha))
-    else
-        None
-
-let ellipse2 (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2: float) (p3x: float) (p3y: float) : Types.Ellipse option =
     let p0 = pointFromTwoLines (Types.Line(float32 m1, float32 (p1y - m1 * p1x))) (Types.Line(float32 m2, float32(p2y - m2 * p2x)))
     let p0x, p0y = float p0.X, float p0.Y
 
@@ -204,30 +77,18 @@ let ellipse2 (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2
 
             Some (Types.Ellipse(float32 cx, float32 cy, float32 majorAxis, float32 minorAxis, float32 phi'))
 
-
-let private vectorRotation (p1x: float32) (p1y: float32) (v1x: float32) (v1y: float32) (px: float32) (py: float32) : float32 =
-    let mutable rotation = 1.f
-    if p1y > py
-    then
-        if v1x > 0.f
-        then
-            rotation <- -1.f
-    elif p1y < py
+let inline private vectorRotation (px: float32) (py: float32) (vx: float32) (vy: float32) (p0x: float32) (p0y: float32) : float32 =
+    if py > p0y
     then
-        if v1x < 0.f
-        then
-            rotation <- -1.f
-    elif p1x > px
+        if vx > 0.f then -1.f else 1.f
+    elif py < p0y
     then
-        if v1y < 0.f
-        then
-            rotation <- -1.f
-    elif p1x < px
+        if vx < 0.f then -1.f else 1.f
+    elif px > p0x
     then
-        if v1y > 0.f
-        then
-            rotation <- -1.f
-    rotation
+        if vy < 0.f then -1.f else 1.f
+    else // p1x < px
+        if vy > 0.f then -1.f else 1.f
 
 let private areVectorsValid (p1x: float32) (p1y: float32) (p2x: float32) (p2y: float32) (v1x: float32) (v1y: float32) (v2x: float32) (v2y: float32) : (float32 * float32) option =
     let m1 = -v1x / v1y
@@ -235,34 +96,38 @@ let private areVectorsValid (p1x: float32) (p1y: float32) (p2x: float32) (p2y: f
 
     let b1 = -m1 * p1x + p1y
     let b2 = -m2 * p2x + p2y
-    let px = -((b1 - b2) / (m1 - m2))
-    let py = -((m2 * b1 - m1 * b2) / (m1 - m2))
+    let p0x = -(b1 - b2) / (m1 - m2)
+    let p0y = -(m2 * b1 - m1 * b2) / (m1 - m2)
 
-    let rot1 = vectorRotation p1x p1y v1x v1y px py
-    let rot2 = vectorRotation p2x p2y v2x v2y px py
+    let rot1 = vectorRotation p1x p1y v1x v1y p0x p0y
+    let rot2 = vectorRotation p2x p2y v2x v2y p0x p0y
 
     if rot1 = rot2
     then
         None
     else
-        let alpha1 = atan2 (p1y - py) (p1x - px)
-        let alpha2 = atan2 (p2y - py) (p2x - px)
+        let alpha1 =
+            let alpha1' = atan2 (p1y - p0y) (p1x - p0x)
+            if alpha1' < 0.f then 2.f * PI + alpha1' else alpha1'
 
-        let alpha1' = if alpha1 < 0.f then 2.f * PI + alpha1 else alpha1
-        let alpha2' = if alpha2 < 0.f then 2.f * PI + alpha2 else alpha2
+        let alpha2 =
+            let alpha2' = atan2 (p2y - p0y) (p2x - p0x)
+            if alpha2' < 0.f then 2.f * PI + alpha2' else alpha2'
 
-        let diff = rot1 * alpha1' + rot2 * alpha2'
+        let diff = rot1 * alpha1 + rot2 * alpha2
 
         if diff > PI || (diff < 0.f && diff > -PI)
         then
-            None
+            Some (m1, m2)
         else
-        Some (m1, m2)
-
+            None
 
+/// <summary>
+/// Build a set of ellipses as a 'MatchingEllipses' object by finding ellipses with the given edges and gradient.
+/// </summary>
 let find (edges: Matrix<byte>)
-         (xGradient: Image<Gray, float32>)
-         (yGradient: Image<Gray, float32>)
+         (xGradient: Matrix<float32>)
+         (yGradient: Matrix<float32>)
          (config: Config) : MatchingEllipses =
 
     let r1, r2 = config.RBCRadius.Min, config.RBCRadius.Max
@@ -270,13 +135,15 @@ let find (edges: Matrix<byte>)
 
     // We choose a window size for which the biggest ellipse can always be fitted in.
     let windowSize = roundInt (2.f * r2)
-    let factorNbPick = config.Parameters.factorNbPick
+    let factorNbValidPick = config.Parameters.factorNbValidPick
+    let factorNbMaxPick = config.Parameters.factorNbMaxPick
+    let nbPickElementsMin = config.Parameters.nbPickElementsMin
 
     let increment = windowSize / (int incrementWindowDivisor)
 
     let radiusTolerance = (r2 - r1) * 0.2f
 
-    let squaredMinimumDistance = (float r2 / 1.5) ** 2.
+    let squaredMinimumDistance = (float config.RBCRadius.Pixel * minimumDistanceBetweenDrawnPoints) ** 2.
     let inline squaredDistance x1 y1 x2 y2 = (x1 - x2) ** 2. + (y1 - y2) ** 2.
 
     let h = edges.Height
@@ -317,10 +184,11 @@ let find (edges: Matrix<byte>)
                     if edgesData.[i, j] = 1uy
                     then currentElements.Add(Point(j, i))
 
-            if currentElements.Count >= 10
+            if currentElements.Count >= nbPickElementsMin
             then
-                let mutable nbOfPicks = (float currentElements.Count) * factorNbPick |> int
-                while nbOfPicks > 0 do
+                let mutable nbOfPicks = (float currentElements.Count) * factorNbMaxPick |> int
+                let mutable nbOfValidPicks = (float currentElements.Count) * factorNbValidPick |> int
+                while nbOfPicks > 0 && nbOfValidPicks > 0 do
                     let p1 = currentElements.[rng.Next(currentElements.Count)]
                     let p2 = currentElements.[rng.Next(currentElements.Count)]
                     let p3 = currentElements.[rng.Next(currentElements.Count)]
@@ -334,12 +202,13 @@ let find (edges: Matrix<byte>)
                            squaredDistance p1xf p1yf p3xf p3yf >= squaredMinimumDistance &&
                            squaredDistance p2xf p2yf p3xf p3yf >= squaredMinimumDistance
                         then
-                            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
+                            match areVectorsValid (float32 p1xf) (float32 p1yf) (float32 p2xf) (float32 p2yf) -xDirData.[p1.Y, p1.X] -yDirData.[p1.Y, p1.X] -xDirData.[p2.Y, p2.X] -yDirData.[p2.Y, p2.X] with
                             | Some (m1, m2) ->
-                                match ellipse2 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 &&
+                                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
+                                    nbOfValidPicks <- nbOfValidPicks - 1
+                                    ellipses.Add e
                                 | _ -> ()
                             | _ -> ()