Simplification of the parasite marker.
[master-thesis.git] / Parasitemia / Parasitemia / Classifier.fs
1 module Classifier
2
3 open System
4 open System.Collections.Generic
5 open System.Drawing
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Types
11 open Utils
12
13
14 type private EllipseFlaggedKd (e: Ellipse) =
15 inherit Ellipse (e.Cx, e.Cy, e.A, e.B, e.Alpha)
16
17 member val Removed = false with get, set
18
19 interface KdTree.I2DCoords with
20 member this.X = this.Cx
21 member this.Y = this.Cy
22
23
24 let findCells (ellipses: Ellipse list) (parasites: ParasitesMarker.Result) (img: Image<Gray, byte>) (config: Config.Config) : Cell list =
25 if ellipses.IsEmpty
26 then
27 []
28 else
29 let infection = parasites.infection.Copy() // To avoid to modify the parameter.
30
31 // This is the minimum window size to check if other ellipses touch 'e'.
32 let searchRegion (e: Ellipse) = { KdTree.minX = e.Cx - (e.A + config.RBCMaxRadius) * config.Parameters.scale
33 KdTree.maxX = e.Cx + (e.A + config.RBCMaxRadius) * config.Parameters.scale
34 KdTree.minY = e.Cy - (e.A + config.RBCMaxRadius) * config.Parameters.scale
35 KdTree.maxY = e.Cy + (e.A + config.RBCMaxRadius) * config.Parameters.scale }
36
37 // The minimum window to contain a given ellipse.
38 let ellipseWindow (e: Ellipse) =
39 let cx, cy = roundInt e.Cx, roundInt e.Cy
40 let a = int (e.A + 0.5)
41 cx - a, cy - a, cx + a, cy + a
42
43 let w = img.Width
44 let w_f = float w
45 let h = img.Height
46 let h_f = float h
47
48 // Return 'true' if the point 'p' is owned by e.
49 // The lines represents all intersections with other ellipses.
50 let pixelOwnedByE (p: PointD) (e: Ellipse) (lines: Line list) =
51 e.Contains p.X p.Y &&
52 seq {
53 let c = PointD(e.Cx, e.Cy)
54 for d1 in lines do
55 let d2 = Utils.lineFromTwoPoints c p
56 if d2.Valid
57 then
58 let p' = Utils.pointFromTwoLines d1 d2
59 yield sign (c.X - p.X) <> sign (c.X - p'.X) || Utils.squaredDistanceTwoPoints c p' > Utils.squaredDistanceTwoPoints c p // 'false' -> the point is owned by another ellipse.
60 else
61 yield true
62 } |> Seq.forall id
63
64 let ellipses = ellipses |> List.map EllipseFlaggedKd
65
66 // 1) Associate touching ellipses with each ellipses.
67 let tree = KdTree.Tree.BuildTree ellipses
68 let neighbors (e: Ellipse) : (EllipseFlaggedKd * PointD * PointD) list =
69 tree.Search (searchRegion e)
70 // We only keep the ellipses touching 'e'.
71 |> List.choose (fun otherE ->
72 match EEOver.EEOverlapArea e otherE with
73 | Some (area, px, py) when area > 0.0 && px.Length >= 2 && py.Length >= 2 ->
74 Some (otherE, PointD(px.[0], py.[0]), PointD(px.[1], py.[1]))
75 | _ ->
76 None )
77
78 // We reverse the list to get the lower score ellipses first.
79 let ellipsesWithNeigbors = ellipses |> List.map (fun e -> e, neighbors e) |> List.rev
80
81 // 2) Remove ellipses with a high standard deviation (high contrast).
82 let globalStdDiviation = MathNet.Numerics.Statistics.StreamingStatistics.StandardDeviation(seq {
83 for y in 0 .. h - 1 do
84 for x in 0 .. w - 1 do
85 yield img.Data.[y, x, 0] |> float })
86
87 for e in ellipses do
88 let minX, minY, maxX, maxY = ellipseWindow e
89
90 let stdDiviation = MathNet.Numerics.Statistics.StreamingStatistics.StandardDeviation(seq {
91 for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
92 for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
93 if e.Contains (float x) (float y)
94 then
95 yield img.Data.[y, x, 0] |> float })
96
97 if stdDiviation > globalStdDiviation * config.Parameters.standardDeviationMaxRatio
98 then
99 e.Removed <- true
100
101 // 3) Remove ellipses touching the edges.
102 for e in ellipses do
103 if e.isOutside w_f h_f then e.Removed <- true
104
105 // 4) Remove ellipses with little area.
106 let minArea = config.RBCMinArea
107 for e, neighbors in ellipsesWithNeigbors do
108 if not e.Removed
109 then
110 let minX, minY, maxX, maxY = ellipseWindow e
111
112 let mutable area = 0
113 for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
114 for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
115 let p = PointD(float x, float y)
116 if pixelOwnedByE p e (neighbors |> List.choose (fun (otherE, p1, p2) -> if otherE.Removed then None else Some (Utils.lineFromTwoPoints p1 p2)))
117 then
118 area <- area + 1
119
120 if area < int minArea
121 then
122 e.Removed <- true
123
124 // 5) Define pixels associated to each ellipse and create the cells.
125 ellipsesWithNeigbors
126 |> List.choose (fun (e, neighbors) ->
127 if e.Removed
128 then
129 None
130 else
131 let minX, minY, maxX, maxY = ellipseWindow e
132
133 let infectedPixels = List<Point>()
134 let mutable stainPixels = 0
135 let mutable darkStainPixels = 0
136 let mutable nbElement = 0
137
138 let elements = new Matrix<byte>(maxY - minY + 1, maxX - minX + 1)
139 for y in minY .. maxY do
140 for x in minX .. maxX do
141 let p = PointD(float x, float y)
142 if pixelOwnedByE p e (neighbors |> List.choose (fun (otherE, p1, p2) -> if otherE.Removed then None else Some (Utils.lineFromTwoPoints p1 p2)))
143 then
144 elements.[y-minY, x-minX] <- 1uy
145 nbElement <- nbElement + 1
146
147 if infection.Data.[y, x, 0] > 0uy
148 then
149 infectedPixels.Add(Point(x, y))
150
151 if parasites.stain.Data.[y, x, 0] > 0uy
152 then
153 stainPixels <- stainPixels + 1
154
155 if parasites.darkStain.Data.[y, x, 0] > 0uy
156 then
157 darkStainPixels <- darkStainPixels + 1
158
159 let cellClass =
160 if float darkStainPixels > config.Parameters.maxDarkStainRatio * (float nbElement) ||
161 float stainPixels > config.Parameters.maxStainRatio * (float nbElement) (* ||
162 sqrt (((float sumCoords_x) / (float nbElement) - e.Cx) ** 2.0 + ((float sumCoords_y) / (float nbElement) - e.Cy) ** 2.0) > e.A * config.maxOffcenter *)
163 then
164 Peculiar
165 elif infectedPixels.Count >= 1
166 then
167 let infectionToRemove = ImgTools.connectedComponents parasites.stain infectedPixels
168 for p in infectionToRemove do
169 infection.Data.[p.Y, p.X, 0] <- 0uy
170 InfectedRBC
171 else
172 HealthyRBC
173
174 Some { cellClass = cellClass
175 center = Point(roundInt e.Cx, roundInt e.Cy)
176 elements = elements })