Update coding style.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ImgTools / Morpho.fs
index 6c8d7a0..d2826bf 100644 (file)
@@ -37,28 +37,28 @@ let inline findExtremum (img : Image<Gray, 'TDepth>) (extremumType : ExtremumTyp
     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
@@ -68,23 +68,23 @@ let inline findExtremum (img : Image<Gray, 'TDepth>) (extremumType : ExtremumTyp
 
                             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 inline findMaxima (img : Image<Gray, 'TDepth>) : IEnumerable<Points> when 'TDepth : unmanaged =
     findExtremum img ExtremumType.Maxima
@@ -94,7 +94,7 @@ let inline findMinima (img : Image<Gray, 'TDepth>) : IEnumerable<Points> when 'T
 
 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
 
@@ -103,8 +103,8 @@ type PriorityQueue () =
             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
@@ -122,8 +122,8 @@ type PriorityQueue () =
             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
@@ -150,11 +150,11 @@ type PriorityQueue () =
         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
@@ -173,7 +173,7 @@ type PriorityQueue () =
 
     member this.Clear () =
         while highest > lowest  do
-            q.[highest].Clear()
+            q.[highest].Clear ()
             highest <- highest - 1
         highest <- -1
         lowest <- size
@@ -199,33 +199,33 @@ let private areaOperation (img : Image<Gray, byte>) (area : int) (op : AreaOpera
     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
@@ -238,10 +238,10 @@ let private areaOperation (img : Image<Gray, byte>) (area : int) (op : AreaOpera
                         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
 
@@ -251,8 +251,8 @@ let private areaOperation (img : Image<Gray, byte>) (area : int) (op : AreaOpera
                         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
@@ -264,11 +264,11 @@ let private areaOperation (img : Image<Gray, byte>) (area : int) (op : AreaOpera
                                 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
@@ -280,19 +280,19 @@ let private areaOperation (img : Image<Gray, byte>) (area : int) (op : AreaOpera
                     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
@@ -331,8 +331,8 @@ let areaOpen2 (img : Image<Gray, byte>) (area : int) =
 
     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]
@@ -341,22 +341,22 @@ let areaOpen2 (img : Image<Gray, byte>) (area : int) =
                 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
 
@@ -366,7 +366,7 @@ let areaOpen2 (img : Image<Gray, byte>) (area : int) =
 
 [<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
@@ -379,29 +379,29 @@ let private areaOperationF (img : Image<Gray, float32>) (areas : (int * 'a) list
 
     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
@@ -422,23 +422,22 @@ let private areaOperationF (img : Image<Gray, float32>) (areas : (int * 'a) list
                 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 ()
@@ -449,7 +448,7 @@ let private areaOperationF (img : Image<Gray, float32>) (areas : (int * 'a) list
                                 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
@@ -566,10 +565,10 @@ let removeArea (mat : Matrix<byte>) (areaSize : int) =
         ( 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
@@ -577,19 +576,19 @@ let removeArea (mat : Matrix<byte>) (areaSize : int) =
     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
@@ -599,19 +598,19 @@ let connectedComponents (img : Image<Gray, byte>) (startPoints : List<Point>) :
     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