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