X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FParasitemia%2FImgTools.fs;h=f64b1c3318d687d6dc314fe98f4dbdd125446d6f;hp=5e32ccb2ac487703915d8efa9b21efe0a583184f;hb=044b0ae69df3ac565432545b2fa934589016f9bd;hpb=dcf3645b3426991237567e90bab9806a9c111cd1 diff --git a/Parasitemia/Parasitemia/ImgTools.fs b/Parasitemia/Parasitemia/ImgTools.fs index 5e32ccb..f64b1c3 100644 --- a/Parasitemia/Parasitemia/ImgTools.fs +++ b/Parasitemia/Parasitemia/ImgTools.fs @@ -8,17 +8,18 @@ open System.Linq open Emgu.CV open Emgu.CV.Structure -open Utils open Heap +open Const +open Utils // Normalize image values between 0uy and 255uy. -let normalizeAndConvert (img: Image) : Image = +let normalizeAndConvert (img: Image) : Image = let min = ref [| 0.0 |] let minLocation = ref <| [| Point() |] let max = ref [| 0.0 |] let maxLocation = ref <| [| Point() |] img.MinMax(min, max, minLocation, maxLocation) - ((img - (!min).[0]) / ((!max).[0] - (!min).[0]) * 255.0).Convert() + ((img.Convert() - (!min).[0]) / ((!max).[0] - (!min).[0]) * 255.0).Convert() let saveImg (img: Image<'TColor, 'TDepth>) (filepath: string) = @@ -45,7 +46,8 @@ let suppressMConnections (img: Matrix) = then img.[i, j] <- 0uy -let findEdges (img: Image) : Matrix * Image * Image = + +let findEdges (img: Image) : Matrix * Image * Image = let w = img.Width let h = img.Height @@ -54,94 +56,114 @@ let findEdges (img: Image) : Matrix * Image * [ 2.0f; 0.0f; -2.0f ] [ 1.0f; 0.0f; -1.0f ]], Point(1, 1)) - let xGradient = img.Convolution(sobelKernel).Convert() - let yGradient = img.Convolution(sobelKernel.Transpose()).Convert() + let xGradient = img.Convolution(sobelKernel) + let yGradient = img.Convolution(sobelKernel.Transpose()) let xGradientData = xGradient.Data let yGradientData = yGradient.Data for r in 0 .. h - 1 do - xGradientData.[r, 0, 0] <- 0.0 - xGradientData.[r, w - 1, 0] <- 0.0 - yGradientData.[r, 0, 0] <- 0.0 - yGradientData.[r, w - 1, 0] <- 0.0 + xGradientData.[r, 0, 0] <- 0.f + xGradientData.[r, w - 1, 0] <- 0.f + yGradientData.[r, 0, 0] <- 0.f + yGradientData.[r, w - 1, 0] <- 0.f for c in 0 .. w - 1 do - xGradientData.[0, c, 0] <- 0.0 - xGradientData.[h - 1, c, 0] <- 0.0 - yGradientData.[0, c, 0] <- 0.0 - yGradientData.[h - 1, c, 0] <- 0.0 + xGradientData.[0, c, 0] <- 0.f + xGradientData.[h - 1, c, 0] <- 0.f + yGradientData.[0, c, 0] <- 0.f + yGradientData.[h - 1, c, 0] <- 0.f - use magnitudes = new Matrix(xGradient.Size) - CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, new Mat()) // Compute the magnitudes (without angles). + use magnitudes = new Matrix(xGradient.Size) + use angles = new Matrix(xGradient.Size) + CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, angles) // Compute the magnitudes (without angles). let thresholdHigh, thresholdLow = - let sensibility = 0.1 + let sensibilityHigh = 0.1f + let sensibilityLow = 0.1f use magnitudesByte = magnitudes.Convert() - let threshold = CvInvoke.Threshold(magnitudesByte, magnitudesByte, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) - threshold + (sensibility * threshold), threshold - (sensibility * threshold) + let threshold = float32 <| CvInvoke.Threshold(magnitudesByte, magnitudesByte, 0.0, 1.0, CvEnum.ThresholdType.Otsu ||| CvEnum.ThresholdType.Binary) + threshold + (sensibilityHigh * threshold), threshold - (sensibilityLow * threshold) // Non-maximum suppression. use nms = new Matrix(xGradient.Size) - nms.SetValue(1.0) + + let nmsData = nms.Data + let anglesData = angles.Data + let magnitudesData = magnitudes.Data + let xGradientData = xGradient.Data + let yGradientData = yGradient.Data + + let PI = float32 Math.PI for i in 0 .. h - 1 do - nms.Data.[i, 0] <- 0uy - nms.Data.[i, w - 1] <- 0uy + nmsData.[i, 0] <- 0uy + nmsData.[i, w - 1] <- 0uy for j in 0 .. w - 1 do - nms.Data.[0, j] <- 0uy - nms.Data.[h - 1, j] <- 0uy + nmsData.[0, j] <- 0uy + nmsData.[h - 1, j] <- 0uy for i in 1 .. h - 2 do for j in 1 .. w - 2 do - let vx = xGradient.Data.[i, j, 0] - let vy = yGradient.Data.[i, j, 0] - let angle = - let a = atan2 vy vx - if a < 0.0 then 2. * Math.PI + a else a - - let mNeigbors (sign: int) : float = - if angle < Math.PI / 8. || angle >= 15.0 * Math.PI / 8. then magnitudes.Data.[i, j + sign] - elif angle < 3.0 * Math.PI / 8. then magnitudes.Data.[i + sign, j + sign] - elif angle < 5.0 * Math.PI / 8. then magnitudes.Data.[i + sign, j] - elif angle < 7.0 * Math.PI / 8. then magnitudes.Data.[i + sign, j - sign] - elif angle < 9.0 * Math.PI / 8. then magnitudes.Data.[i, j - sign] - elif angle < 11.0 * Math.PI / 8. then magnitudes.Data.[i - sign, j - sign] - elif angle < 13.0 * Math.PI / 8. then magnitudes.Data.[i - sign, j] - else magnitudes.Data.[i - sign, j + sign] - - let m = magnitudes.Data.[i, j] - if m < mNeigbors 1 || m < mNeigbors -1 || m < thresholdLow + let vx = xGradientData.[i, j, 0] + let vy = yGradientData.[i, j, 0] + if vx <> 0.f || vy <> 0.f then - nms.Data.[i, j] <- 0uy + let angle = anglesData.[i, j] + + let vx', vy' = abs vx, abs vy + let ratio2 = if vx' > vy' then vy' / vx' else vx' / vy' + let ratio1 = 1.f - ratio2 + + let mNeigbors (sign: int) : float32 = + if angle < PI / 4.f + then ratio1 * magnitudesData.[i, j + sign] + ratio2 * magnitudesData.[i + sign, j + sign] + elif angle < PI / 2.f + then ratio2 * magnitudesData.[i + sign, j + sign] + ratio1 * magnitudesData.[i + sign, j] + elif angle < 3.f * PI / 4.f + then ratio1 * magnitudesData.[i + sign, j] + ratio2 * magnitudesData.[i + sign, j - sign] + elif angle < PI + then ratio2 * magnitudesData.[i + sign, j - sign] + ratio1 * magnitudesData.[i, j - sign] + elif angle < 5.f * PI / 4.f + then ratio1 * magnitudesData.[i, j - sign] + ratio2 * magnitudesData.[i - sign, j - sign] + elif angle < 3.f * PI / 2.f + then ratio2 * magnitudesData.[i - sign, j - sign] + ratio1 * magnitudesData.[i - sign, j] + elif angle < 7.f * PI / 4.f + then ratio1 * magnitudesData.[i - sign, j] + ratio2 * magnitudesData.[i - sign, j + sign] + else ratio2 * magnitudesData.[i - sign, j + sign] + ratio1 * magnitudesData.[i, j + sign] + + let m = magnitudesData.[i, j] + if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1 + then + nmsData.[i, j] <- 1uy - // suppressMConnections nms // It's not usefull for the rest of the process (ellipse detection). + // suppressMConnections nms // It's not helpful for the rest of the process (ellipse detection). let edges = new Matrix(xGradient.Size) + let edgesData = edges.Data - // Histeresis thresholding. + // Hysteresis thresholding. let toVisit = Stack() for i in 0 .. h - 1 do for j in 0 .. w - 1 do - if nms.Data.[i, j] = 1uy && magnitudes.Data.[i, j] >= thresholdHigh + if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh then - nms.Data.[i, j] <- 0uy + nmsData.[i, j] <- 0uy toVisit.Push(Point(j, i)) while toVisit.Count > 0 do let p = toVisit.Pop() - edges.Data.[p.Y, p.X] <- 1uy + edgesData.[p.Y, p.X] <- 1uy for i' in -1 .. 1 do for j' in -1 .. 1 do if i' <> 0 || j' <> 0 then let ni = p.Y + i' let nj = p.X + j' - if ni >= 0 && ni < h && nj >= 0 && nj < w && nms.Data.[ni, nj] = 1uy + if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy then - nms.Data.[ni, nj] <- 0uy + nmsData.[ni, nj] <- 0uy toVisit.Push(Point(nj, ni)) - edges, xGradient, yGradient @@ -152,7 +174,7 @@ let gaussianFilter (img : Image<'TColor, 'TDepth>) (standardDeviation : float) : type Points = HashSet -let drawPoints (img: Image) (points: Points) (intensity: byte) = +let drawPoints (img: Image) (points: Points) (intensity: 'TDepth) = for p in points do img.Data.[p.Y, p.X, 0] <- intensity @@ -160,7 +182,7 @@ type ExtremumType = | Maxima = 1 | Minima = 2 -let findExtremum (img: Image) (extremumType: ExtremumType) : IEnumerable = +let findExtremum (img: Image) (extremumType: ExtremumType) : IEnumerable = let w = img.Width let h = img.Height let se = [| -1, 0; 0, -1; 1, 0; 0, 1 |] @@ -225,10 +247,11 @@ let findExtremum (img: Image) (extremumType: ExtremumType) : IEnumer result.Select(fun l -> Points(l)) -let findMaxima (img: Image) : IEnumerable = +let findMaxima (img: Image) : IEnumerable = findExtremum img ExtremumType.Maxima -let findMinima (img: Image) : IEnumerable = + +let findMinima (img: Image) : IEnumerable = findExtremum img ExtremumType.Minima @@ -479,6 +502,117 @@ let areaOpen (img: Image) (area: int) = let areaClose (img: Image) (area: int) = areaOperation img area AreaOperation.Closing +[] +type Island (cmp: IComparer) = + member val Shore = Heap.Heap(cmp) with get + member val Level = 0.f with get, set + member val Surface = 0 with get, set + + +let private areaOperationF (img: Image) (area: int) (op: AreaOperation) = + 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 with member this.Compare(v1, v2) = v1.CompareTo(v2) } + else { new IComparer 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() + 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() + 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 && ownership.[ni, nj] = null && not (shorePoints.Contains(neighbor)) + then + shorePoints.Add(neighbor) |> ignore + island.Shore.Add earth.[ni, nj, 0] neighbor + + for island in islands do + let mutable stop = island.Shore.IsEmpty + + // 'true' if 'p' is owned or adjacent to 'island'. + let ownedOrAdjacent (p: Point) : bool = + 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) || + (p.X > 0 && ownership.[p.Y, p.X - 1] = island) || + (p.X < w - 1 && ownership.[p.Y, p.X + 1] = island) + + while not stop && island.Surface < area do + let level, next = island.Shore.Max + let other = ownership.[next.Y, next.X] + if other = island // During merging, some points on the shore may be owned by the island itself -> ignored. + then + island.Shore.RemoveNext () + else + if other <> null + then // We touching another island. + if island.Surface + other.Surface >= area + then + stop <- true + else // We can merge 'other' into 'surface'. + island.Surface <- island.Surface + other.Surface + island.Level <- if comparer.Compare(island.Level, other.Level) > 0 then island.Level else 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 + other.Shore.Clear() + + elif comparer.Compare(level, island.Level) > 0 + then + stop <- true + else + island.Shore.RemoveNext () + for i, j in se do + let ni = i + next.Y + let nj = j + next.X + if ni < 0 || ni >= h || nj < 0 || nj >= w + then + island.Surface <- Int32.MaxValue + stop <- true + else + let neighbor = Point(nj, ni) + if not <| ownedOrAdjacent neighbor + then + island.Shore.Add earth.[ni, nj, 0] neighbor + if not stop + then + ownership.[next.Y, next.X] <- island + island.Level <- level + island.Surface <- island.Surface + 1 + + for i in 0 .. h - 1 do + for j in 0 .. w - 1 do + let island = ownership.[i, j] + if island <> null + then + earth.[i, j, 0] <- island.Level + () + + +let areaOpenF (img: Image) (area: int) = + areaOperationF img area AreaOperation.Opening + +let areaCloseF (img: Image) (area: int) = + areaOperationF img area AreaOperation.Closing + // A simpler algorithm than 'areaOpen' but slower. let areaOpen2 (img: Image) (area: int) = let w = img.Width @@ -599,7 +733,7 @@ let removeArea (mat: Matrix) (areaSize: int) = ( 0, -1) // p8 (-1, -1) |] // p9 - let mat' = new Matrix(mat.Size) + use mat' = new Matrix(mat.Size) let w = mat'.Width let h = mat'.Height mat.CopyTo(mat') @@ -667,21 +801,21 @@ let drawEllipse (img: Image<'TColor, 'TDepth>) (e: Types.Ellipse) (color: 'TColo if alpha >= 1.0 then - img.Draw(Ellipse(PointF(float32 e.Cx, float32 e.Cy), SizeF(2. * e.B |> float32, 2. * e.A |> float32), float32 <| e.Alpha / Math.PI * 180.), color, 1, CvEnum.LineType.AntiAlias) + 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) else - let windowPosX = e.Cx - e.A - 5.0 - let gapX = windowPosX - (float (int windowPosX)) + let windowPosX = e.Cx - e.A - 5.f + let gapX = windowPosX - (float32 (int windowPosX)) - let windowPosY = e.Cy - e.A - 5.0 - let gapY = windowPosY - (float (int windowPosY)) + let windowPosY = e.Cy - e.A - 5.f + let gapY = windowPosY - (float32 (int windowPosY)) - let roi = Rectangle(int windowPosX, int windowPosY, 2. * (e.A + 5.0) |> int, 2.* (e.A + 5.0) |> int) + let roi = Rectangle(int windowPosX, int windowPosY, 2.f * (e.A + 5.f) |> int, 2.f * (e.A + 5.f) |> int) img.ROI <- roi if roi = img.ROI // We do not display ellipses touching the edges (FIXME) then use i = new Image<'TColor, 'TDepth>(img.ROI.Size) - i.Draw(Ellipse(PointF(float32 <| (e.A + 5. + gapX) , float32 <| (e.A + 5. + gapY)), SizeF(2. * e.B |> float32, 2. * e.A |> float32), float32 <| e.Alpha / Math.PI * 180.), color, 1, CvEnum.LineType.AntiAlias) + 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) CvInvoke.AddWeighted(img, 1.0, i, alpha, 0.0, img) img.ROI <- Rectangle.Empty