X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FEllipse.fs;h=5a7df2b3256581de2032cfeadf594c30a54c536f;hb=HEAD;hp=b39ab4d61d46c11d249c3a264346f6bb016fb876;hpb=1b8e45987bde692ab5602c281f878707f70459b7;p=master-thesis.git
diff --git a/Parasitemia/ParasitemiaCore/Ellipse.fs b/Parasitemia/ParasitemiaCore/Ellipse.fs
index b39ab4d..5a7df2b 100644
--- a/Parasitemia/ParasitemiaCore/Ellipse.fs
+++ b/Parasitemia/ParasitemiaCore/Ellipse.fs
@@ -7,7 +7,6 @@ open System.Drawing
open MathNet.Numerics.LinearAlgebra
open Emgu.CV
-open Emgu.CV.Structure
open Utils
open Config
@@ -22,21 +21,25 @@ let minimumDistanceBetweenDrawnPoints = 0.6
/// '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 p0 = pointFromTwoLines (Types.Line(float32 m1, float32 (p1y - m1 * p1x))) (Types.Line(float32 m2, float32(p2y - m2 * p2x)))
+let ellipse (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 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 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]
@@ -49,18 +52,16 @@ let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2:
let cy = d / a
let at = c * f - e ** 2. + (e * d - b * f) * cx + (b * e - c * d) * cy
- if at = 0.
- then
+ 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 eigen = q.Evd ()
let eigenValues = eigen.EigenValues
let lambda = eigenValues.[1].Real
let mu = eigenValues.[0].Real
- if lambda <= 0. || mu <= 0.
- then
+ if lambda <= 0. || mu <= 0. then
None
else
let r1, r2 = 1. / (sqrt lambda), 1. / (sqrt mu)
@@ -75,22 +76,24 @@ let ellipse (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2:
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'))
+ Some (Types.Ellipse (float32 cx, float32 cy, float32 majorAxis, float32 minorAxis, float32 phi'))
-let inline private vectorRotation (px: float32) (py: float32) (vx: float32) (vy: float32) (p0x: float32) (p0y: float32) : float32 =
- if py > p0y
- then
+
+// Optimized version of 'ellipse': TODO
+let ellipse' (p1x : float) (p1y : float) (m1 : float) (p2x : float) (p2y : float) (m2 : float) (p3x : float) (p3y : float) : Types.Ellipse option =
+ ellipse p1x p1y m1 p2x p2y m2 p3x p3y
+
+let inline private vectorRotation (px : float32) (py : float32) (vx : float32) (vy : float32) (p0x : float32) (p0y : float32) : float32 =
+ if py > p0y then
if vx > 0.f then -1.f else 1.f
- elif py < p0y
- then
+ elif py < p0y then
if vx < 0.f then -1.f else 1.f
- elif px > p0x
- then
+ elif px > p0x then
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 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
@@ -102,8 +105,7 @@ let private areVectorsValid (p1x: float32) (p1y: float32) (p2x: float32) (p2y: f
let rot1 = vectorRotation p1x p1y v1x v1y p0x p0y
let rot2 = vectorRotation p2x p2y v2x v2y p0x p0y
- if rot1 = rot2
- then
+ if rot1 = rot2 then
None
else
let alpha1 =
@@ -116,8 +118,7 @@ let private areVectorsValid (p1x: float32) (p1y: float32) (p2x: float32) (p2y: f
let diff = rot1 * alpha1 + rot2 * alpha2
- if diff > PI || (diff < 0.f && diff > -PI)
- then
+ if diff > PI || (diff < 0.f && diff > -PI) then
Some (m1, m2)
else
None
@@ -125,10 +126,10 @@ let private areVectorsValid (p1x: float32) (p1y: float32) (p2x: float32) (p2y: f
///
/// Build a set of ellipses as a 'MatchingEllipses' object by finding ellipses with the given edges and gradient.
///
-let find (edges: Matrix)
- (xGradient: Matrix)
- (yGradient: Matrix)
- (config: Config) : MatchingEllipses =
+let find (edges : Matrix)
+ (xGradient : Matrix)
+ (yGradient : Matrix)
+ (config : Config) : MatchingEllipses =
let r1, r2 = config.RBCRadius.Min, config.RBCRadius.Max
let incrementWindowDivisor = 4.f
@@ -153,15 +154,15 @@ let find (edges: Matrix)
let mutable last_i, last_j = Int32.MaxValue, Int32.MaxValue
- let currentElements = List()
+ let currentElements = List ()
let edgesData = edges.Data
let xDirData = xGradient.Data
let yDirData = yGradient.Data
- let rng = Random(42)
+ let rng = Random 42
- let ellipses = MatchingEllipses(config.RBCRadius.Pixel)
+ 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
@@ -172,28 +173,26 @@ let find (edges: Matrix)
let window_j_end = if window_j + windowSize - 1 >= w then w - 1 else window_j + windowSize - 1
// Remove old elements.
- let indexFirstElement = currentElements.FindIndex(fun p -> p.X >= window_j_begin)
- if indexFirstElement > 0
- then currentElements.RemoveRange(0, indexFirstElement)
+ let indexFirstElement = currentElements.FindIndex (fun p -> p.X >= window_j_begin)
+ if indexFirstElement > 0 then
+ currentElements.RemoveRange (0, indexFirstElement)
// Add the new elements.
let newElemsBegin_j = window_j + windowSize - increment
let newElemsEnd_j = window_j + windowSize - 1
for j = (if newElemsBegin_j < 0 then 0 else newElemsBegin_j) to (if newElemsEnd_j >= w then w - 1 else newElemsEnd_j) do
for i = window_i_begin to window_i_end do
- if edgesData.[i, j] = 1uy
- then currentElements.Add(Point(j, i))
+ if edgesData.[i, j] = 1uy then
+ currentElements.Add (Point (j, i))
- if currentElements.Count >= nbPickElementsMin
- then
+ if currentElements.Count >= nbPickElementsMin then
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)]
- if p1 <> p2 && p1 <> p3 && p2 <> p3
- then
+ let p1 = currentElements.[rng.Next currentElements.Count]
+ let p2 = currentElements.[rng.Next currentElements.Count]
+ let p3 = currentElements.[rng.Next currentElements.Count]
+ if p1 <> p2 && p1 <> p3 && p2 <> p3 then
nbOfPicks <- nbOfPicks - 1
let p1yf, p1xf = float p1.Y, float p1.X
let p2yf, p2xf = float p2.Y, float p2.X
@@ -212,7 +211,7 @@ let find (edges: Matrix)
| _ -> ()
| _ -> ()
- currentElements.Clear()
+ currentElements.Clear ()
ellipses