X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FParasitemiaCore%2FImgTools%2FEdges.fs;h=7c3e6d325be4fbca03bf41789d7f1e1b51a86513;hp=df5858ddc2493f0c294a6999524577e1c323cf42;hb=2d712781def419c9acc98368f7102b19b064f16d;hpb=1b8e45987bde692ab5602c281f878707f70459b7 diff --git a/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs b/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs index df5858d..7c3e6d3 100644 --- a/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs +++ b/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs @@ -20,30 +20,32 @@ let sensibilityLow = 0.0f /// The thresholds are automatically defined with otsu on gradient magnitudes. /// /// -let find (img: Image) : Matrix * Matrix * Matrix = +let find (img : Image) : Matrix * Matrix * Matrix = let w = img.Width let h = img.Height use sobelKernel = - new Matrix(array2D [[ -1.0f; 0.0f; 1.0f ] - [ -2.0f; 0.0f; 2.0f ] - [ -1.0f; 0.0f; 1.0f ]]) + new Matrix ( + array2D [[ -1.0f; 0.0f; 1.0f ] + [ -2.0f; 0.0f; 2.0f ] + [ -1.0f; 0.0f; 1.0f ]] + ) - let xGradient = new Matrix(img.Size) - let yGradient = new Matrix(img.Size) - CvInvoke.Filter2D(img, xGradient, sobelKernel, Point(1, 1)) - CvInvoke.Filter2D(img, yGradient, sobelKernel.Transpose(), Point(1, 1)) + let xGradient = new Matrix (img.Size) + let yGradient = new Matrix (img.Size) + CvInvoke.Filter2D (img, xGradient, sobelKernel, Point (1, 1)) + CvInvoke.Filter2D (img, yGradient, sobelKernel.Transpose (), Point (1, 1)) - use magnitudes = new Matrix(xGradient.Size) - use angles = new Matrix(xGradient.Size) - CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, angles) // Compute the magnitudes and angles. The angles are between 0 and 2 * pi. + use magnitudes = new Matrix (xGradient.Size) + use angles = new Matrix (xGradient.Size) + CvInvoke.CartToPolar (xGradient, yGradient, magnitudes, angles) // Compute the magnitudes and angles. The angles are between 0 and 2 * pi. let thresholdHigh, thresholdLow = let threshold, _, _ = otsu (histogramMat magnitudes 300) threshold + (sensibilityHigh * threshold), threshold - (sensibilityLow * threshold) // Non-maximum suppression. - use nms = new Matrix(xGradient.Size) + use nms = new Matrix (xGradient.Size) let nmsData = nms.Data let anglesData = angles.Data @@ -55,61 +57,57 @@ let find (img: Image) : Matrix * Matrix * Matrix 0.f || vy <> 0.f - then + if vx <> 0.f || vy <> 0.f then 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 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 + if m >= thresholdLow && m > mNeigbors 1 && m > mNeigbors -1 then nmsData.[i, j] <- 1uy // suppressMConnections nms // It's not helpful for the rest of the process (ellipse detection). - let edges = new Matrix(xGradient.Size) + let edges = new Matrix (xGradient.Size) let edgesData = edges.Data // Hysteresis thresholding. - let toVisit = Stack() + let toVisit = Stack () for i = 0 to h - 1 do for j = 0 to w - 1 do - if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh - then + if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh then nmsData.[i, j] <- 0uy - toVisit.Push(Point(j, i)) + toVisit.Push (Point (j, i)) while toVisit.Count > 0 do - let p = toVisit.Pop() + let p = toVisit.Pop () edgesData.[p.Y, p.X] <- 1uy for i' = -1 to 1 do for j' = -1 to 1 do - if i' <> 0 || j' <> 0 - then + 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 && nmsData.[ni, nj] = 1uy - then + if ni >= 0 && ni < h && nj >= 0 && nj < w && nmsData.[ni, nj] = 1uy then nmsData.[ni, nj] <- 0uy - toVisit.Push(Point(nj, ni)) + toVisit.Push (Point (nj, ni)) edges, xGradient, yGradient \ No newline at end of file