[OpenCV-Python] 19 Canny Edge Detection

OpenCV-Python: IV Image Processing in OpenCV

19 Canny edge detection

Goal
  • Understand the concept of Canny edge detection
  • Learning function cv2.Canny()

19.1 Principle

Canny edge detection is a very popular edge detection algorithm, proposed by John F. Canny in 1986. It is an algorithm composed of many steps, which we will introduce step by step next.

19.1.1 Noise removal

Since edge detection is easily affected by noise, the first step is to use a 5x5 Gaussian filter to remove noise, which we have already learned.

19.1.2 Calculating the image gradient

Use the Sobel operator to calculate the first derivative (image gradient) (Gx and Gy) in the horizontal and vertical directions on the smoothed image. According to the obtained two gradient maps (Gx and Gy) to find the gradient and direction of the boundary, the formula is as follows:
      Edge_Gradient ; (G) = \sqrt{G_x^2 + G_y^2}  Angle ; (\theta) = \tan^{-1} \bigg(\frac{G_y}{G_x}\bigg)
the direction of the gradient is always perpendicular to the boundary. The gradient directions are classified into four categories: vertical, horizontal, and two diagonals.

19.1.3 Non-maximum suppression

After obtaining the direction and size of the gradient, a scan of the entire image should be done to remove those non-boundary points. Check each pixel to see if the gradient of this point is the largest among the surrounding points with the same gradient direction. As shown below:

What you get now is a binary image with "narrow boundaries".

19.1.4 Hysteresis threshold

Now it is necessary to make sure that those boundaries are the real ones. At this time, we need to set two thresholds: minVal and maxVal. When the gray gradient of the image is higher than maxVal, it is considered to be a true boundary, and those lower than minVal will be discarded. If it is between the two, it depends on whether the point is connected to a boundary point that is determined to be true. If it is, it is considered to be a boundary point, and if it is not, it is discarded. As shown below:

Hysteresis Thresholding
A is higher than the threshold maxVal, so it is the true boundary point. Although C is lower than maxVal but higher than minVal and connected to A, it is also regarded as the true boundary point. And B will be discarded because it is not only lower than maxVal but also not connected to the true boundary point. So choosing the right maxVal and minVal is very important for getting good results.
In this step, some small noise points will also be removed, because we assume that the borders are all long line segments.

19.2 Canny boundary detection in OpenCV

Only one function is needed in OpenCV: cv2.Canny(), to complete the above steps.
Let's see how to use this function. The first parameter of this function is the input image. The second and third are minVal and maxVal respectively. The third parameter sets the size of the Sobel convolution kernel used to calculate the image gradient. The default value is 3. The last parameter is L2gradient, which can be used to set the gradient equation. If set to True, the equation we mentioned above will be used, otherwise, the equation will be used:. Edge_Gradient ; (G) = |G_x| + |G_y|Instead, the default value is False.

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
edges = cv2.Canny(img,100,200)

plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()

result:

My result

img

Official result

Canny Edge Detection

More resources

  1. Canny edge detector at Wikipedia
  2. Canny Edge Detection Tutorial by Bill Green, 2002.

For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113421918