5 open System.Collections.Generic
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>()
25 let saveImg (img: Image<'TColor, 'TDepth>) (filepath: string) =
29 let saveMat (mat: Matrix<'TDepth>) (filepath
: string) =
30 use img = new Image<Gray, 'TDeph>(mat.Size)
35 type Histogram = { data: int[]; total: int; sum: int; min: float32; max: float32 }
37 let histogramImg (img: Image<Gray, float32>) (nbSamples: int) : Histogram =
38 let imgData = img.Data
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]
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
52 let data = Array.zeroCreate nbSamples
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
59 { data = data; total = img.Height * img.Width; sum = Array.sum data; min = min; max = max }
61 let histogramMat (mat: Matrix<float32>) (nbSamples: int) : Histogram =
62 let matData = mat.Data
66 let minLocation = ref <| Point()
68 let maxLocation = ref <| Point()
69 mat.MinMax(min, max, minLocation, maxLocation)
70 float32 !min, float32 !max
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
76 let data = Array.zeroCreate nbSamples
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
83 { data = data; total = mat.Height * mat.Width; sum = Array.sum data; min = min; max = max }
85 let histogram (values: float32 seq) (nbSamples: int) : Histogram =
86 let mutable min = Single.MaxValue
87 let mutable max = Single.MinValue
92 if v < min then min <- v
93 if v > max then max <- v
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
99 let data = Array.zeroCreate nbSamples
103 data.[p] <- data.[p] + 1
105 { data = data; total = n; sum = Array.sum data; min = min; max = max }
107 let otsu (hist: Histogram) : float32 * float32 * float32 =
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
114 for i in 0 .. hist.data.Length - 1 do
115 wB <- wB + hist.data.[i]
118 let wF = hist.total - wB
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
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)
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)
147 float32 l / float32 hist.data.Length * (hist.max - hist.min) + hist.min
149 toFloat level, toFloat mean1, toFloat mean2
152 let suppressMConnections (img: Matrix<byte>) =
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)
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)
167 let findEdges (img: Image<Gray, float32>) : Matrix<byte> * Image<Gray, float32> * Image<Gray, float32> =
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))
176 let xGradient = img.Convolution(sobelKernel)
177 let yGradient = img.Convolution(sobelKernel.Transpose())
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
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
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).
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)
204 // Non-maximum suppression.
205 use nms = new Matrix<byte>(xGradient.Size)
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
213 let PI = float32 Math.PI
215 for i in 0 .. h - 1 do
216 nmsData.[i, 0] <- 0uy
217 nmsData.[i, w - 1] <- 0uy
219 for j in 0 .. w - 1 do
220 nmsData.[0, j] <- 0uy
221 nmsData.[h - 1, j] <- 0uy
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
229 let angle = anglesData.[i, j]
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
235 let mNeigbors (sign: int) : float32 =
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]
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]
252 let m = magnitudesData.[i, j]
253 if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1
255 nmsData.[i, j] <- 1uy
257 // suppressMConnections nms // It's
not helpful
for the rest of the process (ellipse
detection).
259 let edges = new Matrix<byte
>(xGradient.Size)
260 let edgesData = edges.Data
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
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
275 if i
' <> 0 || j' <> 0
279 if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy
281 nmsData.[ni, nj] <- 0uy
282 toVisit.Push(Point(nj, ni))
284 edges, xGradient, yGradient
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
)
292 type Points = HashSet<Point>
294 let drawPoints (img: Image<Gray, 'TDepth>) (points: Points) (intensity: 'TDepth) =
296 img.Data.[p.Y, p.X, 0] <- intensity
302 let findExtremum (img: Image<Gray, 'TDepth>) (extremumType: ExtremumType) : IEnumerable<Points> =
305 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
307 let imgData = img.Data
308 let suppress: bool[,] = Array2D.zeroCreate h w
310 let result = List<List<Point>>()
312 let flood (start: Point) : List<List<Point>> =
313 let sameLevelToCheck = Stack<Point>()
314 let betterLevelToCheck = Stack<Point>()
315 betterLevelToCheck.Push(start)
317 let result' = List<List<Point>>()
319 while betterLevelToCheck.Count > 0 do
320 let p = betterLevelToCheck.Pop()
321 if not suppress.[p.Y, p.X]
323 suppress.[p.Y, p.X] <- true
324 sameLevelToCheck.Push(p)
325 let current = List<Point>()
327 let mutable betterExists = false
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
336 if ni >= 0 && ni < h && nj >= 0 && nj < w
338 let level = imgData.[ni, nj, 0]
339 let notSuppressed = not suppress.[ni, nj]
341 if level = currentLevel && notSuppressed
343 suppress.[ni, nj] <- true
344 sameLevelToCheck.Push(Point(nj, ni))
345 elif
if extremumType
= ExtremumType.Maxima then level > currentLevel else level < currentLevel
350 betterLevelToCheck.Push(Point(nj, ni))
357 for i
in 0 .. h - 1 do
358 for j in 0 .. w - 1 do
359 let maxima = flood (Point(j, i
))
362 result.AddRange(maxima)
364 result.Select(fun l
-> Points(l
))
367 let findMaxima (img: Image<Gray, 'TDepth>) : IEnumerable<Points> =
368 findExtremum img ExtremumType.Maxima
371 let findMinima (img: Image<Gray, 'TDepth>) : IEnumerable<Points> =
372 findExtremum img ExtremumType.Minima
375 type PriorityQueue () =
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
381 member this
.NextMax () : byte
* Point =
384 invalidOp
"Queue is empty"
388 l.Remove(next) |> ignore
389 let value = byte
highest
393 highest <- highest - 1
394 while highest > lowest && q.[highest].Count = 0 do
395 highest <- highest - 1
403 member this
.NextMin () : byte
* Point =
406 invalidOp
"Queue is empty"
408 let l = q.[lowest + 1]
410 l.Remove(next) |> ignore
411 let value = byte
(lowest + 1)
416 while lowest < highest && q.[lowest + 1].Count = 0 do
431 member this
.Add (value: byte
) (p: Point) =
441 q.[vi].Add(p) |> ignore
443 member this
.Remove (value: byte
) (p: Point) =
445 if q.[vi].Remove(p) && q.[vi].Count = 0
449 highest <- highest - 1
450 while highest > lowest && q.[highest].Count = 0 do
451 highest <- highest - 1
455 while lowest < highest && q.[lowest + 1].Count = 0 do
458 if highest = lowest // The queue is now empty.
463 member this
.IsEmpty =
466 member this
.Clear () =
467 while highest > lowest do
469 highest <- highest - 1
474 type private AreaState =
479 type private AreaOperation =
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
489 let private areaOperation
(img: Image<Gray, byte
>) (area
: int) (op
: AreaOperation) =
492 let imgData = img.Data
493 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
495 let areas = List<Area>((if op
= AreaOperation.Opening then findMaxima img else findMinima img) |> Seq.map
Area)
497 let pixels: Area[,] = Array2D.create
h w null
499 for e
in m.Elements do
500 pixels.[e
.Y, e
.X] <- m
502 let queue = PriorityQueue()
504 let addEdgeToQueue (elements
: Points) =
509 let p' = Point(nj, ni)
510 if ni >= 0 && ni < h && nj >= 0 && nj < w && not (elements.Contains(p'))
512 queue.Add (imgData.[ni, nj, 0]) p'
514 // Reverse order is quicker.
515 for i in areas.Count - 1 .. -1 .. 0 do
517 if m.Elements.Count <= area && m.State <> AreaState.Removed
520 addEdgeToQueue m.Elements
522 let mutable intensity = if op = AreaOperation.Opening then queue.Max else queue.Min
523 let nextElements = Points()
525 let mutable stop = false
527 let intensity', p = if op
= AreaOperation.Opening then queue.NextMax () else queue.NextMin ()
528 let mutable merged = false
530 if intensity' = intensity // The intensity doesn't
change.
532 if m.Elements.Count + nextElements.Count + 1 > area
534 m.State <- AreaState.Validated
535 m.Intensity <- Some intensity
538 nextElements.Add(p) |> ignore
540 elif
if op
= AreaOperation.Opening then intensity' < intensity else intensity' > intensity
542 m.Elements.UnionWith(nextElements)
543 for e
in nextElements do
544 pixels.[e
.Y, e
.X] <- m
546 if m.Elements.Count = area
548 m.State <- AreaState.Validated
549 m.Intensity <- Some (intensity')
552 intensity <- intensity'
554 nextElements.Add(p) |> ignore
557 match pixels.[p.Y, p.X] with
560 if m'.Elements.Count + m.Elements.Count <= area
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
571 intensity <- intensityMax
577 m.State <- AreaState.Validated
578 m.Intensity <- Some (intensity)
581 if not stop && not merged
586 let p' = Point(nj, ni)
587 if ni < 0 || ni >= h || nj < 0 || nj >= w
589 m.State <- AreaState.Validated
590 m.Intensity <- Some (intensity)
592 elif not (m.Elements.Contains(p')) && not (nextElements.Contains(p'))
594 queue.Add (imgData.[ni, nj, 0]) p'
598 if m.Elements.Count + nextElements.Count <= area
600 m.State <- AreaState.Validated
601 m.Intensity <- Some intensity'
602 m.Elements.UnionWith(nextElements)
606 if m.State = AreaState.Validated
608 match m.Intensity with
610 for p in m.Elements do
611 imgData.[p.Y, p.X, 0] <- i
616 let areaOpen (img: Image<Gray, byte>) (area: int) =
617 areaOperation img area AreaOperation.Opening
619 let areaClose (img: Image<Gray, byte>) (area: int) =
620 areaOperation img area AreaOperation.Closing
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
629 let private areaOperationF (img: Image<Gray, float32>) (areas: (int * 'a
) list
) (f
: ('a -> float32 -> unit) option) (op: AreaOperation) =
633 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
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) }
639 let ownership: Island[,] = Array2D.create h w null
641 // Initialize islands with their shore.
642 let islands = List<Island>()
643 let extremum = img |> if op = AreaOperation.Opening then findMaxima else findMinima
647 Island(comparer, Level = earth.[p.Y, p.X, 0], Surface = e.Count)
649 let shorePoints = Points()
651 ownership.[p.Y, p.X] <- island
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))
658 shorePoints.Add(neighbor) |> ignore
659 island.Shore.Add earth.[ni, nj, 0] neighbor
661 for area, obj in areas do
662 for island in islands do
663 let mutable stop = island.Shore.IsEmpty
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)
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.
678 island.Shore.RemoveNext ()
680 if not <| Object.ReferenceEquals(other, null)
681 then // We touching another island.
682 if island.Surface + other.Surface >= area
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
696 elif comparer.Compare(level, island.Level) > 0
700 island.Shore.RemoveNext ()
704 if ni < 0 || ni >= h || nj < 0 || nj >= w
706 island.Surface <- Int32.MaxValue
709 let neighbor = Point(nj, ni)
710 if not <| ownedOrAdjacent neighbor
712 island.Shore.Add earth.[ni, nj, 0] neighbor
715 ownership.[next.Y, next.X] <- island
716 island.Level <- level
717 island.Surface <- island.Surface + 1
719 let mutable diff = 0.f
721 for i in 0 .. h - 1 do
722 for j in 0 .. w - 1 do
723 match ownership.[i, j] with
727 diff <- diff + l - earth.[i, j, 0]
731 | Some f' -> f
' obj diff
736 let areaOpenF (img: Image<Gray, float32>) (area: int) =
737 areaOperationF img [ area, () ] None AreaOperation.Opening
739 let areaCloseF (img: Image<Gray, float32>) (area: int) =
740 areaOperationF img [ area, () ] None AreaOperation.Closing
742 let areaOpenFWithFun (img: Image<Gray, float32>) (areas: (int * 'a
) list
) (f
: 'a -> float32 -> unit) =
743 areaOperationF img areas (Some f) AreaOperation.Opening
745 let areaCloseFWithFun (img: Image<Gray, float32>) (areas: (int * 'a
) list
) (f: 'a -> float32 -> unit) =
746 areaOperationF img areas (Some f) AreaOperation.Closing
748 // A simpler algorithm than 'areaOpen' but slower.
749 let areaOpen2 (img: Image<Gray, byte>) (area: int) =
752 let imgData = img.Data
753 let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
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
761 let flooded : bool[,] = Array2D.zeroCreate h w
763 let pointsChecked = HashSet<Point>()
764 let pointsToCheck = Stack<Point>()
766 for level in 255 .. -1 .. 0 do
767 let mutable n = histogram.[level]
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
774 let mutable maxNeighborValue = 0uy
775 pointsChecked.Clear()
776 pointsToCheck.Clear()
777 pointsToCheck.Push(Point(j, i))
779 while pointsToCheck.Count > 0 do
780 let next = pointsToCheck.Pop()
781 pointsChecked.Add(next) |> ignore
782 flooded.[next.Y, next.X] <- true
785 let p = Point(next.X + nx, next.Y + ny)
786 if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h
788 let v = imgData.[p.Y, p.X, 0]
791 if not (pointsChecked.Contains(p))
793 pointsToCheck.Push(p)
794 elif v > maxNeighborValue
796 maxNeighborValue <- v
798 if int maxNeighborValue < level && pointsChecked.Count <= area
800 for p in pointsChecked do
801 imgData.[p.Y, p.X, 0] <- maxNeighborValue
804 // Zhang and Suen algorithm.
805 // Modify 'mat
' in place.
806 let thin (mat: Matrix<byte>) =
809 let mutable data1 = mat.Data
810 let mutable data2 = Array2D.copy data1
812 let mutable pixelChanged = true
813 let mutable oddIteration = true
815 while pixelChanged do
816 pixelChanged <- false
819 if data1.[i, j] = 1uy
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]
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 &&
841 then p2 * p4 * p6 = 0uy && p4 * p6 * p8 = 0uy
842 else p2 * p4 * p8 = 0uy && p2 * p6 * p8 = 0uy
849 oddIteration <- not oddIteration
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) =
868 use mat' = new Matrix<byte
>(mat.Size)
874 let data' = mat'.Data
878 if data'.[i
, j] = 1uy
880 let neighborhood = List<Point>()
881 let neighborsToCheck = Stack<Point>()
882 neighborsToCheck.Push(Point(j, i
))
885 while neighborsToCheck.Count > 0 do
886 let n = neighborsToCheck.Pop()
888 for (ni, nj) in neighbors do
891 if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy
893 neighborsToCheck.Push(Point(pj, pi))
894 data'.[pi, pj] <- 0uy
895 if neighborhood.Count <= areaSize
897 for n in neighborhood do
898 data.[n.Y, n.X] <- 0uy
900 let connectedComponents (img: Image<Gray, byte>) (startPoints: List<Point>) : List<Point> =
904 let pointChecked = Points()
905 let pointToCheck = Stack<Point>(startPoints);
909 while pointToCheck.Count > 0 do
910 let next = pointToCheck.Pop()
911 pointChecked.Add(next) |> ignore
914 if ny <> 0 && nx <> 0
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)
921 List<Point>(pointChecked)
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
);
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);
932 let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColor) (alpha
: float) =
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)
938 let windowPosX = e
.Cx - e
.A - 5.f
939 let gapX = windowPosX - (float32
(int windowPosX))
941 let windowPosY = e
.Cy - e
.A - 5.f
942 let gapY = windowPosY - (float32
(int windowPosY))
944 let roi = Rectangle(int windowPosX, int windowPosY, 2.f * (e
.A + 5.f) |> int, 2.f * (e
.A + 5.f) |> int)
947 if roi = img.ROI // We do not display ellipses touching the edges (FIXME)
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
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
959 let rngCell = System.Random()
960 let drawCell (img: Image<Bgr, byte>) (drawCellContent: bool) (c: Types.Cell) =
963 let colorB = rngCell.Next(20, 70)
964 let colorG = rngCell.Next(20, 70)
965 let colorR = rngCell.Next(20, 70)
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
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)
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.)
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
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
992 let drawCells (img: Image<Bgr, byte>) (drawCellContent: bool) (cells: Types.Cell list) =
993 List.iter (fun c -> drawCell img drawCellContent c) cells