A, above

Canny edge detection algorithm, Canny is a name Canny goal is to find an optimal edge detection algorithm, the meaning of the optimal edge detection is:

(1) Optimal detection: the algorithm can identify as many actual edges in the image as possible, and the probability of missing real edges and false detection of non-edges is as small as possible; (2) Optimal positioning criteria: the detected edge point is closest to the actual edge point, or the detected edge deviates from the real edge of the object to the least extent due to the influence of noise; (3) One-to-one correspondence between detection points and edge points: the edge points detected by the operator should be one-to-one correspondence with the actual edge points.

Second, Canny edge detection algorithm process

  1. Gaussian filtering is used to smooth the image in order to remove noise
  2. Look for intensity gradients of images
  3. Application of Non-maximum suppression to eliminate edge error detection (not originally but detected to be)
  4. A two-threshold method is applied to determine possible (potential) boundaries
  5. Use lag technology to track boundaries

3. Interface layout

  • aLabel
  • NButton
  • threePicture

Four, function realization

4.1 Opening an Image

 private void openFileBtn_Click(object sender, EventArgs e)
 {
     OpenFileDialog openfiledialog = new OpenFileDialog();
     openfiledialog.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
     openfiledialog.RestoreDirectory = true;
     if (openfiledialog.ShowDialog() == DialogResult.OK)
     {
         Console.WriteLine(openfiledialog.FileName);
         fileName = openfiledialog.FileName;

         //Mat src = new Mat("foo.png", LoadMode.Color);
         Mat src = new Mat(fileName);
         //Mat src = new Mat(fileName, ImreadModes.Color);
         var frameBitmap = BitmapConverter.ToBitmap(src);

         pictureBox1.Image?.Dispose();
         pictureBox1.Image = frameBitmap;
     }
 }
Copy the code

4.2 Canny edge detection – source code

private void threshold1ScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    threshold1Label.Text = threshold1ScrollBar.Value + "";
    cannyBtn_Click(sender, e);
}

private void threshold2ScrollBar_Scroll(object sender, ScrollEventArgs e)
{
    threshold2Label.Text = threshold2ScrollBar.Value + "";
    cannyBtn_Click(sender, e);
}

private void cannyBtn_Click(object sender, EventArgs e)
{
    GaussianBlurBtn_Click(sender, e);
    edges = new Mat(mInput.Rows, mInput.Cols, MatType.CV_8UC4);

    double threshold1 = threshold1ScrollBar.Value;
    double threshold2 = threshold2ScrollBar.Value;
    int apertureSize = 3;

    Cv2.Canny(blur, edges, threshold1, threshold2, apertureSize);     // Edge detection

    srcPictureBox.Image = BitmapConverter.ToBitmap(mInput);
    grayPictureBox.Image = BitmapConverter.ToBitmap(blur);
    dstPictureBox.Image = BitmapConverter.ToBitmap(edges);
}
Copy the code

4.3 Canny edge detection – Parameter description

//
/ / in this paper:
// Finds edges in an image using Canny algorithm.
//
/ / parameters:
// src:
// Single-channel 8-bit input image
//
// edges:
// The output edge map. It will have the same size and the same type as image
//
// threshold1:
// The first threshold for the hysteresis procedure
//
// threshold2:
// The second threshold for the hysteresis procedure
//
// apertureSize:
// Aperture size for the Sobel operator [By default this is ApertureSize.Size3]
//
// L2gradient:
// Indicates, whether the more accurate L2 norm should be used to compute the image
// gradient magnitude (true), or a faster default L1 norm is enough (false). [By
// default this is false]
public static void Canny(InputArray src, OutputArray edges, double threshold1, double threshold2, int apertureSize = 3.bool L2gradient = false);
Copy the code
  • threshold1The first threshold of the lag process, the lower limit, is discarded if it is lower than the lower limit
  • threshold2, the second threshold of the lag process, upper limit, if higher than the upper limit, it is considered to be edge pixel. It will only be accepted if it is between two thresholds and connected to edge pixels.
  • apertureSize, Sobel calculation aperture size, can only be selected3/5/7
  • L2gradient, the specification of L2 norm calculation method

Five, operation effect diagram

From left to right

  • The first one is the original
  • The second is the result of gaussian blur
  • The third one is the result of Canny edge detection

If you feel good, just click three times. (Like + Favorites + follow)