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
// Used when searching for neighbor ellipses.
16 let minimumDistanceFromCenterRadiusFactor = 1.f
/ 3.f
17 let minimumAreaFactor = 1.1f
19 type private EllipseScoreFlaggedKd (matchingScore
: float32
, e
: Ellipse) =
20 let mutable matchingScore = matchingScore
22 member this
.Ellipse = e
24 member this
.MatchingScore = matchingScore
26 member this
.AddMatchingScore (score
: float32
) =
27 matchingScore <- matchingScore + score
29 member val Processed = false with get
, set
30 member val Removed = false with get
, set
32 interface KdTree.I2DCoords with
33 member this
.X = this
.Ellipse.Cx
34 member this
.Y = this
.Ellipse.Cy
36 type MatchingEllipses (radius
: float32
) =
37 let ellipses = List<EllipseScoreFlaggedKd>()
39 member this
.Add (e
: Ellipse) =
40 ellipses.Add(EllipseScoreFlaggedKd(0.f
, e
))
42 member this
.Ellipses : Ellipse list =
43 List.ofSeq
ellipses |> List.map
(fun e
-> e
.Ellipse)
45 member this
.PrunedEllipses : Ellipse list =
46 if ellipses.Count = 0 then
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
59 KdTree.minX
= e
.Ellipse.Cx - windowSize / 2.f
60 KdTree.maxX
= e
.Ellipse.Cx + windowSize / 2.f
61 KdTree.minY
= e
.Ellipse.Cy - windowSize / 2.f
62 KdTree.maxY
= e
.Ellipse.Cy + windowSize / 2.f
64 for other
in tree.Search window do
65 if not
other.Processed then
66 let areaOther = other.Ellipse.Area
67 match EEOver.EEOverlapArea e
.Ellipse other.Ellipse with
68 | Some (overlapArea
, _
, _
)
69 // Because of approximation error, see https://github.com/chraibi/EEOver/issues/4
70 when overlapArea
- areaE < 1.f
&& overlapArea
- areaOther < 1.f
->
71 let matchingScore = (2.f
* overlapArea
/ (areaE + areaOther)) ** matchingScorePower
72 other.AddMatchingScore(matchingScore)
73 e
.AddMatchingScore(matchingScore)
76 // 3) Remove ellipses whose center is near the center of another ellipse with a better score.
77 let matchingScoreThreshold = matchingScoreThresholdPerRadiusUnit * radius
79 if e
.MatchingScore < matchingScoreThreshold then
84 KdTree.minX
= e
.Ellipse.Cx - e
.Ellipse.A
85 KdTree.maxX
= e
.Ellipse.Cx + e
.Ellipse.A
86 KdTree.minY
= e
.Ellipse.Cy - e
.Ellipse.A
87 KdTree.maxY
= e
.Ellipse.Cy + e
.Ellipse.A
89 for other in tree.Search window do
90 if not
other.Removed && e
.MatchingScore > other.MatchingScore then
91 // Case where ellipses are too close.
92 if distanceTwoPoints
(PointF(e
.Ellipse.Cx, e
.Ellipse.Cy)) (PointF(other.Ellipse.Cx, other.Ellipse.Cy)) < minimumDistanceFromCenterRadiusFactor * e
.Ellipse.B then
95 // Case where ellipses are overlapped.
96 match EEOver.EEOverlapArea e
.Ellipse other.Ellipse with
97 | Some (overlapArea
, _, _) when e
.Ellipse.Area < minimumAreaFactor * overlapArea
|| other.Ellipse.Area < minimumAreaFactor * overlapArea
->
103 |> List.filter
(fun e
-> not
e.Removed)
104 |> List.sortWith
(fun e1 e2
-> e2
.MatchingScore.CompareTo(e1
.MatchingScore))
105 |> List.map
(fun e -> e.Ellipse)