More homogeneous ellipse density.
[master-thesis.git] / Parasitemia / ParasitemiaCore / Ellipse.fs
index db23ece..cf55a8e 100644 (file)
@@ -131,7 +131,9 @@ 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)
 
@@ -180,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)]
@@ -200,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
                                 | _ -> ()
                             | _ -> ()