- 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)
- then
+ 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) then
- 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)
- then
+ 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) then
let w = img.Width
let h = img.Height
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
let imgData = img.Data
let w = img.Width
let h = img.Height
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
let imgData = img.Data
- let flood (start: Point) : List<List<Point>> =
- let sameLevelToCheck = Stack<Point>()
- let betterLevelToCheck = Stack<Point>()
- betterLevelToCheck.Push(start)
+ let flood (start : Point) : List<List<Point>> =
+ let sameLevelToCheck = Stack<Point> ()
+ let betterLevelToCheck = Stack<Point> ()
+ betterLevelToCheck.Push start
- let p = betterLevelToCheck.Pop()
- if not suppress.[p.Y, p.X]
- then
+ let p = betterLevelToCheck.Pop ()
+ if not suppress.[p.Y, p.X] then
let level = imgData.[ni, nj, 0]
let notSuppressed = not suppress.[ni, nj]
let level = imgData.[ni, nj, 0]
let notSuppressed = not suppress.[ni, nj]
- sameLevelToCheck.Push(Point(nj, ni))
- elif if extremumType = ExtremumType.Maxima then level > currentLevel else level < currentLevel
- then
+ sameLevelToCheck.Push (Point (nj, ni))
+ elif (if extremumType = ExtremumType.Maxima then level > currentLevel else level < currentLevel) then
- let maxima = flood (Point(j, i))
- if maxima.Count > 0
- then
- result.AddRange(maxima)
+ let maxima = flood (Point (j, i))
+ if maxima.Count > 0 then
+ result.AddRange maxima
let mutable highest = -1 // Value of the first elements of 'q'.
let mutable lowest = size
member this.NextMax () : byte * Point =
let mutable highest = -1 // Value of the first elements of 'q'.
let mutable lowest = size
member this.NextMax () : byte * Point =
member this.Elements = elements
member val Intensity = None with get, set
member val State = AreaState.Unprocessed with get, set
member this.Elements = elements
member val Intensity = None with get, set
member val State = AreaState.Unprocessed with get, set
let w = img.Width
let h = img.Height
let imgData = img.Data
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
let w = img.Width
let h = img.Height
let imgData = img.Data
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
queue.Add (imgData.[ni, nj, 0]) p'
// Reverse order is quicker.
for i = areas.Count - 1 downto 0 do
let m = areas.[i]
queue.Add (imgData.[ni, nj, 0]) p'
// Reverse order is quicker.
for i = areas.Count - 1 downto 0 do
let m = areas.[i]
addEdgeToQueue m.Elements
let mutable intensity = if op = AreaOperation.Opening then queue.Max else queue.Min
addEdgeToQueue m.Elements
let mutable intensity = if op = AreaOperation.Opening then queue.Max else queue.Min
let mutable stop = false
while not stop do
let intensity', p = if op = AreaOperation.Opening then queue.NextMax () else queue.NextMin ()
let mutable merged = false
let mutable stop = false
while not stop do
let intensity', p = if op = AreaOperation.Opening then queue.NextMax () else queue.NextMin ()
let mutable merged = false
- if intensity' = intensity // The intensity doesn't change.
- then
- if m.Elements.Count + nextElements.Count + 1 > area
- then
+ if intensity' = intensity then // The intensity doesn't change.
+ if m.Elements.Count + nextElements.Count + 1 > area then
- elif if op = AreaOperation.Opening then intensity' < intensity else intensity' > intensity
- then
- m.Elements.UnionWith(nextElements)
+ elif (if op = AreaOperation.Opening then intensity' < intensity else intensity' > intensity) then
+ m.Elements.UnionWith nextElements
m'.State <- AreaState.Removed
for e in m'.Elements do
pixels.[e.Y, e.X] <- m
queue.Remove imgData.[e.Y, e.X, 0] e
addEdgeToQueue m'.Elements
m'.State <- AreaState.Removed
for e in m'.Elements do
pixels.[e.Y, e.X] <- m
queue.Remove imgData.[e.Y, e.X, 0] e
addEdgeToQueue m'.Elements
areaOperation img area AreaOperation.Closing
// A simpler algorithm than 'areaOpen' on byte image but slower.
areaOperation img area AreaOperation.Closing
// A simpler algorithm than 'areaOpen' on byte image but slower.
- if not flooded.[i, j] && imgData.[i, j, 0] = byte level
- then
+ if not flooded.[i, j] && imgData.[i, j, 0] = byte level then
- let p = Point(next.X + nx, next.Y + ny)
- if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h
- then
+ let p = Point (next.X + nx, next.Y + ny)
+ if p.X >= 0 && p.X < w && p.Y >= 0 && p.Y < h then
for p in pointsChecked do
imgData.[p.Y, p.X, 0] <- maxNeighborValue
[<AllowNullLiteral>]
for p in pointsChecked do
imgData.[p.Y, p.X, 0] <- maxNeighborValue
[<AllowNullLiteral>]
-type Island (cmp: IComparer<float32>) =
- member val Shore = Heap.Heap<float32, Point>(cmp) with get
+type Island (cmp : IComparer<float32>) =
+ 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
member val Level = 0.f with get, set
member val Surface = 0 with get, set
member this.IsInfinite = this.Surface = Int32.MaxValue
let w = img.Width
let h = img.Height
let earth = img.Data
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
let w = img.Width
let h = img.Height
let earth = img.Data
let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |]
- let comparer = if op = AreaOperation.Opening
- then { 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) }
+ let comparer =
+ if op = AreaOperation.Opening then
+ { 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 }
- 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 ()
- 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
island.Shore.Add earth.[ni, nj, 0] neighbor
for area, obj in areas do
ownership.[p.Y, p.X] = island ||
(p.Y > 0 && ownership.[p.Y - 1, p.X] = island) ||
(p.Y < h - 1 && ownership.[p.Y + 1, p.X] = island) ||
ownership.[p.Y, p.X] = island ||
(p.Y > 0 && ownership.[p.Y - 1, p.X] = island) ||
(p.Y < h - 1 && ownership.[p.Y + 1, p.X] = island) ||
while not stop && island.Surface < area do
let level, next = island.Shore.Max
let other = ownership.[next.Y, next.X]
while not stop && island.Surface < area do
let level, next = island.Shore.Max
let other = ownership.[next.Y, next.X]
- 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 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
stop <- true
else // We can merge 'other' into 'surface'.
island.Surface <- island.Surface + other.Surface
island.Level <- other.Level
stop <- true
else // We can merge 'other' into 'surface'.
island.Surface <- island.Surface + other.Surface
island.Level <- other.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
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
- let neighbor = Point(nj, ni)
- if not <| ownedOrAdjacent neighbor
- then
+ let neighbor = Point (nj, ni)
+ if not <| ownedOrAdjacent neighbor then
ownership.[next.Y, next.X] <- island
island.Level <- level
island.Surface <- island.Surface + 1
ownership.[next.Y, next.X] <- island
island.Level <- level
island.Surface <- island.Surface + 1
areaOperationF img [ area, () ] None AreaOperation.Opening
/// <summary>
/// Area closing on float image.
/// </summary>
areaOperationF img [ area, () ] None AreaOperation.Opening
/// <summary>
/// Area closing on float image.
/// </summary>
/// For each area the function 'f' is called with the associated area value of type 'a and the volume difference
/// Between the previous and the current closing.
/// </summary>
/// For each area the function 'f' is called with the associated area value of type 'a and the volume difference
/// Between the previous and the current closing.
/// </summary>
areaOperationF img areas (Some f) AreaOperation.Opening
/// <summary>
/// Same as 'areaOpenFWithFun' for closing operation.
/// </summary>
areaOperationF img areas (Some f) AreaOperation.Opening
/// <summary>
/// Same as 'areaOpenFWithFun' for closing operation.
/// </summary>
areaOperationF img areas (Some f) AreaOperation.Closing
/// <summary>
/// Zhang and Suen thinning algorithm.
/// Modify 'mat' in place.
/// </summary>
areaOperationF img areas (Some f) AreaOperation.Closing
/// <summary>
/// Zhang and Suen thinning algorithm.
/// Modify 'mat' in place.
/// </summary>
let p2 = if i = 0 then 0uy else data1.[i-1, j]
let p3 = if i = 0 || j = w-1 then 0uy else data1.[i-1, j+1]
let p4 = if j = w-1 then 0uy else data1.[i, j+1]
let p2 = if i = 0 then 0uy else data1.[i-1, j]
let p3 = if i = 0 || j = w-1 then 0uy else data1.[i-1, j+1]
let p4 = if j = w-1 then 0uy else data1.[i, j+1]
(if p7 = 0uy && p8 = 1uy then 1 else 0) +
(if p8 = 0uy && p9 = 1uy then 1 else 0) +
(if p9 = 0uy && p2 = 1uy then 1 else 0) = 1 &&
(if p7 = 0uy && p8 = 1uy then 1 else 0) +
(if p8 = 0uy && p9 = 1uy then 1 else 0) +
(if p9 = 0uy && p2 = 1uy then 1 else 0) = 1 &&
/// Remove all 8-connected pixels with an area equal or greater than 'areaSize'.
/// Modify 'mat' in place.
/// </summary>
/// Remove all 8-connected pixels with an area equal or greater than 'areaSize'.
/// Modify 'mat' in place.
/// </summary>
- if data'.[i, j] = 1uy
- then
- let neighborhood = List<Point>()
- let neighborsToCheck = Stack<Point>()
- neighborsToCheck.Push(Point(j, i))
+ if data'.[i, j] = 1uy then
+ let neighborhood = List<Point> ()
+ let neighborsToCheck = Stack<Point> ()
+ neighborsToCheck.Push (Point (j, i))
- if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy
- then
- neighborsToCheck.Push(Point(pj, pi))
+ if pi >= 0 && pi < h && pj >= 0 && pj < w && data'.[pi, pj] = 1uy then
+ neighborsToCheck.Push (Point (pj, pi))
- if ny <> 0 && nx <> 0
- then
- 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)
+ if ny <> 0 && nx <> 0 then
+ 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