1
module ParasitemiaCore.ImgTools
5 open System.Collections.Generic
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])
25 else upperLimit * normalized
27 let mergeChannels (img
: Image<Bgr, float32
>) (rgbWeights
: float * float * float) : Image<Gray, float32
> =
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)
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
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>()
59 let saveImg (img: Image<'TColor, 'TDepth>) (filepath: string) =
62 let saveMat (mat: Matrix<'TDepth>) (filepath
: string) =
63 use img = new Image<Gray, 'TDeph>(mat.Size)
67 type Histogram = { data: int[]; total: int; sum: int; min: float32; max: float32 }
69 let histogramImg (img: Image<Gray, float32>) (nbSamples: int) : Histogram =
70 let imgData = img.Data
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]
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
84 let data = Array.zeroCreate nbSamples
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
91 { data = data; total = img.Height * img.Width; sum = Array.sum data; min = min; max = max }
93 let histogramMat (mat: Matrix<float32>) (nbSamples: int) : Histogram =
94 let matData = mat.Data
98 let minLocation = ref <| Point()
100 let maxLocation = ref <| Point()
101 mat.MinMax(min, max, minLocation, maxLocation)
102 float32 !min, float32 !max
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
108 let data = Array.zeroCreate nbSamples
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
115 { data = data; total = mat.Height * mat.Width; sum = Array.sum data; min = min; max = max }
117 let histogram (values: float32 seq) (nbSamples: int) : Histogram =
118 let mutable min = Single.MaxValue
119 let mutable max = Single.MinValue
124 if v < min then min <- v
125 if v > max then max <- v
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
131 let data = Array.zeroCreate nbSamples
135 data.[p] <- data.[p] + 1
137 { data = data; total = n; sum = Array.sum data; min = min; max = max }
139 let otsu (hist: Histogram) : float32 * float32 * float32 =
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
146 for i in 0 .. hist.data.Length - 1 do
147 wB <- wB + hist.data.[i]
150 let wF = hist.total - wB
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
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)
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)
179 float32 l / float32 hist.data.Length * (hist.max - hist.min) + hist.min
181 toFloat level, toFloat mean1, toFloat mean2
184 /// Remove M-adjacent pixels. It may be used after thinning.
186 let suppressMAdjacency (img: Matrix<byte>) =
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)
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)
201 /// Find edges of an image by using the Canny approach.
202 /// The thresholds are automatically defined with otsu on gradient magnitudes.
204 /// <param name="img"></param>
205 let findEdges (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<float32> =
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 ]])
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))
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.
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)
229 // Non-maximum suppression.
230 use nms = new Matrix<byte>(xGradient.Size)
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
238 for i in 0 .. h - 1 do
239 nmsData.[i, 0] <- 0uy
240 nmsData.[i, w - 1] <- 0uy
242 for j in 0 .. w - 1 do
243 nmsData.[0, j] <- 0uy
244 nmsData.[h - 1, j] <- 0uy
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
252 let angle = anglesData.[i, j]
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
258 let mNeigbors (sign: int) : float32 =
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]
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]
275 let m = magnitudesData.[i, j]
276 if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1
278 nmsData.[i, j] <- 1uy
280 // suppressMConnections nms // It's
not helpful
for the rest of the process (ellipse
detection).
282 let edges = new Matrix<byte
>(xGradient.Size)
283 let edgesData = edges.Data
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
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
298 if i
' <> 0 || j' <> 0
302 if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy
304 nmsData.[ni, nj] <- 0uy
305 toVisit.Push(Point(nj, ni))
307 edges, xGradient, yGradient
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
)
313 let drawPoints (img: Image<Gray, 'TDepth>) (points: Points) (intensity: 'TDepth) =
315 img.Data.[p.Y, p.X, 0] <- intensity
321 let findExtremum (img: Image<Gray, 'TDepth>) (extremumType: ExtremumType) : IEnumerable<Points> =
324 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
326 let imgData = img.Data
327 let suppress: bool[,] = Array2D.zeroCreate h w
329 let result = List<List<Point>>()
331 let flood (start: Point) : List<List<Point>> =
332 let sameLevelToCheck = Stack<Point>()
333 let betterLevelToCheck = Stack<Point>()
334 betterLevelToCheck.Push(start)
336 let result' = List<List<Point>>()
338 while betterLevelToCheck.Count > 0 do
339 let p = betterLevelToCheck.Pop()
340 if not suppress.[p.Y, p.X]
342 suppress.[p.Y, p.X] <- true
343 sameLevelToCheck.Push(p)
344 let current = List<Point>()
346 let mutable betterExists = false
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
355 if ni >= 0 && ni < h && nj >= 0 && nj < w
357 let level = imgData.[ni, nj, 0]
358 let notSuppressed = not suppress.[ni, nj]
360 if level = currentLevel && notSuppressed
362 suppress.[ni, nj] <- true
363 sameLevelToCheck.Push(Point(nj, ni))
364 elif
if extremumType
= ExtremumType.Maxima then level > currentLevel else level < currentLevel
369 betterLevelToCheck.Push(Point(nj, ni))
376 for i
in 0 .. h - 1 do
377 for j in 0 .. w - 1 do
378 let maxima = flood (Point(j, i
))
381 result.AddRange(maxima)
383 result.Select(fun l
-> Points(l
))
385 let findMaxima (img: Image<Gray, 'TDepth>) : IEnumerable<Points> =
386 findExtremum img ExtremumType.Maxima
388 let findMinima (img: Image<Gray, 'TDepth>) : IEnumerable<Points> =
389 findExtremum img ExtremumType.Minima
391 type PriorityQueue () =
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
397 member this
.NextMax () : byte
* Point =
400 invalidOp
"Queue is empty"
404 l.Remove(next) |> ignore
405 let value = byte
highest
409 highest <- highest - 1
410 while highest > lowest && q.[highest].Count = 0 do
411 highest <- highest - 1
419 member this
.NextMin () : byte
* Point =
422 invalidOp
"Queue is empty"
424 let l = q.[lowest + 1]
426 l.Remove(next) |> ignore
427 let value = byte
(lowest + 1)
432 while lowest < highest && q.[lowest + 1].Count = 0 do
447 member this
.Add (value: byte
) (p: Point) =
457 q.[vi].Add(p) |> ignore
459 member this
.Remove (value: byte
) (p: Point) =
461 if q.[vi].Remove(p) && q.[vi].Count = 0
465 highest <- highest - 1
466 while highest > lowest && q.[highest].Count = 0 do
467 highest <- highest - 1
471 while lowest < highest && q.[lowest + 1].Count = 0 do
474 if highest = lowest // The queue is now empty.
479 member this
.IsEmpty =
482 member this
.Clear () =
483 while highest > lowest do
485 highest <- highest - 1
489 type private AreaState =
494 type private AreaOperation =
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
504 let private areaOperation
(img: Image<Gray, byte
>) (area
: int) (op
: AreaOperation) =
507 let imgData = img.Data
508 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
510 let areas = List<Area>((if op
= AreaOperation.Opening then findMaxima img else findMinima img) |> Seq.map
Area)
512 let pixels: Area[,] = Array2D.create
h w null
514 for e
in m.Elements do
515 pixels.[e
.Y, e
.X] <- m
517 let queue = PriorityQueue()
519 let addEdgeToQueue (elements
: Points) =
524 let p' = Point(nj, ni)
525 if ni >= 0 && ni < h && nj >= 0 && nj < w && not (elements.Contains(p'))
527 queue.Add (imgData.[ni, nj, 0]) p'
529 // Reverse order is quicker.
530 for i in areas.Count - 1 .. -1 .. 0 do
532 if m.Elements.Count <= area && m.State <> AreaState.Removed
535 addEdgeToQueue m.Elements
537 let mutable intensity = if op = AreaOperation.Opening then queue.Max else queue.Min
538 let nextElements = Points()
540 let mutable stop = false
542 let intensity', p = if op
= AreaOperation.Opening then queue.NextMax () else queue.NextMin ()
543 let mutable merged = false
545 if intensity' = intensity // The intensity doesn't
change.
547 if m.Elements.Count + nextElements.Count + 1 > area
549 m.State <- AreaState.Validated
550 m.Intensity <- Some intensity
553 nextElements.Add(p) |> ignore
555 elif
if op
= AreaOperation.Opening then intensity' < intensity else intensity' > intensity
557 m.Elements.UnionWith(nextElements)
558 for e
in nextElements do
559 pixels.[e
.Y, e
.X] <- m
561 if m.Elements.Count = area
563 m.State <- AreaState.Validated
564 m.Intensity <- Some (intensity')
567 intensity <- intensity'
569 nextElements.Add(p) |> ignore
572 match pixels.[p.Y, p.X] with
575 if m'.Elements.Count + m.Elements.Count <= area
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
586 intensity <- intensityMax
592 m.State <- AreaState.Validated
593 m.Intensity <- Some (intensity)
596 if not stop && not merged
601 let p' = Point(nj, ni)
602 if ni < 0 || ni >= h || nj < 0 || nj >= w
604 m.State <- AreaState.Validated
605 m.Intensity <- Some (intensity)
607 elif not (m.Elements.Contains(p')) && not (nextElements.Contains(p'))
609 queue.Add (imgData.[ni, nj, 0]) p'
613 if m.Elements.Count + nextElements.Count <= area
615 m.State <- AreaState.Validated
616 m.Intensity <- Some intensity'
617 m.Elements.UnionWith(nextElements)
621 if m.State = AreaState.Validated
623 match m.Intensity with
625 for p in m.Elements do
626 imgData.[p.Y, p.X, 0] <- i
631 /// Area opening on byte image.
633 let areaOpen (img: Image<Gray, byte>) (area: int) =
634 areaOperation img area AreaOperation.Opening
637 /// Area closing on byte image.
639 let areaClose (img: Image<Gray, byte>) (area: int) =
640 areaOperation img area AreaOperation.Closing
642 // A simpler algorithm than 'areaOpen' on byte image but slower.
643 let areaOpen2 (img: Image<Gray, byte>) (area: int) =
646 let imgData = img.Data
647 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
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
655 let flooded : bool[,] = Array2D.zeroCreate h w
657 let pointsChecked = HashSet<Point>()
658 let pointsToCheck = Stack<Point>()
660 for level in 255 .. -1 .. 0 do
661 let mutable n = histogram.[level]
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
668 let mutable maxNeighborValue = 0uy
669 pointsChecked.Clear()
670 pointsToCheck.Clear()
671 pointsToCheck.Push(Point(j, i))
673 while pointsToCheck.Count > 0 do
674 let next = pointsToCheck.Pop()
675 pointsChecked.Add(next) |> ignore
676 flooded.[next.Y, next.X] <- true
679 let p = Point(next.X + nx, next.Y + ny)
680 if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h
682 let v = imgData.[p.Y, p.X, 0]
685 if not (pointsChecked.Contains(p))
687 pointsToCheck.Push(p)
688 elif v > maxNeighborValue
690 maxNeighborValue <- v
692 if int maxNeighborValue < level && pointsChecked.Count <= area
694 for p in pointsChecked do
695 imgData.[p.Y, p.X, 0] <- maxNeighborValue
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
704 let private areaOperationF (img: Image<Gray, float32>) (areas: (int * 'a
) list
) (f
: ('a -> float32 -> unit) option) (op: AreaOperation) =
708 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
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) }
714 let ownership: Island[,] = Array2D.create h w null
716 // Initialize islands with their shore.
717 let islands = List<Island>()
718 let extremum = img |> if op = AreaOperation.Opening then findMaxima else findMinima
722 Island(comparer, Level = earth.[p.Y, p.X, 0], Surface = e.Count)
724 let shorePoints = Points()
726 ownership.[p.Y, p.X] <- island
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))
733 shorePoints.Add(neighbor) |> ignore
734 island.Shore.Add earth.[ni, nj, 0] neighbor
736 for area, obj in areas do
737 for island in islands do
738 let mutable stop = island.Shore.IsEmpty
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)
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.
753 island.Shore.RemoveNext ()
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
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
772 elif comparer.Compare(level, island.Level) > 0
776 island.Shore.RemoveNext ()
780 if ni < 0 || ni >= h || nj < 0 || nj >= w
782 island.Surface <- Int32.MaxValue
785 let neighbor = Point(nj, ni)
786 if not <| ownedOrAdjacent neighbor
788 island.Shore.Add earth.[ni, nj, 0] neighbor
791 ownership.[next.Y, next.X] <- island
792 island.Level <- level
793 island.Surface <- island.Surface + 1
795 let mutable diff = 0.f
797 for i in 0 .. h - 1 do
798 for j in 0 .. w - 1 do
799 match ownership.[i, j] with
803 diff <- diff + l - earth.[i, j, 0]
807 | Some f' -> f
' obj diff
812 /// Area opening on float image.
814 let areaOpenF (img: Image<Gray, float32>) (area: int) =
815 areaOperationF img [ area, () ] None AreaOperation.Opening
818 /// Area closing on float image.
820 let areaCloseF (img: Image<Gray, float32>) (area: int) =
821 areaOperationF img [ area, () ] None AreaOperation.Closing
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.
828 let areaOpenFWithFun (img: Image<Gray, float32
>) (areas: (int * 'a) list) (f: 'a
-> float32
-> unit) =
829 areaOperationF
img areas (Some f) AreaOperation.Opening
832 /// Same as 'areaOpenFWithFun' for closing operation.
834 let areaCloseFWithFun (img: Image<Gray, float32
>) (areas: (int * 'a) list) (f: 'a
-> float32
-> unit) =
835 areaOperationF
img areas (Some f) AreaOperation.Closing
838 /// Zhang and Suen thinning algorithm.
839 /// Modify 'mat' in place.
841 let thin (mat
: Matrix<byte
>) =
844 let mutable data1 = mat
.Data
845 let mutable data2 = Array2D.copy
data1
847 let mutable pixelChanged = true
848 let mutable oddIteration = true
850 while pixelChanged do
851 pixelChanged <- false
854 if data1.[i
, j] = 1uy
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]
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 &&
876 then p2 * p4 * p6 = 0uy && p4 * p6 * p8 = 0uy
877 else p2 * p4 * p8 = 0uy && p2 * p6 * p8 = 0uy
884 oddIteration <- not oddIteration
890 /// Remove all 8-connected pixels with an area equal or greater than 'areaSize'.
891 /// Modify 'mat' in place.
893 let removeArea (mat
: Matrix<byte
>) (areaSize
: int) =
904 use mat' = new Matrix<byte>(mat.Size)
910 let data' = mat'.Data
914 if data'.[i, j] = 1uy
916 let neighborhood = List<Point>()
917 let neighborsToCheck = Stack<Point>()
918 neighborsToCheck.Push(Point(j, i))
921 while neighborsToCheck.Count > 0 do
922 let n = neighborsToCheck.Pop()
924 for (ni, nj) in neighbors do
927 if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy
929 neighborsToCheck.Push(Point(pj, pi))
930 data'.[pi, pj] <- 0uy
931 if neighborhood.Count <= areaSize
933 for n in neighborhood do
934 data.[n.Y, n.X] <- 0uy
936 let connectedComponents (img: Image<Gray, byte
>) (startPoints
: List<Point>) : Points =
940 let pointChecked = Points()
941 let pointToCheck = Stack<Point>(startPoints
);
945 while pointToCheck.Count > 0 do
946 let next = pointToCheck.Pop()
947 pointChecked.Add(next) |> ignore
950 if ny
<> 0 && nx
<> 0
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)
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);
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);
965 let drawEllipse (img: Image<'TColor, 'TDepth>) (e
: Ellipse) (color
: 'TColor) (alpha: float) =
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)
970 let windowPosX = e.Cx - e.A - 5.f
971 let gapX = windowPosX - (float32 (int windowPosX))
973 let windowPosY = e.Cy - e.A - 5.f
974 let gapY = windowPosY - (float32 (int windowPosY))
976 let roi = Rectangle(int windowPosX, int windowPosY, 2.f * (e.A + 5.f) |> int, 2.f * (e.A + 5.f) |> int)
979 if roi = img.ROI // We do not display ellipses touching the edges (FIXME)
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
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
989 let rngCell = System.Random()
990 let drawCell (img: Image<Bgr, byte
>) (drawCellContent
: bool) (c
: Cell) =
993 let colorB = rngCell.Next(20, 70)
994 let colorG = rngCell.Next(20, 70)
995 let colorR = rngCell.Next(20, 70)
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
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)
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.)
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
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
1022 let drawCells (img: Image<Bgr, byte>) (drawCellContent
: bool) (cells
: Cell list) =
1023 List.iter
(fun c
-> drawCell img drawCellContent
c) cells