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