1
module MatchingEllipses
5 open System.Collections
6 open System.Collections.Generic
12 let matchingScoreThreshold1 = 0.6
13 let matchingScoreThreshold2 = 1.
15 type private EllipseScoreFlaggedKd (matchingScore
: float, e
: Ellipse) =
16 let mutable matchingScore = matchingScore
18 member this
.Ellipse = e
20 member this
.MatchingScore = matchingScore
21 member this
.AddMatchingScore(score
: float) =
22 matchingScore <- matchingScore + score
24 member val Processed = false with get
, set
25 member val Removed = false with get
, set
27 interface KdTree.I2DCoords with
28 member this
.X = this
.Ellipse.Cx
29 member this
.Y = this
.Ellipse.Cy
32 type MatchingEllipses (radiusMin
: float) =
33 let ellipses = List<EllipseScoreFlaggedKd>()
35 member this
.Add (e
: Ellipse) =
36 ellipses.Add(EllipseScoreFlaggedKd(0.0, e
))
38 member this
.Ellipses : Ellipse list =
39 List.ofSeq
ellipses |> List.map
(fun e
-> e
.Ellipse)
41 member this
.PrunedEllipses : Ellipse list =
46 // 1) Create a kd-tree from the ellipses list.
47 let tree = KdTree.Tree.BuildTree (List.ofSeq
ellipses)
49 // 2) Compute the matching score of each ellipses.
50 let windowSize = radiusMin
53 let areaE = e
.Ellipse.Area
54 let window = { KdTree.minX
= e
.Ellipse.Cx - windowSize / 2.0
55 KdTree.maxX
= e
.Ellipse.Cx + windowSize / 2.0
56 KdTree.minY
= e
.Ellipse.Cy - windowSize / 2.0
57 KdTree.maxY
= e
.Ellipse.Cy + windowSize / 2.0 }
58 for other
in tree.Search window do
59 if not
other.Processed
61 let areaOther = other.Ellipse.Area
62 match EEOver.EEOverlapArea e
.Ellipse other.Ellipse with
63 | Some (commonArea
, _
, _
) ->
64 let matchingScore = 2.0 * commonArea
/ (areaE + areaOther)
65 if matchingScore >= matchingScoreThreshold1
67 other.AddMatchingScore(matchingScore)
68 e
.AddMatchingScore(matchingScore)
71 // 3) Sort ellipses by their score.
72 ellipses.Sort(fun e1 e2
-> e2
.MatchingScore.CompareTo(e1
.MatchingScore))
74 // 4) Remove ellipses wich have a low score.
75 let i = ellipses.BinarySearch(EllipseScoreFlaggedKd(matchingScoreThreshold2, Ellipse(0.0, 0.0, 0.0, 0.0, 0.0)),
76 { new IComparer<EllipseScoreFlaggedKd> with
77 member this
.Compare(e1
, e2
) = e2
.MatchingScore.CompareTo(e1
.MatchingScore) }) |> abs
78 let nbToRemove = ellipses.Count - i
81 for j
in i .. nbToRemove - 1 do
82 ellipses.[j
].Removed <- true
83 ellipses.RemoveRange(i, nbToRemove)
85 // 5) Remove ellipses whose center is into an ellipse with a better score
89 let window = { KdTree.minX
= e.Ellipse.Cx - e.Ellipse.A
90 KdTree.maxX
= e.Ellipse.Cx + e.Ellipse.A
91 KdTree.minY
= e.Ellipse.Cy - e.Ellipse.A
92 KdTree.maxY
= e.Ellipse.Cy + e.Ellipse.A }
93 for other in tree.Search window do
94 if not
other.Removed && other.MatchingScore < e.MatchingScore
96 if e.Ellipse.Contains other.Ellipse.Cx other.Ellipse.Cy
99 ellipses.RemoveAll(fun e -> e.Removed) |> ignore
101 List.ofSeq
ellipses |> List.map
(fun e -> e.Ellipse)