* Add an exact method to compute an ellipse from three points and two tangents.
[master-thesis.git] / Parasitemia / Parasitemia / Ellipse.fs
index be35940..ac3c041 100644 (file)
@@ -4,6 +4,8 @@ open System
 open System.Collections.Generic
 open System.Drawing
 
+open MathNet.Numerics.LinearAlgebra
+
 open Emgu.CV
 open Emgu.CV.Structure
 
@@ -147,6 +149,61 @@ let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2:
     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
+
+    let s = matrix [[ 1.;   0.;  0.  ]
+                    [ 0.;   0.; -0.5 ]
+                    [ 0.; -0.5;  0.  ]]
+
+    let v0 = matrix [[ 1.; p0x; p0y ]]
+    let v1 = matrix [[ 1.; p1x; p1y ]]
+    let v2 = matrix [[ 1.; p2x; p2y ]]
+    let v3 = matrix [[ 1.; p3x; p3y ]]
+
+    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()
+    let conicMat = p * s.Inverse() * p.Transpose()
+    let a = conicMat.[0, 0]
+    let b = conicMat.[0, 1]
+    let c = conicMat.[1, 1]
+    let d = conicMat.[0, 2]
+    let e = conicMat.[1, 2]
+    let f = conicMat.[2, 2]
+
+    // Center.
+    let cx = b / a
+    let cy = d / a
+
+    let at = c * f - e ** 2. + (e * d - b * f) * cx + (b * e - c * d) * cy
+    if at = 0.
+    then
+        None
+    else
+        let q = (-1. / at) * (matrix [[ a * f - d ** 2.0; b * d - a * e ]; [ b * d - a * e; a * c - b ** 2.0 ]])
+        let eigen = q.Evd()
+        let eigenValues = eigen.EigenValues
+        let lambda = eigenValues.[1].Real
+        let mu = eigenValues.[0].Real
+
+        if lambda <= 0. || mu <= 0.
+        then
+            None
+        else
+            let r1, r2 = 1. / (sqrt lambda), 1. / (sqrt mu)
+
+            let eigenVectors = eigen.EigenVectors
+            let v_a = eigenVectors.[0, 0]
+            let v_b = eigenVectors.[1, 0] // [0, 1]
+
+            // Angle against the longest axis.
+            let phi = (if r2 > r1 then atan (v_b / v_a) else atan (v_a / v_b))
+
+            let phi' = if phi < 0. then phi + Math.PI else phi
+            let majorAxis, minorAxis = if r1 > r2 then r1, r2 else r2, r1
+
+            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
@@ -172,7 +229,6 @@ let private vectorRotation (p1x: float32) (p1y: float32) (v1x: float32) (v1y: fl
             rotation <- -1.f
     rotation
 
-
 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
     let m2 = -v2x / v2y
@@ -238,7 +294,7 @@ let find (edges: Matrix<byte>)
 
     let rng = Random(42)
 
-    let ellipses = MatchingEllipses(r1)
+    let ellipses = MatchingEllipses(config.RBCRadius.Pixel)
 
     for window_i in -windowSize + increment .. increment .. h - increment do
         for window_j in -windowSize + increment .. increment .. w - increment do
@@ -280,7 +336,8 @@ let find (edges: Matrix<byte>)
                         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
                             | Some (m1, m2) ->
-                                match ellipse p1xf p1yf (float m1) p2xf p2yf (float m2) p3xf p3yf with
+                                //let pouet = ellipse2 p1xf p1yf (float m1) p2xf p2yf (float m2) p3xf p3yf
+                                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 &&
                                               e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance ->
                                      ellipses.Add e