Project the colors to have the best contrast for RBCs and parasites analyze.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ImgTools.fs
1 module ParasitemiaCore.ImgTools
2
3 open System
4 open System.Drawing
5 open System.Collections.Generic
6 open System.Linq
7
8 open Emgu.CV
9 open Emgu.CV.Structure
10
11 open Heap
12 open Const
13 open Types
14 open Utils
15
16 let normalize (img: Image<Gray, float32>) (upperLimit: float) : Image<Gray, float32> =
17 let min = ref [| 0.0 |]
18 let minLocation = ref <| [| Point() |]
19 let max = ref [| 0.0 |]
20 let maxLocation = ref <| [| Point() |]
21 img.MinMax(min, max, minLocation, maxLocation)
22 let normalized = (img - (!min).[0]) / ((!max).[0] - (!min).[0])
23 if upperLimit = 1.0
24 then normalized
25 else upperLimit * normalized
26
27 let mergeChannels (img: Image<Bgr, float32>) (rgbWeights: float * float * float) : Image<Gray, float32> =
28 match rgbWeights with
29 | 1., 0., 0. -> img.[2]
30 | 0., 1., 0. -> img.[1]
31 | 0., 0., 1. -> img.[0]
32 | redFactor, greenFactor, blueFactor ->
33 let result = new Image<Gray, float32>(img.Size)
34 CvInvoke.AddWeighted(result, 1., img.[2], redFactor, 0., result)
35 CvInvoke.AddWeighted(result, 1., img.[1], greenFactor, 0., result)
36 CvInvoke.AddWeighted(result, 1., img.[0], blueFactor, 0., result)
37 result
38
39 let mergeChannelsWithProjection (img: Image<Bgr, float32>) (v1r: float32, v1g: float32, v1b: float32) (v2r: float32, v2g: float32, v2b: float32) (upperLimit: float) : Image<Gray, float32> =
40 let vr, vg, vb = v2r - v1r, v2g - v1g, v2b - v1b
41 let vMagnitude = sqrt (vr ** 2.f + vg ** 2.f + vb ** 2.f)
42 let project (r: float32) (g: float32) (b: float32) = ((r - v1r) * vr + (g - v1g) * vg + (b - v1b) * vb) / vMagnitude
43 let result = new Image<Gray, float32>(img.Size)
44 // TODO: Essayer en bindant Data pour gagner du temps
45 for i in 0 .. img.Height - 1 do
46 for j in 0 .. img.Width - 1 do
47 result.Data.[i, j, 0] <- project img.Data.[i, j, 2] img.Data.[i, j, 1] img.Data.[i, j, 0]
48 normalize result upperLimit
49
50 // Normalize image values between 0uy and 255uy.
51 let normalizeAndConvert (img: Image<Gray, 'TDepth>) : Image<Gray, byte> =
52 let min = ref [| 0.0 |]
53 let minLocation = ref <| [| Point() |]
54 let max = ref [| 0.0 |]
55 let maxLocation = ref <| [| Point() |]
56 img.MinMax(min, max, minLocation, maxLocation)
57 ((img.Convert<Gray, float32>() - (!min).[0]) / ((!max).[0] - (!min).[0]) * 255.0).Convert<Gray, byte>()
58
59 let saveImg (img: Image<'TColor, 'TDepth>) (filepath: string) =
60 img.Save(filepath)
61
62 let saveMat (mat: Matrix<'TDepth>) (filepath: string) =
63 use img = new Image<Gray, 'TDeph>(mat.Size)
64 mat.CopyTo(img)
65 saveImg img filepath
66
67 type Histogram = { data: int[]; total: int; sum: int; min: float32; max: float32 }
68
69 let histogramImg (img: Image<Gray, float32>) (nbSamples: int) : Histogram =
70 let imgData = img.Data
71
72 let min, max =
73 let min = ref [| 0.0 |]
74 let minLocation = ref <| [| Point() |]
75 let max = ref [| 0.0 |]
76 let maxLocation = ref <| [| Point() |]
77 img.MinMax(min, max, minLocation, maxLocation)
78 float32 (!min).[0], float32 (!max).[0]
79
80 let inline bin (x: float32) : int =
81 let p = int ((x - min) / (max - min) * float32 nbSamples)
82 if p >= nbSamples then nbSamples - 1 else p
83
84 let data = Array.zeroCreate nbSamples
85
86 for i in 0 .. img.Height - 1 do
87 for j in 0 .. img.Width - 1 do
88 let p = bin imgData.[i, j, 0]
89 data.[p] <- data.[p] + 1
90
91 { data = data; total = img.Height * img.Width; sum = Array.sum data; min = min; max = max }
92
93 let histogramMat (mat: Matrix<float32>) (nbSamples: int) : Histogram =
94 let matData = mat.Data
95
96 let min, max =
97 let min = ref 0.0
98 let minLocation = ref <| Point()
99 let max = ref 0.0
100 let maxLocation = ref <| Point()
101 mat.MinMax(min, max, minLocation, maxLocation)
102 float32 !min, float32 !max
103
104 let inline bin (x: float32) : int =
105 let p = int ((x - min) / (max - min) * float32 nbSamples)
106 if p >= nbSamples then nbSamples - 1 else p
107
108 let data = Array.zeroCreate nbSamples
109
110 for i in 0 .. mat.Height - 1 do
111 for j in 0 .. mat.Width - 1 do
112 let p = bin matData.[i, j]
113 data.[p] <- data.[p] + 1
114
115 { data = data; total = mat.Height * mat.Width; sum = Array.sum data; min = min; max = max }
116
117 let histogram (values: float32 seq) (nbSamples: int) : Histogram =
118 let mutable min = Single.MaxValue
119 let mutable max = Single.MinValue
120 let mutable n = 0
121
122 for v in values do
123 n <- n + 1
124 if v < min then min <- v
125 if v > max then max <- v
126
127 let inline bin (x: float32) : int =
128 let p = int ((x - min) / (max - min) * float32 nbSamples)
129 if p >= nbSamples then nbSamples - 1 else p
130
131 let data = Array.zeroCreate nbSamples
132
133 for v in values do
134 let p = bin v
135 data.[p] <- data.[p] + 1
136
137 { data = data; total = n; sum = Array.sum data; min = min; max = max }
138
139 let otsu (hist: Histogram) : float32 * float32 * float32 =
140 let mutable sumB = 0
141 let mutable wB = 0
142 let mutable maximum = 0.0
143 let mutable level = 0
144 let sum = hist.data |> Array.mapi (fun i v -> i * v |> float) |> Array.sum
145
146 for i in 0 .. hist.data.Length - 1 do
147 wB <- wB + hist.data.[i]
148 if wB <> 0
149 then
150 let wF = hist.total - wB
151 if wF <> 0
152 then
153 sumB <- sumB + i * hist.data.[i]
154 let mB = (float sumB) / (float wB)
155 let mF = (sum - float sumB) / (float wF)
156 let between = (float wB) * (float wF) * (mB - mF) ** 2.;
157 if between >= maximum
158 then
159 level <- i
160 maximum <- between
161
162 let mean1 =
163 let mutable sum = 0
164 let mutable nb = 0
165 for i in 0 .. level - 1 do
166 sum <- sum + i * hist.data.[i]
167 nb <- nb + hist.data.[i]
168 (sum + level * hist.data.[level] / 2) / (nb + hist.data.[level] / 2)
169
170 let mean2 =
171 let mutable sum = 0
172 let mutable nb = 0
173 for i in level + 1 .. hist.data.Length - 1 do
174 sum <- sum + i * hist.data.[i]
175 nb <- nb + hist.data.[i]
176 (sum + level * hist.data.[level] / 2) / (nb + hist.data.[level] / 2)
177
178 let toFloat l =
179 float32 l / float32 hist.data.Length * (hist.max - hist.min) + hist.min
180
181 toFloat level, toFloat mean1, toFloat mean2
182
183 /// <summary>
184 /// Remove M-adjacent pixels. It may be used after thinning.
185 /// </summary>
186 let suppressMAdjacency (img: Matrix<byte>) =
187 let w = img.Width
188 let h = img.Height
189 for i in 1 .. h - 2 do
190 for j in 1 .. w - 2 do
191 if img.[i, j] > 0uy && img.Data.[i + 1, j] > 0uy && (img.Data.[i, j - 1] > 0uy && img.Data.[i - 1, j + 1] = 0uy || img.Data.[i, j + 1] > 0uy && img.Data.[i - 1, j - 1] = 0uy)
192 then
193 img.[i, j] <- 0uy
194 for i in 1 .. h - 2 do
195 for j in 1 .. w - 2 do
196 if img.[i, j] > 0uy && img.Data.[i - 1, j] > 0uy && (img.Data.[i, j - 1] > 0uy && img.Data.[i + 1, j + 1] = 0uy || img.Data.[i, j + 1] > 0uy && img.Data.[i + 1, j - 1] = 0uy)
197 then
198 img.[i, j] <- 0uy
199
200 /// <summary>
201 /// Find edges of an image by using the Canny approach.
202 /// The thresholds are automatically defined with otsu on gradient magnitudes.
203 /// </summary>
204 /// <param name="img"></param>
205 let findEdges (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<float32> =
206 let w = img.Width
207 let h = img.Height
208
209 use sobelKernel =
210 new Matrix<float32>(array2D [[ 1.0f; 0.0f; -1.0f ]
211 [ 2.0f; 0.0f; -2.0f ]
212 [ 1.0f; 0.0f; -1.0f ]])
213
214 let xGradient = new Matrix<float32>(img.Size)
215 let yGradient = new Matrix<float32>(img.Size)
216 CvInvoke.Filter2D(img, xGradient, sobelKernel, Point(1, 1))
217 CvInvoke.Filter2D(img, yGradient, sobelKernel.Transpose(), Point(1, 1))
218
219 use magnitudes = new Matrix<float32>(xGradient.Size)
220 use angles = new Matrix<float32>(xGradient.Size)
221 CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, angles) // Compute the magnitudes and angles.
222
223 let thresholdHigh, thresholdLow =
224 let sensibilityHigh = 0.1f
225 let sensibilityLow = 0.0f
226 let threshold, _, _ = otsu (histogramMat magnitudes 300)
227 threshold + (sensibilityHigh * threshold), threshold - (sensibilityLow * threshold)
228
229 // Non-maximum suppression.
230 use nms = new Matrix<byte>(xGradient.Size)
231
232 let nmsData = nms.Data
233 let anglesData = angles.Data
234 let magnitudesData = magnitudes.Data
235 let xGradientData = xGradient.Data
236 let yGradientData = yGradient.Data
237
238 for i in 0 .. h - 1 do
239 nmsData.[i, 0] <- 0uy
240 nmsData.[i, w - 1] <- 0uy
241
242 for j in 0 .. w - 1 do
243 nmsData.[0, j] <- 0uy
244 nmsData.[h - 1, j] <- 0uy
245
246 for i in 1 .. h - 2 do
247 for j in 1 .. w - 2 do
248 let vx = xGradientData.[i, j]
249 let vy = yGradientData.[i, j]
250 if vx <> 0.f || vy <> 0.f
251 then
252 let angle = anglesData.[i, j]
253
254 let vx', vy' = abs vx, abs vy
255 let ratio2 = if vx' > vy' then vy' / vx' else vx' / vy'
256 let ratio1 = 1.f - ratio2
257
258 let mNeigbors (sign: int) : float32 =
259 if angle < PI / 4.f
260 then ratio1 * magnitudesData.[i, j + sign] + ratio2 * magnitudesData.[i + sign, j + sign]
261 elif angle < PI / 2.f
262 then ratio2 * magnitudesData.[i + sign, j + sign] + ratio1 * magnitudesData.[i + sign, j]
263 elif angle < 3.f * PI / 4.f
264 then ratio1 * magnitudesData.[i + sign, j] + ratio2 * magnitudesData.[i + sign, j - sign]
265 elif angle < PI
266 then ratio2 * magnitudesData.[i + sign, j - sign] + ratio1 * magnitudesData.[i, j - sign]
267 elif angle < 5.f * PI / 4.f
268 then ratio1 * magnitudesData.[i, j - sign] + ratio2 * magnitudesData.[i - sign, j - sign]
269 elif angle < 3.f * PI / 2.f
270 then ratio2 * magnitudesData.[i - sign, j - sign] + ratio1 * magnitudesData.[i - sign, j]
271 elif angle < 7.f * PI / 4.f
272 then ratio1 * magnitudesData.[i - sign, j] + ratio2 * magnitudesData.[i - sign, j + sign]
273 else ratio2 * magnitudesData.[i - sign, j + sign] + ratio1 * magnitudesData.[i, j + sign]
274
275 let m = magnitudesData.[i, j]
276 if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1
277 then
278 nmsData.[i, j] <- 1uy
279
280 // suppressMConnections nms // It's not helpful for the rest of the process (ellipse detection).
281
282 let edges = new Matrix<byte>(xGradient.Size)
283 let edgesData = edges.Data
284
285 // Hysteresis thresholding.
286 let toVisit = Stack<Point>()
287 for i in 0 .. h - 1 do
288 for j in 0 .. w - 1 do
289 if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh
290 then
291 nmsData.[i, j] <- 0uy
292 toVisit.Push(Point(j, i))
293 while toVisit.Count > 0 do
294 let p = toVisit.Pop()
295 edgesData.[p.Y, p.X] <- 1uy
296 for i' in -1 .. 1 do
297 for j' in -1 .. 1 do
298 if i' <> 0 || j' <> 0
299 then
300 let ni = p.Y + i'
301 let nj = p.X + j'
302 if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy
303 then
304 nmsData.[ni, nj] <- 0uy
305 toVisit.Push(Point(nj, ni))
306
307 edges, xGradient, yGradient
308
309 let gaussianFilter (img : Image<'TColor, 'TDepth>) (standardDeviation : float) : Image<'TColor, 'TDepth> =
310 let size = 2 * int (ceil (4.0 * standardDeviation)) + 1
311 img.SmoothGaussian(size, size, standardDeviation, standardDeviation)
312
313 let drawPoints (img: Image<Gray, 'TDepth>) (points: Points) (intensity: 'TDepth) =
314 for p in points do
315 img.Data.[p.Y, p.X, 0] <- intensity
316
317 type ExtremumType =
318 | Maxima = 1
319 | Minima = 2
320
321 let findExtremum (img: Image<Gray, 'TDepth>) (extremumType: ExtremumType) : IEnumerable<Points> =
322 let w = img.Width
323 let h = img.Height
324 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
325
326 let imgData = img.Data
327 let suppress: bool[,] = Array2D.zeroCreate h w
328
329 let result = List<List<Point>>()
330
331 let flood (start: Point) : List<List<Point>> =
332 let sameLevelToCheck = Stack<Point>()
333 let betterLevelToCheck = Stack<Point>()
334 betterLevelToCheck.Push(start)
335
336 let result' = List<List<Point>>()
337
338 while betterLevelToCheck.Count > 0 do
339 let p = betterLevelToCheck.Pop()
340 if not suppress.[p.Y, p.X]
341 then
342 suppress.[p.Y, p.X] <- true
343 sameLevelToCheck.Push(p)
344 let current = List<Point>()
345
346 let mutable betterExists = false
347
348 while sameLevelToCheck.Count > 0 do
349 let p' = sameLevelToCheck.Pop()
350 let currentLevel = imgData.[p'.Y, p'.X, 0]
351 current.Add(p') |> ignore
352 for i, j in se do
353 let ni = i + p'.Y
354 let nj = j + p'.X
355 if ni >= 0 && ni < h && nj >= 0 && nj < w
356 then
357 let level = imgData.[ni, nj, 0]
358 let notSuppressed = not suppress.[ni, nj]
359
360 if level = currentLevel && notSuppressed
361 then
362 suppress.[ni, nj] <- true
363 sameLevelToCheck.Push(Point(nj, ni))
364 elif if extremumType = ExtremumType.Maxima then level > currentLevel else level < currentLevel
365 then
366 betterExists <- true
367 if notSuppressed
368 then
369 betterLevelToCheck.Push(Point(nj, ni))
370
371 if not betterExists
372 then
373 result'.Add(current)
374 result'
375
376 for i in 0 .. h - 1 do
377 for j in 0 .. w - 1 do
378 let maxima = flood (Point(j, i))
379 if maxima.Count > 0
380 then
381 result.AddRange(maxima)
382
383 result.Select(fun l -> Points(l))
384
385 let findMaxima (img: Image<Gray, 'TDepth>) : IEnumerable<Points> =
386 findExtremum img ExtremumType.Maxima
387
388 let findMinima (img: Image<Gray, 'TDepth>) : IEnumerable<Points> =
389 findExtremum img ExtremumType.Minima
390
391 type PriorityQueue () =
392 let size = 256
393 let q: Points[] = Array.init size (fun i -> Points())
394 let mutable highest = -1 // Value of the first elements of 'q'.
395 let mutable lowest = size
396
397 member this.NextMax () : byte * Point =
398 if this.IsEmpty
399 then
400 invalidOp "Queue is empty"
401 else
402 let l = q.[highest]
403 let next = l.First()
404 l.Remove(next) |> ignore
405 let value = byte highest
406
407 if l.Count = 0
408 then
409 highest <- highest - 1
410 while highest > lowest && q.[highest].Count = 0 do
411 highest <- highest - 1
412 if highest = lowest
413 then
414 highest <- -1
415 lowest <- size
416
417 value, next
418
419 member this.NextMin () : byte * Point =
420 if this.IsEmpty
421 then
422 invalidOp "Queue is empty"
423 else
424 let l = q.[lowest + 1]
425 let next = l.First()
426 l.Remove(next) |> ignore
427 let value = byte (lowest + 1)
428
429 if l.Count = 0
430 then
431 lowest <- lowest + 1
432 while lowest < highest && q.[lowest + 1].Count = 0 do
433 lowest <- lowest + 1
434 if highest = lowest
435 then
436 highest <- -1
437 lowest <- size
438
439 value, next
440
441 member this.Max =
442 highest |> byte
443
444 member this.Min =
445 lowest + 1 |> byte
446
447 member this.Add (value: byte) (p: Point) =
448 let vi = int value
449
450 if vi > highest
451 then
452 highest <- vi
453 if vi <= lowest
454 then
455 lowest <- vi - 1
456
457 q.[vi].Add(p) |> ignore
458
459 member this.Remove (value: byte) (p: Point) =
460 let vi = int value
461 if q.[vi].Remove(p) && q.[vi].Count = 0
462 then
463 if vi = highest
464 then
465 highest <- highest - 1
466 while highest > lowest && q.[highest].Count = 0 do
467 highest <- highest - 1
468 elif vi - 1 = lowest
469 then
470 lowest <- lowest + 1
471 while lowest < highest && q.[lowest + 1].Count = 0 do
472 lowest <- lowest + 1
473
474 if highest = lowest // The queue is now empty.
475 then
476 highest <- -1
477 lowest <- size
478
479 member this.IsEmpty =
480 highest = -1
481
482 member this.Clear () =
483 while highest > lowest do
484 q.[highest].Clear()
485 highest <- highest - 1
486 highest <- -1
487 lowest <- size
488
489 type private AreaState =
490 | Removed = 1
491 | Unprocessed = 2
492 | Validated = 3
493
494 type private AreaOperation =
495 | Opening = 1
496 | Closing = 2
497
498 [<AllowNullLiteral>]
499 type private Area (elements: Points) =
500 member this.Elements = elements
501 member val Intensity = None with get, set
502 member val State = AreaState.Unprocessed with get, set
503
504 let private areaOperation (img: Image<Gray, byte>) (area: int) (op: AreaOperation) =
505 let w = img.Width
506 let h = img.Height
507 let imgData = img.Data
508 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
509
510 let areas = List<Area>((if op = AreaOperation.Opening then findMaxima img else findMinima img) |> Seq.map Area)
511
512 let pixels: Area[,] = Array2D.create h w null
513 for m in areas do
514 for e in m.Elements do
515 pixels.[e.Y, e.X] <- m
516
517 let queue = PriorityQueue()
518
519 let addEdgeToQueue (elements: Points) =
520 for p in elements do
521 for i, j in se do
522 let ni = i + p.Y
523 let nj = j + p.X
524 let p' = Point(nj, ni)
525 if ni >= 0 && ni < h && nj >= 0 && nj < w && not (elements.Contains(p'))
526 then
527 queue.Add (imgData.[ni, nj, 0]) p'
528
529 // Reverse order is quicker.
530 for i in areas.Count - 1 .. -1 .. 0 do
531 let m = areas.[i]
532 if m.Elements.Count <= area && m.State <> AreaState.Removed
533 then
534 queue.Clear()
535 addEdgeToQueue m.Elements
536
537 let mutable intensity = if op = AreaOperation.Opening then queue.Max else queue.Min
538 let nextElements = Points()
539
540 let mutable stop = false
541 while not stop do
542 let intensity', p = if op = AreaOperation.Opening then queue.NextMax () else queue.NextMin ()
543 let mutable merged = false
544
545 if intensity' = intensity // The intensity doesn't change.
546 then
547 if m.Elements.Count + nextElements.Count + 1 > area
548 then
549 m.State <- AreaState.Validated
550 m.Intensity <- Some intensity
551 stop <- true
552 else
553 nextElements.Add(p) |> ignore
554
555 elif if op = AreaOperation.Opening then intensity' < intensity else intensity' > intensity
556 then
557 m.Elements.UnionWith(nextElements)
558 for e in nextElements do
559 pixels.[e.Y, e.X] <- m
560
561 if m.Elements.Count = area
562 then
563 m.State <- AreaState.Validated
564 m.Intensity <- Some (intensity')
565 stop <- true
566 else
567 intensity <- intensity'
568 nextElements.Clear()
569 nextElements.Add(p) |> ignore
570
571 else
572 match pixels.[p.Y, p.X] with
573 | null -> ()
574 | m' ->
575 if m'.Elements.Count + m.Elements.Count <= area
576 then
577 m'.State <- AreaState.Removed
578 for e in m'.Elements do
579 pixels.[e.Y, e.X] <- m
580 queue.Remove imgData.[e.Y, e.X, 0] e
581 addEdgeToQueue m'.Elements
582 m.Elements.UnionWith(m'.Elements)
583 let intensityMax = if op = AreaOperation.Opening then queue.Max else queue.Min
584 if intensityMax <> intensity
585 then
586 intensity <- intensityMax
587 nextElements.Clear()
588 merged <- true
589
590 if not merged
591 then
592 m.State <- AreaState.Validated
593 m.Intensity <- Some (intensity)
594 stop <- true
595
596 if not stop && not merged
597 then
598 for i, j in se do
599 let ni = i + p.Y
600 let nj = j + p.X
601 let p' = Point(nj, ni)
602 if ni < 0 || ni >= h || nj < 0 || nj >= w
603 then
604 m.State <- AreaState.Validated
605 m.Intensity <- Some (intensity)
606 stop <- true
607 elif not (m.Elements.Contains(p')) && not (nextElements.Contains(p'))
608 then
609 queue.Add (imgData.[ni, nj, 0]) p'
610
611 if queue.IsEmpty
612 then
613 if m.Elements.Count + nextElements.Count <= area
614 then
615 m.State <- AreaState.Validated
616 m.Intensity <- Some intensity'
617 m.Elements.UnionWith(nextElements)
618 stop <- true
619
620 for m in areas do
621 if m.State = AreaState.Validated
622 then
623 match m.Intensity with
624 | Some i ->
625 for p in m.Elements do
626 imgData.[p.Y, p.X, 0] <- i
627 | _ -> ()
628 ()
629
630 /// <summary>
631 /// Area opening on byte image.
632 /// </summary>
633 let areaOpen (img: Image<Gray, byte>) (area: int) =
634 areaOperation img area AreaOperation.Opening
635
636 /// <summary>
637 /// Area closing on byte image.
638 /// </summary>
639 let areaClose (img: Image<Gray, byte>) (area: int) =
640 areaOperation img area AreaOperation.Closing
641
642 // A simpler algorithm than 'areaOpen' on byte image but slower.
643 let areaOpen2 (img: Image<Gray, byte>) (area: int) =
644 let w = img.Width
645 let h = img.Height
646 let imgData = img.Data
647 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
648
649 let histogram = Array.zeroCreate 256
650 for i in 0 .. h - 1 do
651 for j in 0 .. w - 1 do
652 let v = imgData.[i, j, 0] |> int
653 histogram.[v] <- histogram.[v] + 1
654
655 let flooded : bool[,] = Array2D.zeroCreate h w
656
657 let pointsChecked = HashSet<Point>()
658 let pointsToCheck = Stack<Point>()
659
660 for level in 255 .. -1 .. 0 do
661 let mutable n = histogram.[level]
662 if n > 0
663 then
664 for i in 0 .. h - 1 do
665 for j in 0 .. w - 1 do
666 if not flooded.[i, j] && imgData.[i, j, 0] = byte level
667 then
668 let mutable maxNeighborValue = 0uy
669 pointsChecked.Clear()
670 pointsToCheck.Clear()
671 pointsToCheck.Push(Point(j, i))
672
673 while pointsToCheck.Count > 0 do
674 let next = pointsToCheck.Pop()
675 pointsChecked.Add(next) |> ignore
676 flooded.[next.Y, next.X] <- true
677
678 for nx, ny in se do
679 let p = Point(next.X + nx, next.Y + ny)
680 if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h
681 then
682 let v = imgData.[p.Y, p.X, 0]
683 if v = byte level
684 then
685 if not (pointsChecked.Contains(p))
686 then
687 pointsToCheck.Push(p)
688 elif v > maxNeighborValue
689 then
690 maxNeighborValue <- v
691
692 if int maxNeighborValue < level && pointsChecked.Count <= area
693 then
694 for p in pointsChecked do
695 imgData.[p.Y, p.X, 0] <- maxNeighborValue
696
697 [<AllowNullLiteral>]
698 type Island (cmp: IComparer<float32>) =
699 member val Shore = Heap.Heap<float32, Point>(cmp) with get
700 member val Level = 0.f with get, set
701 member val Surface = 0 with get, set
702 member this.IsInfinite = this.Surface = Int32.MaxValue
703
704 let private areaOperationF (img: Image<Gray, float32>) (areas: (int * 'a) list) (f: ('a -> float32 -> unit) option) (op: AreaOperation) =
705 let w = img.Width
706 let h = img.Height
707 let earth = img.Data
708 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
709
710 let comparer = if op = AreaOperation.Opening
711 then { new IComparer<float32> with member this.Compare(v1, v2) = v1.CompareTo(v2) }
712 else { new IComparer<float32> with member this.Compare(v1, v2) = v2.CompareTo(v1) }
713
714 let ownership: Island[,] = Array2D.create h w null
715
716 // Initialize islands with their shore.
717 let islands = List<Island>()
718 let extremum = img |> if op = AreaOperation.Opening then findMaxima else findMinima
719 for e in extremum do
720 let island =
721 let p = e.First()
722 Island(comparer, Level = earth.[p.Y, p.X, 0], Surface = e.Count)
723 islands.Add(island)
724 let shorePoints = Points()
725 for p in e do
726 ownership.[p.Y, p.X] <- island
727 for i, j in se do
728 let ni = i + p.Y
729 let nj = j + p.X
730 let neighbor = Point(nj, ni)
731 if ni >= 0 && ni < h && nj >= 0 && nj < w && Object.ReferenceEquals(ownership.[ni, nj], null) && not (shorePoints.Contains(neighbor))
732 then
733 shorePoints.Add(neighbor) |> ignore
734 island.Shore.Add earth.[ni, nj, 0] neighbor
735
736 for area, obj in areas do
737 for island in islands do
738 let mutable stop = island.Shore.IsEmpty
739
740 // 'true' if 'p' is owned or adjacent to 'island'.
741 let inline ownedOrAdjacent (p: Point) : bool =
742 ownership.[p.Y, p.X] = island ||
743 (p.Y > 0 && ownership.[p.Y - 1, p.X] = island) ||
744 (p.Y < h - 1 && ownership.[p.Y + 1, p.X] = island) ||
745 (p.X > 0 && ownership.[p.Y, p.X - 1] = island) ||
746 (p.X < w - 1 && ownership.[p.Y, p.X + 1] = island)
747
748 while not stop && island.Surface < area do
749 let level, next = island.Shore.Max
750 let other = ownership.[next.Y, next.X]
751 if other = island // During merging, some points on the shore may be owned by the island itself -> ignored.
752 then
753 island.Shore.RemoveNext ()
754 else
755 if not <| Object.ReferenceEquals(other, null)
756 then // We touching another island.
757 if island.IsInfinite || other.IsInfinite || island.Surface + other.Surface >= area || comparer.Compare(island.Level, other.Level) < 0
758 then
759 stop <- true
760 else // We can merge 'other' into 'surface'.
761 island.Surface <- island.Surface + other.Surface
762 island.Level <- other.Level
763 // island.Level <- if comparer.Compare(island.Level, other.Level) > 0 then other.Level else island.Level
764 for l, p in other.Shore do
765 let mutable currentY = p.Y + 1
766 while currentY < h && ownership.[currentY, p.X] = other do
767 ownership.[currentY, p.X] <- island
768 currentY <- currentY + 1
769 island.Shore.Add l p
770 other.Shore.Clear()
771
772 elif comparer.Compare(level, island.Level) > 0
773 then
774 stop <- true
775 else
776 island.Shore.RemoveNext ()
777 for i, j in se do
778 let ni = i + next.Y
779 let nj = j + next.X
780 if ni < 0 || ni >= h || nj < 0 || nj >= w
781 then
782 island.Surface <- Int32.MaxValue
783 stop <- true
784 else
785 let neighbor = Point(nj, ni)
786 if not <| ownedOrAdjacent neighbor
787 then
788 island.Shore.Add earth.[ni, nj, 0] neighbor
789 if not stop
790 then
791 ownership.[next.Y, next.X] <- island
792 island.Level <- level
793 island.Surface <- island.Surface + 1
794
795 let mutable diff = 0.f
796
797 for i in 0 .. h - 1 do
798 for j in 0 .. w - 1 do
799 match ownership.[i, j] with
800 | null -> ()
801 | island ->
802 let l = island.Level
803 diff <- diff + l - earth.[i, j, 0]
804 earth.[i, j, 0] <- l
805
806 match f with
807 | Some f' -> f' obj diff
808 | _ -> ()
809 ()
810
811 /// <summary>
812 /// Area opening on float image.
813 /// </summary>
814 let areaOpenF (img: Image<Gray, float32>) (area: int) =
815 areaOperationF img [ area, () ] None AreaOperation.Opening
816
817 /// <summary>
818 /// Area closing on float image.
819 /// </summary>
820 let areaCloseF (img: Image<Gray, float32>) (area: int) =
821 areaOperationF img [ area, () ] None AreaOperation.Closing
822
823 /// <summary>
824 /// Area closing on float image with different areas. Given areas must be sorted increasingly.
825 /// For each area the function 'f' is called with the associated area value of type 'a and the volume difference
826 /// Between the previous and the current closing.
827 /// </summary>
828 let areaOpenFWithFun (img: Image<Gray, float32>) (areas: (int * 'a) list) (f: 'a -> float32 -> unit) =
829 areaOperationF img areas (Some f) AreaOperation.Opening
830
831 /// <summary>
832 /// Same as 'areaOpenFWithFun' for closing operation.
833 /// </summary>
834 let areaCloseFWithFun (img: Image<Gray, float32>) (areas: (int * 'a) list) (f: 'a -> float32 -> unit) =
835 areaOperationF img areas (Some f) AreaOperation.Closing
836
837 /// <summary>
838 /// Zhang and Suen thinning algorithm.
839 /// Modify 'mat' in place.
840 /// </summary>
841 let thin (mat: Matrix<byte>) =
842 let w = mat.Width
843 let h = mat.Height
844 let mutable data1 = mat.Data
845 let mutable data2 = Array2D.copy data1
846
847 let mutable pixelChanged = true
848 let mutable oddIteration = true
849
850 while pixelChanged do
851 pixelChanged <- false
852 for i in 0..h-1 do
853 for j in 0..w-1 do
854 if data1.[i, j] = 1uy
855 then
856 let p2 = if i = 0 then 0uy else data1.[i-1, j]
857 let p3 = if i = 0 || j = w-1 then 0uy else data1.[i-1, j+1]
858 let p4 = if j = w-1 then 0uy else data1.[i, j+1]
859 let p5 = if i = h-1 || j = w-1 then 0uy else data1.[i+1, j+1]
860 let p6 = if i = h-1 then 0uy else data1.[i+1, j]
861 let p7 = if i = h-1 || j = 0 then 0uy else data1.[i+1, j-1]
862 let p8 = if j = 0 then 0uy else data1.[i, j-1]
863 let p9 = if i = 0 || j = 0 then 0uy else data1.[i-1, j-1]
864
865 let sumNeighbors = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9
866 if sumNeighbors >= 2uy && sumNeighbors <= 6uy &&
867 (if p2 = 0uy && p3 = 1uy then 1 else 0) +
868 (if p3 = 0uy && p4 = 1uy then 1 else 0) +
869 (if p4 = 0uy && p5 = 1uy then 1 else 0) +
870 (if p5 = 0uy && p6 = 1uy then 1 else 0) +
871 (if p6 = 0uy && p7 = 1uy then 1 else 0) +
872 (if p7 = 0uy && p8 = 1uy then 1 else 0) +
873 (if p8 = 0uy && p9 = 1uy then 1 else 0) +
874 (if p9 = 0uy && p2 = 1uy then 1 else 0) = 1 &&
875 if oddIteration
876 then p2 * p4 * p6 = 0uy && p4 * p6 * p8 = 0uy
877 else p2 * p4 * p8 = 0uy && p2 * p6 * p8 = 0uy
878 then
879 data2.[i, j] <- 0uy
880 pixelChanged <- true
881 else
882 data2.[i, j] <- 0uy
883
884 oddIteration <- not oddIteration
885 let tmp = data1
886 data1 <- data2
887 data2 <- tmp
888
889 /// <summary>
890 /// Remove all 8-connected pixels with an area equal or greater than 'areaSize'.
891 /// Modify 'mat' in place.
892 /// </summary>
893 let removeArea (mat: Matrix<byte>) (areaSize: int) =
894 let neighbors = [|
895 (-1, 0) // p2
896 (-1, 1) // p3
897 ( 0, 1) // p4
898 ( 1, 1) // p5
899 ( 1, 0) // p6
900 ( 1, -1) // p7
901 ( 0, -1) // p8
902 (-1, -1) |] // p9
903
904 use mat' = new Matrix<byte>(mat.Size)
905 let w = mat'.Width
906 let h = mat'.Height
907 mat.CopyTo(mat')
908
909 let data = mat.Data
910 let data' = mat'.Data
911
912 for i in 0..h-1 do
913 for j in 0..w-1 do
914 if data'.[i, j] = 1uy
915 then
916 let neighborhood = List<Point>()
917 let neighborsToCheck = Stack<Point>()
918 neighborsToCheck.Push(Point(j, i))
919 data'.[i, j] <- 0uy
920
921 while neighborsToCheck.Count > 0 do
922 let n = neighborsToCheck.Pop()
923 neighborhood.Add(n)
924 for (ni, nj) in neighbors do
925 let pi = n.Y + ni
926 let pj = n.X + nj
927 if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy
928 then
929 neighborsToCheck.Push(Point(pj, pi))
930 data'.[pi, pj] <- 0uy
931 if neighborhood.Count <= areaSize
932 then
933 for n in neighborhood do
934 data.[n.Y, n.X] <- 0uy
935
936 let connectedComponents (img: Image<Gray, byte>) (startPoints: List<Point>) : Points =
937 let w = img.Width
938 let h = img.Height
939
940 let pointChecked = Points()
941 let pointToCheck = Stack<Point>(startPoints);
942
943 let data = img.Data
944
945 while pointToCheck.Count > 0 do
946 let next = pointToCheck.Pop()
947 pointChecked.Add(next) |> ignore
948 for ny in -1 .. 1 do
949 for nx in -1 .. 1 do
950 if ny <> 0 && nx <> 0
951 then
952 let p = Point(next.X + nx, next.Y + ny)
953 if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h && data.[p.Y, p.X, 0] > 0uy && not (pointChecked.Contains p)
954 then
955 pointToCheck.Push(p)
956
957 pointChecked
958
959 let drawLine (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: int) (y0: int) (x1: int) (y1: int) (thickness: int) =
960 img.Draw(LineSegment2D(Point(x0, y0), Point(x1, y1)), color, thickness);
961
962 let drawLineF (img: Image<'TColor, 'TDepth>) (color: 'TColor) (x0: float) (y0: float) (x1: float) (y1: float) (thickness: int) =
963 img.Draw(LineSegment2DF(PointF(float32 x0, float32 y0), PointF(float32 x1, float32 y1)), color, thickness, CvEnum.LineType.AntiAlias);
964
965 let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Ellipse) (color: 'TColor) (alpha: float) =
966 if alpha >= 1.0
967 then
968 img.Draw(Emgu.CV.Structure.Ellipse(PointF(e.Cx, e.Cy), SizeF(2.f * e.B, 2.f * e.A), e.Alpha / PI * 180.f), color, 1, CvEnum.LineType.AntiAlias)
969 else
970 let windowPosX = e.Cx - e.A - 5.f
971 let gapX = windowPosX - (float32 (int windowPosX))
972
973 let windowPosY = e.Cy - e.A - 5.f
974 let gapY = windowPosY - (float32 (int windowPosY))
975
976 let roi = Rectangle(int windowPosX, int windowPosY, 2.f * (e.A + 5.f) |> int, 2.f * (e.A + 5.f) |> int)
977
978 img.ROI <- roi
979 if roi = img.ROI // We do not display ellipses touching the edges (FIXME)
980 then
981 use i = new Image<'TColor, 'TDepth>(img.ROI.Size)
982 i.Draw(Emgu.CV.Structure.Ellipse(PointF(e.A + 5.f + gapX, e.A + 5.f + gapY), SizeF(2.f * e.B, 2.f * e.A), e.Alpha / PI * 180.f), color, 1, CvEnum.LineType.AntiAlias)
983 CvInvoke.AddWeighted(img, 1.0, i, alpha, 0.0, img)
984 img.ROI <- Rectangle.Empty
985
986 let drawEllipses (img: Image<'TColor, 'TDepth>) (ellipses: Ellipse list) (color: 'TColor) (alpha: float) =
987 List.iter (fun e -> drawEllipse img e color alpha) ellipses
988
989 let rngCell = System.Random()
990 let drawCell (img: Image<Bgr, byte>) (drawCellContent: bool) (c: Cell) =
991 if drawCellContent
992 then
993 let colorB = rngCell.Next(20, 70)
994 let colorG = rngCell.Next(20, 70)
995 let colorR = rngCell.Next(20, 70)
996
997 for y in 0 .. c.elements.Height - 1 do
998 for x in 0 .. c.elements.Width - 1 do
999 if c.elements.[y, x] > 0uy
1000 then
1001 let dx, dy = c.center.X - c.elements.Width / 2, c.center.Y - c.elements.Height / 2
1002 let b = img.Data.[y + dy, x + dx, 0] |> int
1003 let g = img.Data.[y + dy, x + dx, 1] |> int
1004 let r = img.Data.[y + dy, x + dx, 2] |> int
1005 img.Data.[y + dy, x + dx, 0] <- if b + colorB > 255 then 255uy else byte (b + colorB)
1006 img.Data.[y + dy, x + dx, 1] <- if g + colorG > 255 then 255uy else byte (g + colorG)
1007 img.Data.[y + dy, x + dx, 2] <- if r + colorR > 255 then 255uy else byte (r + colorR)
1008
1009 let crossColor, crossColor2 =
1010 match c.cellClass with
1011 | HealthyRBC -> Bgr(255., 0., 0.), Bgr(255., 255., 255.)
1012 | InfectedRBC -> Bgr(0., 0., 255.), Bgr(120., 120., 255.)
1013 | Peculiar -> Bgr(0., 0., 0.), Bgr(80., 80., 80.)
1014
1015 drawLine img crossColor2 (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 2
1016 drawLine img crossColor2 c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 2
1017
1018 drawLine img crossColor (c.center.X - 3) c.center.Y (c.center.X + 3) c.center.Y 1
1019 drawLine img crossColor c.center.X (c.center.Y - 3) c.center.X (c.center.Y + 3) 1
1020
1021
1022 let drawCells (img: Image<Bgr, byte>) (drawCellContent: bool) (cells: Cell list) =
1023 List.iter (fun c -> drawCell img drawCellContent c) cells