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