* Remove ellipses with too small area (improved).
[master-thesis.git] / Parasitemia / Parasitemia / ImgTools.fs
1 module ImgTools
2
3 open System
4 open System.Drawing
5 open System.Collections.Generic
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Utils
11
12 // Normalize image values between 0uy and 255uy.
13 let normalizeAndConvert (img: Image<Gray, float32>) : Image<Gray, byte> =
14 let min = ref [| 0.0 |]
15 let minLocation = ref <| [| Point() |]
16 let max = ref [| 0.0 |]
17 let maxLocation = ref <| [| Point() |]
18 img.MinMax(min, max, minLocation, maxLocation)
19 ((img - (!min).[0]) / ((!max).[0] - (!min).[0]) * 255.0).Convert<Gray, byte>()
20
21 let gaussianFilter (img : Image<'TColor, 'TDepth>) (standardDeviation : float) : Image<'TColor, 'TDepth> =
22 let size = 2 * int (ceil (4.0 * standardDeviation)) + 1
23 img.SmoothGaussian(size, size, standardDeviation, standardDeviation)
24
25 // Zhang and Suen algorithm.
26 // Modify 'mat' in place.
27 let thin (mat: Matrix<byte>) =
28 let w = mat.Width
29 let h = mat.Height
30 let mutable data1 = mat.Data
31 let mutable data2 = Array2D.copy data1
32
33 let mutable pixelChanged = true
34 let mutable oddIteration = true
35
36 while pixelChanged do
37 pixelChanged <- false
38 for i in 0..h-1 do
39 for j in 0..w-1 do
40 if data1.[i, j] = 1uy
41 then
42 let p2 = if i = 0 then 0uy else data1.[i-1, j]
43 let p3 = if i = 0 || j = w-1 then 0uy else data1.[i-1, j+1]
44 let p4 = if j = w-1 then 0uy else data1.[i, j+1]
45 let p5 = if i = h-1 || j = w-1 then 0uy else data1.[i+1, j+1]
46 let p6 = if i = h-1 then 0uy else data1.[i+1, j]
47 let p7 = if i = h-1 || j = 0 then 0uy else data1.[i+1, j-1]
48 let p8 = if j = 0 then 0uy else data1.[i, j-1]
49 let p9 = if i = 0 || j = 0 then 0uy else data1.[i-1, j-1]
50
51 let sumNeighbors = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9
52 if sumNeighbors >= 2uy && sumNeighbors <= 6uy &&
53 (if p2 = 0uy && p3 = 1uy then 1 else 0) +
54 (if p3 = 0uy && p4 = 1uy then 1 else 0) +
55 (if p4 = 0uy && p5 = 1uy then 1 else 0) +
56 (if p5 = 0uy && p6 = 1uy then 1 else 0) +
57 (if p6 = 0uy && p7 = 1uy then 1 else 0) +
58 (if p7 = 0uy && p8 = 1uy then 1 else 0) +
59 (if p8 = 0uy && p9 = 1uy then 1 else 0) +
60 (if p9 = 0uy && p2 = 1uy then 1 else 0) = 1 &&
61 if oddIteration
62 then p2 * p4 * p6 = 0uy && p4 * p6 * p8 = 0uy
63 else p2 * p4 * p8 = 0uy && p2 * p6 * p8 = 0uy
64 then
65 data2.[i, j] <- 0uy
66 pixelChanged <- true
67 else
68 data2.[i, j] <- 0uy
69
70 oddIteration <- not oddIteration
71 let tmp = data1
72 data1 <- data2
73 data2 <- tmp
74
75
76 let pop (l: List<'a>) : 'a =
77 let n = l.[l.Count - 1]
78 l.RemoveAt(l.Count - 1)
79 n
80
81 // Remove all 8-connected pixels with an area equal or greater than 'areaSize'.
82 // Modify 'mat' in place.
83 let removeArea (mat: Matrix<byte>) (areaSize: int) =
84 let neighbors = [|
85 (-1, 0) // p2
86 (-1, 1) // p3
87 ( 0, 1) // p4
88 ( 1, 1) // p5
89 ( 1, 0) // p6
90 ( 1, -1) // p7
91 ( 0, -1) // p8
92 (-1, -1) |] // p9
93
94 let mat' = new Matrix<byte>(mat.Size)
95 let w = mat'.Width
96 let h = mat'.Height
97 mat.CopyTo(mat')
98
99 let data = mat.Data
100 let data' = mat'.Data
101
102 for i in 0..h-1 do
103 for j in 0..w-1 do
104 if data'.[i, j] = 1uy
105 then
106 let neighborhood = List<(int*int)>()
107 let neighborsToCheck = List<(int*int)>()
108 neighborsToCheck.Add((i, j))
109 data'.[i, j] <- 0uy
110
111 while neighborsToCheck.Count > 0 do
112 let (ci, cj) = pop neighborsToCheck
113 neighborhood.Add((ci, cj))
114 for (ni, nj) in neighbors do
115 let pi = ci + ni
116 let pj = cj + nj
117 if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy
118 then
119 neighborsToCheck.Add((pi, pj))
120 data'.[pi, pj] <- 0uy
121 if neighborhood.Count <= areaSize
122 then
123 for (ni, nj) in neighborhood do
124 data.[ni, nj] <- 0uy
125
126 let connectedComponents (img: Image<Gray, byte>) (startPoints: List<Point>) : List<Point> =
127 let w = img.Width
128 let h = img.Height
129
130 let pointChecked = HashSet<Point>()
131 let pointToCheck = List<Point>(startPoints);
132
133 let data = img.Data
134
135 while pointToCheck.Count > 0 do
136 let next = pop pointToCheck
137 pointChecked.Add(next) |> ignore
138 for ny in -1 .. 1 do
139 for nx in -1 .. 1 do
140 if ny <> 0 && nx <> 0
141 then
142 let p = Point(next.X + nx, next.Y + ny)
143 if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h && data.[p.Y, p.X, 0] > 0uy && not (pointChecked.Contains p)
144 then
145 pointToCheck.Add(p)
146
147 List<Point>(pointChecked)
148
149
150 let saveImg (img: Image<'TColor, 'TDepth>) (filepath: string) =
151 img.Save(filepath)
152
153
154 let saveMat (mat: Matrix<'TDepth>) (filepath: string) =
155 use img = new Image<Gray, 'TDeph>(mat.Size)
156 mat.CopyTo(img)
157 saveImg img filepath
158
159 let drawLine (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: int) (y0: int) (x1: int) (y1: int) (thickness: int) =
160 img.Draw(LineSegment2D(Point(x0, y0), Point(x1, y1)), color, thickness);
161
162 let drawLineF (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: float) (y0: float) (x1: float) (y1: float) (thickness: int) =
163 img.Draw(LineSegment2DF(PointF(float32 x0, float32 y0), PointF(float32 x1, float32 y1)), color, thickness, CvEnum.LineType.AntiAlias);
164
165 let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) (alpha: float) =
166
167 if alpha >= 1.0
168 then
169 img.Draw(Ellipse(PointF(float32 e.Cx, float32 e.Cy), SizeF(2. * e.B |> float32, 2. * e.A |> float32), float32 <| e.Alpha / Math.PI * 180.), color, 1, CvEnum.LineType.AntiAlias)
170 else
171 let windowPosX = e.Cx - e.A - 5.0
172 let gapX = windowPosX - (float (int windowPosX))
173
174 let windowPosY = e.Cy - e.A - 5.0
175 let gapY = windowPosY - (float (int windowPosY))
176
177 let roi = Rectangle(int windowPosX, int windowPosY, 2. * (e.A + 5.0) |> int, 2.* (e.A + 5.0) |> int)
178
179 img.ROI <- roi
180 if roi = img.ROI // We do not display ellipses touching the edges (FIXME)
181 then
182 use i = new Image<'TColor, 'TDepth>(img.ROI.Size)
183 i.Draw(Ellipse(PointF(float32 <| (e.A + 5. + gapX) , float32 <| (e.A + 5. + gapY)), SizeF(2. * e.B |> float32, 2. * e.A |> float32), float32 <| e.Alpha / Math.PI * 180.), color, 1, CvEnum.LineType.AntiAlias)
184 CvInvoke.AddWeighted(img, 1.0, i, alpha, 0.0, img)
185 img.ROI <- Rectangle.Empty
186
187
188 let drawEllipses (img: Image<'TColor, 'TDepth>) (ellipses: Types.Ellipse list) (color: 'TColor) (alpha: float) =
189 List.iter (fun e -> drawEllipse img e color alpha) ellipses
190
191
192 let rngCell = System.Random()
193 let drawCell (img: Image<Bgr, byte>) (drawCellContent: bool) (c: Types.Cell) =
194 if drawCellContent
195 then
196 let colorB = rngCell.Next(20, 70)
197 let colorG = rngCell.Next(20, 70)
198 let colorR = rngCell.Next(20, 70)
199
200 for y in 0 .. c.elements.Height - 1 do
201 for x in 0 .. c.elements.Width - 1 do
202 if c.elements.[y, x] > 0uy
203 then
204 let dx, dy = c.center.X - c.elements.Width / 2, c.center.Y - c.elements.Height / 2
205 let b = img.Data.[y + dy, x + dx, 0] |> int
206 let g = img.Data.[y + dy, x + dx, 1] |> int
207 let r = img.Data.[y + dy, x + dx, 2] |> int
208 img.Data.[y + dy, x + dx, 0] <- if b + colorB > 255 then 255uy else byte (b + colorB)
209 img.Data.[y + dy, x + dx, 1] <- if g + colorG > 255 then 255uy else byte (g + colorG)
210 img.Data.[y + dy, x + dx, 2] <- if r + colorR > 255 then 255uy else byte (r + colorR)
211
212 let crossColor, crossColor2 =
213 match c.cellClass with
214 | Types.HealthyRBC -> Bgr(255., 0., 0.), Bgr(255., 255., 255.)
215 | Types.InfectedRBC -> Bgr(0., 0., 255.), Bgr(120., 120., 255.)
216 | Types.Peculiar -> Bgr(0., 0., 0.), Bgr(80., 80., 80.)
217
218 drawLine img crossColor2 (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 2
219 drawLine img crossColor2 c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 2
220
221 drawLine img crossColor (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 1
222 drawLine img crossColor c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 1
223
224
225 let drawCells (img: Image<Bgr, byte>) (drawCellContent: bool) (cells: Types.Cell list) =
226 List.iter (fun c -> drawCell img drawCellContent c) cells