1
module ParasitemiaCore.MatchingEllipses
6 open System.Collections
7 open System.Collections.Generic
12 // All ellipses with a score below this are removed.
13 let matchingScoreThresholdPerRadiusUnit = 0.07f // For a radius of 1. // 0.25
14 let matchingScorePower = 20.f
15 let windowSizeRadiusFactor = 1.f
/ 2.f
16 let minimumDistanceFromCenterRadiusFactor = 1.f
/ 3.f
18 type private EllipseScoreFlaggedKd (matchingScore
: float32
, e
: Ellipse) =
19 let mutable matchingScore = matchingScore
21 member this
.Ellipse = e
23 member this
.MatchingScore = matchingScore
25 member this
.AddMatchingScore (score
: float32
) =
26 matchingScore <- matchingScore + score
28 member val Processed = false with get
, set
29 member val Removed = false with get
, set
31 interface KdTree.I2DCoords with
32 member this
.X = this
.Ellipse.Cx
33 member this
.Y = this
.Ellipse.Cy
35 type MatchingEllipses (radius
: float32
) =
36 let ellipses = List<EllipseScoreFlaggedKd>()
38 member this
.Add (e
: Ellipse) =
39 ellipses.Add(EllipseScoreFlaggedKd(0.f
, e
))
41 member this
.Ellipses : Ellipse list =
42 List.ofSeq
ellipses |> List.map
(fun e
-> e
.Ellipse)
44 member this
.PrunedEllipses : Ellipse list =
49 // 1) Create a kd-tree from the ellipses list.
50 let tree = KdTree.Tree.BuildTree (List.ofSeq
ellipses)
52 // 2) Compute the matching score of each ellipses.
53 let windowSize = radius
* windowSizeRadiusFactor
56 let areaE = e
.Ellipse.Area
57 let window = { KdTree.minX
= e
.Ellipse.Cx - windowSize / 2.f
58 KdTree.maxX
= e
.Ellipse.Cx + windowSize / 2.f
59 KdTree.minY
= e
.Ellipse.Cy - windowSize / 2.f
60 KdTree.maxY
= e
.Ellipse.Cy + windowSize / 2.f
}
61 for other
in tree.Search window do
62 if not
other.Processed
64 let areaOther = other.Ellipse.Area
65 match EEOver.EEOverlapArea e
.Ellipse other.Ellipse with
66 | Some (overlapArea
, _
, _
)
67 // Because of approximation error, see https://github.com/chraibi/EEOver/issues/4
68 when overlapArea
- areaE < 1.f
&& overlapArea
- areaOther < 1.f
->
69 let matchingScore = (2.f
* overlapArea
/ (areaE + areaOther)) ** matchingScorePower
70 other.AddMatchingScore(matchingScore)
71 e
.AddMatchingScore(matchingScore)
74 // 3) Remove ellipses whose center is near the center of another ellipse with a better score.
75 let matchingScoreThreshold = matchingScoreThresholdPerRadiusUnit * radius
77 if e
.MatchingScore < matchingScoreThreshold
81 let window = { KdTree.minX
= e
.Ellipse.Cx - e
.Ellipse.A
82 KdTree.maxX
= e
.Ellipse.Cx + e
.Ellipse.A
83 KdTree.minY
= e
.Ellipse.Cy - e
.Ellipse.A
84 KdTree.maxY
= e
.Ellipse.Cy + e
.Ellipse.A }
85 for other in tree.Search window do
86 if not
other.Removed && e
.MatchingScore > other.MatchingScore
88 // Case where ellipses are too close.
89 if distanceTwoPoints
(PointF(e
.Ellipse.Cx, e
.Ellipse.Cy)) (PointF(other.Ellipse.Cx, other.Ellipse.Cy)) < minimumDistanceFromCenterRadiusFactor * e
.Ellipse.B
93 // Case where ellipses are overlapped.
94 match EEOver.EEOverlapArea e
.Ellipse other.Ellipse with
95 | Some (overlapArea
, _, _) when e
.Ellipse.Area < 1.1f * overlapArea
|| other.Ellipse.Area < 1.1f * overlapArea
->
101 |> List.filter
(fun e
-> not
e.Removed)
102 |> List.sortWith
(fun e1 e2
-> e2
.MatchingScore.CompareTo(e1
.MatchingScore))
103 |> List.map
(fun e -> e.Ellipse)