Change the parasite detection method.
[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: ParasitesMarker2.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.RBCMax) * config.Parameters.scale
33 KdTree.maxX = e.Cx + (e.A + config.RBCMax) * config.Parameters.scale
34 KdTree.minY = e.Cy - (e.A + config.RBCMax) * config.Parameters.scale
35 KdTree.maxY = e.Cy + (e.A + config.RBCMax) * 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 let ellipses = ellipses |> List.map EllipseFlaggedKd
49
50 // 1) Associate touching ellipses with each ellipses.
51 let tree = KdTree.Tree.BuildTree ellipses
52 let neighbors (e: Ellipse) : (EllipseFlaggedKd * PointD * PointD) list =
53 tree.Search (searchRegion e)
54 // We only keep the ellipses touching 'e'.
55 |> List.choose (fun otherE ->
56 match EEOver.EEOverlapArea e otherE with
57 | Some (area, px, py) when area > 0.0 && px.Length >= 2 && py.Length >= 2 ->
58 Some (otherE, PointD(px.[0], py.[0]), PointD(px.[1], py.[1]))
59 | _ ->
60 None )
61
62 let ellipsesWithNeigbors = ellipses |> List.choose (fun e -> if e.Removed then None else Some (e, neighbors e))
63
64 // 2) Remove ellipses with a lower percentage of foreground. (taken from the lower score to the highest).
65 (*for e, neighbors in List.rev ellipsesWithNeigbors do
66 let minX, minY, maxX, maxY = ellipseWindow e
67
68 let mutable totalElement = 0
69 let mutable fgElement = 0
70 for y in (if minY < 0 then 0 else minY) .. (if maxY >= fg.Height then fg.Height - 1 else maxY) do
71 for x in (if minX < 0 then 0 else minX) .. (if maxX >= fg.Width then fg.Width - 1 else maxX) do
72 let yf, xf = float y, float x
73 if e.Contains xf yf && neighbors |> List.forall (fun (otherE, _, _) -> otherE.Removed || not <| otherE.Contains xf yf)
74 then
75 totalElement <- totalElement + 1
76 if fg.Data.[y, x, 0] > 0uy
77 then
78 fgElement <- fgElement + 1
79
80 if (float fgElement) / (float totalElement) < config.Parameters.percentageOfFgValidCell
81 then
82 e.Removed <- true*)
83
84 // 3) Remove ellipses touching the edges.
85 for e in ellipses do
86 if e.isOutside w_f h_f then e.Removed <- true
87
88 // 4) Remove ellipses with little area.
89 for e, neighbors in ellipsesWithNeigbors do
90 if not e.Removed
91 then
92 let minX, minY, maxX, maxY = ellipseWindow e
93
94 let mutable area = 0
95 for y in (if minY < 0 then 0 else minY) .. (if maxY >= h then h - 1 else maxY) do
96 for x in (if minX < 0 then 0 else minX) .. (if maxX >= w then w - 1 else maxX) do
97 let yf, xf = float y, float x
98 if e.Contains xf yf &&
99 neighbors |> List.forall (fun (otherE, _, _) -> otherE.Removed || not <| otherE.Contains xf yf)
100 then
101 area <- area + 1
102
103 if area < int config.RBCMinArea
104 then
105 e.Removed <- true
106
107 // 5) Define pixels associated to each ellipse and create the cells.
108
109 // Return 'true' if the point 'p' is owned by e.
110 // The lines represents all intersections with other ellipses.
111 let pixelOwnedByE (p: PointD) (e: Ellipse) (lines: Line list) =
112 e.Contains p.X p.Y &&
113 seq {
114 let c = PointD(e.Cx, e.Cy)
115 for d1 in lines do
116 let d2 = Utils.lineFromTwoPoints c p
117 let p' = Utils.pointFromTwoLines d1 d2
118 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.
119 } |> Seq.forall id
120
121 ellipsesWithNeigbors
122 |> List.choose (fun (e, neighbors) ->
123 if e.Removed
124 then
125 None
126 else
127 let minX, minY, maxX, maxY = ellipseWindow e
128
129 let infectedPixels = List<Point>()
130 let mutable stainPixels = 0
131 let mutable darkStainPixels = 0
132 let mutable nbElement = 0
133
134 let mutable stain_x = 0.0
135 let mutable stain_x2 = 0.0
136 let mutable stain_y = 0.0
137 let mutable stain_y2 = 0.0
138
139 let mutable sumCoords_y = 0
140 let mutable sumCoords_x = 0
141
142 let elements = new Matrix<byte>(maxY - minY + 1, maxX - minX + 1)
143 for y in minY .. maxY do
144 for x in minX .. maxX do
145 let p = PointD(float x, float y)
146 if pixelOwnedByE p e (neighbors |> List.choose (fun (otherE, p1, p2) -> if otherE.Removed then None else Some (Utils.lineFromTwoPoints p1 p2)))
147 then
148 elements.[y-minY, x-minX] <- 1uy
149 nbElement <- nbElement + 1
150 sumCoords_y <- sumCoords_y + y
151 sumCoords_x <- sumCoords_x + x
152
153 if infection.Data.[y, x, 0] > 0uy
154 then
155 infectedPixels.Add(Point(x, y))
156
157 if parasites.stain.Data.[y, x, 0] > 0uy
158 then
159 stainPixels <- stainPixels + 1
160 stain_x <- stain_x + (float x)
161 stain_x2 <- stain_x2 + (float x) ** 2.0
162 stain_y <- stain_y + (float y)
163 stain_y2 <- stain_y2 + (float y) ** 2.0
164
165 if parasites.darkStain.Data.[y, x, 0] > 0uy
166 then
167 darkStainPixels <- darkStainPixels + 1
168
169 let cellClass =
170 if float darkStainPixels > config.Parameters.maxDarkStainRatio * (float nbElement) (* ||
171 sqrt (((float sumCoords_x) / (float nbElement) - e.Cx) ** 2.0 + ((float sumCoords_y) / (float nbElement) - e.Cy) ** 2.0) > e.A * config.maxOffcenter *)
172 then
173 Peculiar
174 elif infectedPixels.Count > config.Parameters.infectionPixelsRequired
175 then
176 let infectionToRemove = ImgTools.connectedComponents parasites.stain infectedPixels
177 for p in infectionToRemove do
178 infection.Data.[p.Y, p.X, 0] <- 0uy
179 InfectedRBC
180 else
181 HealthyRBC
182
183 Some { cellClass = cellClass
184 center = Point(roundInt e.Cx, roundInt e.Cy)
185 elements = elements })