| Maxima = 1
| Minima = 2
-let findExtremum (img : Image<Gray, 'TDepth>) (extremumType : ExtremumType) : IEnumerable<Points> =
+let inline findExtremum (img : Image<Gray, 'TDepth>) (extremumType : ExtremumType) : IEnumerable<Points> when 'TDepth : unmanaged =
let w = img.Width
let h = img.Height
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
let imgData = img.Data
let suppress : bool[,] = Array2D.zeroCreate h w
- let result = List<List<Point>>()
+ let result = List<List<Point>> ()
let flood (start : Point) : List<List<Point>> =
- let sameLevelToCheck = Stack<Point>()
- let betterLevelToCheck = Stack<Point>()
- betterLevelToCheck.Push(start)
+ let sameLevelToCheck = Stack<Point> ()
+ let betterLevelToCheck = Stack<Point> ()
+ betterLevelToCheck.Push start
- let result' = List<List<Point>>()
+ let result' = List<List<Point>> ()
while betterLevelToCheck.Count > 0 do
- let p = betterLevelToCheck.Pop()
+ let p = betterLevelToCheck.Pop ()
if not suppress.[p.Y, p.X] then
suppress.[p.Y, p.X] <- true
- sameLevelToCheck.Push(p)
- let current = List<Point>()
+ sameLevelToCheck.Push p
+ let current = List<Point> ()
let mutable betterExists = false
while sameLevelToCheck.Count > 0 do
- let p' = sameLevelToCheck.Pop()
+ let p' = sameLevelToCheck.Pop ()
let currentLevel = imgData.[p'.Y, p'.X, 0]
- current.Add(p') |> ignore
+ current.Add p' |> ignore
for i, j in se do
let ni = i + p'.Y
let nj = j + p'.X
let level = imgData.[ni, nj, 0]
let notSuppressed = not suppress.[ni, nj]
- if level = currentLevel && notSuppressed then
+ if notSuppressed && level = currentLevel then
suppress.[ni, nj] <- true
- sameLevelToCheck.Push(Point(nj, ni))
+ sameLevelToCheck.Push (Point (nj, ni))
elif (if extremumType = ExtremumType.Maxima then level > currentLevel else level < currentLevel) then
betterExists <- true
if notSuppressed then
- betterLevelToCheck.Push(Point(nj, ni))
+ betterLevelToCheck.Push (Point (nj, ni))
if not betterExists then
- result'.Add(current)
+ result'.Add current
result'
for i = 0 to h - 1 do
for j = 0 to w - 1 do
- let maxima = flood (Point(j, i))
+ let maxima = flood (Point (j, i))
if maxima.Count > 0 then
- result.AddRange(maxima)
+ result.AddRange maxima
- result.Select(fun l -> Points(l))
+ result.Select (fun l -> Points l)
-let findMaxima (img : Image<Gray, 'TDepth>) : IEnumerable<Points> =
+let inline findMaxima (img : Image<Gray, 'TDepth>) : IEnumerable<Points> when 'TDepth : unmanaged =
findExtremum img ExtremumType.Maxima
-let findMinima (img : Image<Gray, 'TDepth>) : IEnumerable<Points> =
+let inline findMinima (img : Image<Gray, 'TDepth>) : IEnumerable<Points> when 'TDepth : unmanaged =
findExtremum img ExtremumType.Minima
type PriorityQueue () =
let size = 256
- let q : Points[] = Array.init size (fun i -> Points())
+ let q : Points[] = Array.init size (fun i -> Points ())
let mutable highest = -1 // Value of the first elements of 'q'.
let mutable lowest = size
invalidOp "Queue is empty"
else
let l = q.[highest]
- let next = l.First()
- l.Remove(next) |> ignore
+ let next = l.First ()
+ l.Remove next |> ignore
let value = byte highest
if l.Count = 0 then
invalidOp "Queue is empty"
else
let l = q.[lowest + 1]
- let next = l.First()
- l.Remove(next) |> ignore
+ let next = l.First ()
+ l.Remove next |> ignore
let value = byte (lowest + 1)
if l.Count = 0 then
if vi <= lowest then
lowest <- vi - 1
- q.[vi].Add(p) |> ignore
+ q.[vi].Add p |> ignore
member this.Remove (value : byte) (p : Point) =
let vi = int value
- if q.[vi].Remove(p) && q.[vi].Count = 0 then
+ if q.[vi].Remove p && q.[vi].Count = 0 then
if vi = highest then
highest <- highest - 1
while highest > lowest && q.[highest].Count = 0 do
member this.Clear () =
while highest > lowest do
- q.[highest].Clear()
+ q.[highest].Clear ()
highest <- highest - 1
highest <- -1
lowest <- size
let imgData = img.Data
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
- let areas = List<Area>((if op = AreaOperation.Opening then findMaxima img else findMinima img) |> Seq.map Area)
+ let areas = List<Area> ((if op = AreaOperation.Opening then findMaxima img else findMinima img) |> Seq.map Area)
let pixels : Area[,] = Array2D.create h w null
for m in areas do
for e in m.Elements do
pixels.[e.Y, e.X] <- m
- let queue = PriorityQueue()
+ let queue = PriorityQueue ()
let addEdgeToQueue (elements : Points) =
for p in elements do
for i, j in se do
let ni = i + p.Y
let nj = j + p.X
- let p' = Point(nj, ni)
- if ni >= 0 && ni < h && nj >= 0 && nj < w && not (elements.Contains(p')) then
+ let p' = Point (nj, ni)
+ if ni >= 0 && ni < h && nj >= 0 && nj < w && not (elements.Contains p') then
queue.Add (imgData.[ni, nj, 0]) p'
// Reverse order is quicker.
for i = areas.Count - 1 downto 0 do
let m = areas.[i]
if m.Elements.Count <= area && m.State <> AreaState.Removed then
- queue.Clear()
+ queue.Clear ()
addEdgeToQueue m.Elements
let mutable intensity = if op = AreaOperation.Opening then queue.Max else queue.Min
- let nextElements = Points()
+ let nextElements = Points ()
let mutable stop = false
while not stop do
m.Intensity <- Some intensity
stop <- true
else
- nextElements.Add(p) |> ignore
+ nextElements.Add p |> ignore
elif (if op = AreaOperation.Opening then intensity' < intensity else intensity' > intensity) then
- m.Elements.UnionWith(nextElements)
+ m.Elements.UnionWith nextElements
for e in nextElements do
pixels.[e.Y, e.X] <- m
stop <- true
else
intensity <- intensity'
- nextElements.Clear()
- nextElements.Add(p) |> ignore
+ nextElements.Clear ()
+ nextElements.Add p |> ignore
else
match pixels.[p.Y, p.X] with
pixels.[e.Y, e.X] <- m
queue.Remove imgData.[e.Y, e.X, 0] e
addEdgeToQueue m'.Elements
- m.Elements.UnionWith(m'.Elements)
+ m.Elements.UnionWith m'.Elements
let intensityMax = if op = AreaOperation.Opening then queue.Max else queue.Min
if intensityMax <> intensity then
intensity <- intensityMax
- nextElements.Clear()
+ nextElements.Clear ()
merged <- true
if not merged then
for i, j in se do
let ni = i + p.Y
let nj = j + p.X
- let p' = Point(nj, ni)
+ let p' = Point (nj, ni)
if ni < 0 || ni >= h || nj < 0 || nj >= w then
m.State <- AreaState.Validated
m.Intensity <- Some (intensity)
stop <- true
- elif not (m.Elements.Contains(p')) && not (nextElements.Contains(p')) then
+ elif not (m.Elements.Contains p') && not (nextElements.Contains p') then
queue.Add (imgData.[ni, nj, 0]) p'
if queue.IsEmpty then
if m.Elements.Count + nextElements.Count <= area then
m.State <- AreaState.Validated
m.Intensity <- Some intensity'
- m.Elements.UnionWith(nextElements)
+ m.Elements.UnionWith nextElements
stop <- true
for m in areas do
let flooded : bool[,] = Array2D.zeroCreate h w
- let pointsChecked = HashSet<Point>()
- let pointsToCheck = Stack<Point>()
+ let pointsChecked = HashSet<Point> ()
+ let pointsToCheck = Stack<Point> ()
for level = 255 downto 0 do
let mutable n = histogram.[level]
for j = 0 to w - 1 do
if not flooded.[i, j] && imgData.[i, j, 0] = byte level then
let mutable maxNeighborValue = 0uy
- pointsChecked.Clear()
- pointsToCheck.Clear()
- pointsToCheck.Push(Point(j, i))
+ pointsChecked.Clear ()
+ pointsToCheck.Clear ()
+ pointsToCheck.Push (Point (j, i))
while pointsToCheck.Count > 0 do
- let next = pointsToCheck.Pop()
- pointsChecked.Add(next) |> ignore
+ let next = pointsToCheck.Pop ()
+ pointsChecked.Add next |> ignore
flooded.[next.Y, next.X] <- true
for nx, ny in se do
- let p = Point(next.X + nx, next.Y + ny)
+ let p = Point (next.X + nx, next.Y + ny)
if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h then
let v = imgData.[p.Y, p.X, 0]
if v = byte level then
- if not (pointsChecked.Contains(p)) then
- pointsToCheck.Push(p)
+ if not (pointsChecked.Contains p) then
+ pointsToCheck.Push p
elif v > maxNeighborValue then
maxNeighborValue <- v
[<AllowNullLiteral>]
type Island (cmp : IComparer<float32>) =
- member val Shore = Heap.Heap<float32, Point>(cmp) with get
+ member val Shore = Heap.Heap<float32, Point> cmp with get
member val Level = 0.f with get, set
member val Surface = 0 with get, set
member this.IsInfinite = this.Surface = Int32.MaxValue
let comparer =
if op = AreaOperation.Opening then
- { new IComparer<float32> with member this.Compare(v1, v2) = v1.CompareTo(v2) }
+ { new IComparer<float32> with member this.Compare (v1, v2) = v1.CompareTo v2 }
else
- { new IComparer<float32> with member this.Compare(v1, v2) = v2.CompareTo(v1) }
+ { new IComparer<float32> with member this.Compare (v1, v2) = v2.CompareTo v1 }
let ownership : Island[,] = Array2D.create h w null
// Initialize islands with their shore.
- let islands = List<Island>()
+ let islands = List<Island> ()
let extremum = img |> if op = AreaOperation.Opening then findMaxima else findMinima
for e in extremum do
let island =
- let p = e.First()
- Island(comparer, Level = earth.[p.Y, p.X, 0], Surface = e.Count)
- islands.Add(island)
- let shorePoints = Points()
+ let p = e.First ()
+ Island (comparer, Level = earth.[p.Y, p.X, 0], Surface = e.Count)
+ islands.Add island
+ let shorePoints = Points ()
for p in e do
ownership.[p.Y, p.X] <- island
for i, j in se do
let ni = i + p.Y
let nj = j + p.X
- let neighbor = Point(nj, ni)
- if ni >= 0 && ni < h && nj >= 0 && nj < w && Object.ReferenceEquals(ownership.[ni, nj], null) && not (shorePoints.Contains(neighbor)) then
- shorePoints.Add(neighbor) |> ignore
+ let neighbor = Point (nj, ni)
+ if ni >= 0 && ni < h && nj >= 0 && nj < w && Object.ReferenceEquals (ownership.[ni, nj], null) && not (shorePoints.Contains neighbor) then
+ shorePoints.Add neighbor |> ignore
island.Shore.Add earth.[ni, nj, 0] neighbor
for area, obj in areas do
if other = island then // During merging, some points on the shore may be owned by the island itself -> ignored.
island.Shore.RemoveNext ()
else
- if not <| Object.ReferenceEquals(other, null) then
+ if not <| Object.ReferenceEquals (other, null) then
// We touching another island.
- if island.IsInfinite || other.IsInfinite || island.Surface + other.Surface >= area || comparer.Compare(island.Level, other.Level) < 0 then
+ if island.IsInfinite || other.IsInfinite || island.Surface + other.Surface >= area || comparer.Compare (island.Level, other.Level) < 0 then
stop <- true
else // We can merge 'other' into 'surface'.
island.Surface <- island.Surface + other.Surface
island.Level <- other.Level
- // island.Level <- if comparer.Compare(island.Level, other.Level) > 0 then other.Level else island.Level
for l, p in other.Shore do
let mutable currentY = p.Y + 1
while currentY < h && ownership.[currentY, p.X] = other do
ownership.[currentY, p.X] <- island
currentY <- currentY + 1
island.Shore.Add l p
- other.Shore.Clear()
+ other.Shore.Clear ()
- elif comparer.Compare(level, island.Level) > 0 then
+ elif comparer.Compare (level, island.Level) > 0 then
stop <- true
else
island.Shore.RemoveNext ()
island.Surface <- Int32.MaxValue
stop <- true
else
- let neighbor = Point(nj, ni)
+ let neighbor = Point (nj, ni)
if not <| ownedOrAdjacent neighbor then
island.Shore.Add earth.[ni, nj, 0] neighbor
if not stop then
( 0, -1) // p8
(-1, -1) |] // p9
- use mat' = new Matrix<byte>(mat.Size)
+ use mat' = new Matrix<byte> (mat.Size)
let w = mat'.Width
let h = mat'.Height
- mat.CopyTo(mat')
+ mat.CopyTo mat'
let data = mat.Data
let data' = mat'.Data
for i = 0 to h - 1 do
for j = 0 to w - 1 do
if data'.[i, j] = 1uy then
- let neighborhood = List<Point>()
- let neighborsToCheck = Stack<Point>()
- neighborsToCheck.Push(Point(j, i))
+ let neighborhood = List<Point> ()
+ let neighborsToCheck = Stack<Point> ()
+ neighborsToCheck.Push (Point (j, i))
data'.[i, j] <- 0uy
while neighborsToCheck.Count > 0 do
- let n = neighborsToCheck.Pop()
- neighborhood.Add(n)
+ let n = neighborsToCheck.Pop ()
+ neighborhood.Add n
for (ni, nj) in neighbors do
let pi = n.Y + ni
let pj = n.X + nj
if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy then
- neighborsToCheck.Push(Point(pj, pi))
+ neighborsToCheck.Push (Point (pj, pi))
data'.[pi, pj] <- 0uy
if neighborhood.Count <= areaSize then
for n in neighborhood do
let w = img.Width
let h = img.Height
- let pointChecked = Points()
- let pointToCheck = Stack<Point>(startPoints);
+ let pointChecked = Points ()
+ let pointToCheck = Stack<Point> startPoints;
let data = img.Data
while pointToCheck.Count > 0 do
- let next = pointToCheck.Pop()
- pointChecked.Add(next) |> ignore
+ let next = pointToCheck.Pop ()
+ pointChecked.Add next |> ignore
for ny = -1 to 1 do
for nx = -1 to 1 do
if ny <> 0 && nx <> 0 then
- let p = Point(next.X + nx, next.Y + ny)
+ let p = Point (next.X + nx, next.Y + ny)
if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h && data.[p.Y, p.X, 0] > 0uy && not (pointChecked.Contains p) then
- pointToCheck.Push(p)
+ pointToCheck.Push p
pointChecked