X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;ds=inline;f=Parasitemia%2FParasitemiaCore%2FImgTools%2FEdges.fs;h=b44c768125b3be77a1c5934c747fcf34aa2834fe;hb=refs%2Ftags%2F1.0.13;hp=b174ee9d060ed08ad4541b3e45112465cbe8a943;hpb=3f8b0d281b3058faf23dbd0363de440bd04c6574;p=master-thesis.git
diff --git a/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs b/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs
index b174ee9..b44c768 100644
--- a/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs
+++ b/Parasitemia/ParasitemiaCore/ImgTools/Edges.fs
@@ -1,6 +1,5 @@
module ParasitemiaCore.Edges
-open System
open System.Drawing
open System.Collections.Generic
@@ -11,37 +10,41 @@ open Const
open Histogram
open Otsu
+// Sensibilities of the hysteresis search.
+let sensibilityHigh = 0.1f
+let sensibilityLow = 0.0f
+
///
/// Find edges of an image by using the Canny approach.
/// 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.
+ 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 sensibilityHigh = 0.1f
- let sensibilityLow = 0.0f
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
@@ -49,73 +52,61 @@ 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()
- for i in 0 .. h - 1 do
- for j in 0 .. w - 1 do
- if nmsData.[i, j] = 1uy && magnitudesData.[i, j] >= thresholdHigh
- then
+ 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
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' in -1 .. 1 do
- for j' in -1 .. 1 do
- if i' <> 0 || j' <> 0
- then
+ for i' = -1 to 1 do
+ for j' = -1 to 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 && 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