Change the parasite detection method.
[master-thesis.git] / Parasitemia / Parasitemia / Ellipse.fs
index 128c823..33333bf 100644 (file)
@@ -46,8 +46,8 @@ let private goldenSectionSearch (f: float -> float) (nbIter: int) (xmin: float)
 // Ellipse.A is always equal or greater than Ellipse.B.
 // Ellipse.Alpha is between 0 and Pi.
 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 = 4
-    let accuracy_extremum_search_2 = 3
+    let accuracy_extremum_search_1 = 8 // 3
+    let accuracy_extremum_search_2 = 8 // 4
 
     // p3 as the referencial.
     let p1x = p1x - p3x
@@ -178,26 +178,39 @@ let private areVectorsValid (p1x: float) (p1y: float) (p2x: float) (p2y: float)
 
     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 px = -((b1 - b2) / (m1 - m2))
+    let py = -((m2 * b1 - m1 * b2) / (m1 - m2))
 
     let rot1 = vectorRotation p1x p1y v1x v1y px py
     let rot2 = vectorRotation p2x p2y v2x v2y px py
 
-    if rot1 = rot2 || rot1 * atan2 (p1y - py) (p1x - px) + rot2 * atan2 (p2y - py) (p2x - px) <= 0.0
+    if rot1 = rot2
     then
         None
     else
+        let alpha1 = atan2 (p1y - py) (p1x - px)
+        let alpha2 = atan2 (p2y - py) (p2x - px)
+
+        let alpha1' = if alpha1 < 0.0 then 2.0 * Math.PI + alpha1 else alpha1
+        let alpha2' = if alpha2 < 0.0 then 2.0 * Math.PI + alpha2 else alpha2
+
+        let diff = rot1 * alpha1' + rot2 * alpha2'
+
+        if diff > Math.PI || (diff < 0.0 && diff > -Math.PI)
+        then
+            None
+        else
         Some (m1, m2)
 
+
 let find (edges: Matrix<byte>)
          (xDir: Image<Gray, float>)
          (yDir: Image<Gray, float>)
          (config: Config) : MatchingEllipses =
 
-    let r1, r2 = config.scale * config.minRBCSize, config.scale * config.maxRBCSize
-    let windowSize = roundInt (config.factorWindowSize * r2)
-    let factorNbPick = config.factorNbPick
+    let r1, r2 = config.Parameters.scale * config.RBCMin, config.Parameters.scale * config.RBCMax // FIXME: scale factor should be applied in Config!?
+    let windowSize = roundInt (config.Parameters.factorWindowSize * r2)
+    let factorNbPick = config.Parameters.factorNbPick
 
     let increment = windowSize / 4
 
@@ -262,6 +275,8 @@ let find (edges: Matrix<byte>)
                                 match ellipse p1xf p1yf m1 p2xf p2yf m2 p3xf p3yf with
                                 | Some e when e.Cx > 0.0 && e.Cx < (float w) - 1.0 && e.Cy > 0.0 && e.Cy < (float h) - 1.0 &&
                                               e.A >= r1 - radiusTolerance && e.A <= r2 + radiusTolerance && e.B >= r1 - radiusTolerance && e.B <= r2 + radiusTolerance ->
+
+                                     let prout = areVectorsValid p1xf p1yf p2xf p2yf -xDirData.[p1y, p1x, 0] -yDirData.[p1y, p1x, 0] -xDirData.[p2y, p2x, 0] -yDirData.[p2y, p2x, 0]
                                      ellipses.Add e
                                 | _ -> ()
                             | _ -> ()