More homogeneous ellipse density.
[master-thesis.git] / Parasitemia / ParasitemiaCore / Ellipse.fs
index 4e48230..cf55a8e 100644 (file)
@@ -14,6 +14,9 @@ open Config
 open MatchingEllipses
 open Const
 
+// 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.
@@ -76,28 +79,17 @@ let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2:
 
 
 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
+        if v1x > 0.f then -1.f else 1.f
     elif p1y < py
     then
-        if v1x < 0.f
-        then
-            rotation <- -1.f
+        if v1x < 0.f then -1.f else 1.f
     elif p1x > px
     then
-        if v1y < 0.f
-        then
-            rotation <- -1.f
-    elif p1x < px
-    then
-        if v1y > 0.f
-        then
-            rotation <- -1.f
-    rotation
+        if v1y < 0.f then -1.f else 1.f
+    else // p1x < px
+        if v1y > 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
@@ -125,9 +117,9 @@ let private areVectorsValid (p1x: float32) (p1y: float32) (p2x: float32) (p2y: f
 
         if diff > PI || (diff < 0.f && diff > -PI)
         then
-            None
+            Some (m1, m2)
         else
-        Some (m1, m2)
+            None
 
 let find (edges: Matrix<byte>)
          (xGradient: Matrix<float32>)
@@ -139,13 +131,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
@@ -188,8 +182,9 @@ let find (edges: Matrix<byte>)
 
             if currentElements.Count >= 10
             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)]
@@ -208,7 +203,8 @@ let find (edges: Matrix<byte>)
                                 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
                                 | _ -> ()
                             | _ -> ()