Update coding style.
[master-thesis.git] / Parasitemia / ParasitemiaCore / MatchingEllipses.fs
1 module ParasitemiaCore.MatchingEllipses
2
3 open System
4 open System.Drawing
5 open System.Linq
6 open System.Collections
7 open System.Collections.Generic
8
9 open Types
10 open Utils
11
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
18
19 type private EllipseScoreFlaggedKd (matchingScore : float32, e : Ellipse) =
20 let mutable matchingScore = matchingScore
21
22 member this.Ellipse = e
23
24 member this.MatchingScore = matchingScore
25
26 member this.AddMatchingScore (score : float32) =
27 matchingScore <- matchingScore + score
28
29 member val Processed = false with get, set
30 member val Removed = false with get, set
31
32 interface KdTree.I2DCoords with
33 member this.X = this.Ellipse.Cx
34 member this.Y = this.Ellipse.Cy
35
36 type MatchingEllipses (radius : float32) =
37 let ellipses = List<EllipseScoreFlaggedKd> ()
38
39 member this.Add (e : Ellipse) =
40 ellipses.Add (EllipseScoreFlaggedKd (0.f, e))
41
42 member this.Ellipses : Ellipse list =
43 List.ofSeq ellipses |> List.map (fun e -> e.Ellipse)
44
45 member this.PrunedEllipses : Ellipse list =
46 if ellipses.Count = 0 then
47 []
48 else
49 // 1) Create a kd-tree from the ellipses list.
50 let tree = KdTree.Tree.BuildTree (List.ofSeq ellipses)
51
52 // 2) Compute the matching score of each ellipses.
53 let windowSize = radius * windowSizeRadiusFactor
54 for e in ellipses do
55 e.Processed <- true
56 let areaE = e.Ellipse.Area
57 let window =
58 {
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
63 }
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
74 | _ -> ()
75
76 // 3) Remove ellipses whose center is near the center of another ellipse with a better score.
77 let matchingScoreThreshold = matchingScoreThresholdPerRadiusUnit * radius
78 for e in ellipses do
79 if e.MatchingScore < matchingScoreThreshold then
80 e.Removed <- true
81 else
82 let window =
83 {
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
88 }
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
93 other.Removed <- true
94 else
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 ->
98 other.Removed <- true
99 | _ ->
100 ()
101 ellipses
102 |> List.ofSeq
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)
106