Cleaning and some little tweaks.
[master-thesis.git] / Parasitemia / ParasitemiaCore / ImgTools / Edges.fs
index b174ee9..a1b6033 100644 (file)
@@ -11,6 +11,10 @@ open Const
 open Histogram
 open Otsu
 
+// Sensibilities of the hysteresis search.
+let sensibilityHigh = 0.1f
+let sensibilityLow = 0.0f
+
 /// <summary>
 /// Find edges of an image by using the Canny approach.
 /// The thresholds are automatically defined with otsu on gradient magnitudes.
@@ -21,9 +25,9 @@ let find (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<f
     let h = img.Height
 
     use sobelKernel =
-        new Matrix<float32>(array2D [[ 1.0f; 0.0f; -1.0f ]
-                                     [ 2.0f; 0.0f; -2.0f ]
-                                     [ 1.0f; 0.0f; -1.0f ]])
+        new Matrix<float32>(array2D [[ -1.0f; 0.0f; 1.0f ]
+                                     [ -2.0f; 0.0f; 2.0f ]
+                                     [ -1.0f; 0.0f; 1.0f ]])
 
     let xGradient = new Matrix<float32>(img.Size)
     let yGradient = new Matrix<float32>(img.Size)
@@ -32,11 +36,9 @@ let find (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<f
 
     use magnitudes = new Matrix<float32>(xGradient.Size)
     use angles = new Matrix<float32>(xGradient.Size)
-    CvInvoke.CartToPolar(xGradient, yGradient, magnitudes, angles) // Compute the magnitudes and angles.
+    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)
 
@@ -49,14 +51,6 @@ let find (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<f
     let xGradientData = xGradient.Data
     let yGradientData = yGradient.Data
 
-    for i in 0 .. h - 1 do
-        nmsData.[i, 0] <- 0uy
-        nmsData.[i, w - 1] <- 0uy
-
-    for j in 0 .. w - 1 do
-        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 = xGradientData.[i, j]
@@ -93,6 +87,9 @@ let find (img: Image<Gray, float32>) : Matrix<byte> * Matrix<float32> * Matrix<f
 
     // suppressMConnections nms // It's not helpful for the rest of the process (ellipse detection).
 
+    IO.saveMat magnitudes "magnitudes.png"
+    IO.saveMat nms "nms.png"
+
     let edges = new Matrix<byte>(xGradient.Size)
     let edgesData = edges.Data