The last article introduced some basic concepts of edge detection. This paper is the second part of the edge detection series, introducing two mature edge detection algorithms: Sobel operator and Canny operator.
Sobel operator
Sobel operator is an edge detection algorithm based on gradient. In the last article, we have come to the conclusion that the gradient is large in the position of the edge in the image. The Sobel operator uses two convolution kernels of 3×33 × times33×3 to convolve with the image and get the gradient of each place (the two convolution kernels are x direction and Y direction respectively).
The two convolution kernels of Sobel operator are as follows:
Decomposition of Sobel convolution kernel
The above Sobel convolution kernel can be decomposed into two parts, namely, Gaussian smoothing and gradient calculation:
\left[\begin{matrix} 1\\ 2\ 1\ end{matrix} right]⎣⎢, repeatable, repeatable, repeatable, repeatable [+10−1] \left[\begin{matrix} + 1&0&-1 \end{matrix} \right][+10−1] is used to calculate the gradient.
Sobel operator
As in the previous article, the magnitude and direction of the results are calculated using the following formula:
In a word, Sobel operator can perform image smoothing and gradient calculation in one step. The place where the median value of the image obtained by Sobel operator is larger is the location of the edge.
As shown below, the upper left is the original image, the upper right and lower left are GyG_yGy and GxG_xGx respectively, and the lower right is the result of GGG. The brighter place in the figure below on the right is the edge of the image (a certain threshold value is generally selected during implementation, and pixels larger than the threshold value are considered as edges).
Defects of Sobel operator
Sobel operator is a simple edge detection method, which has the following two defects:
- Poor locality: GGG is only a simple calculated gradient, and several pixels near the edge will have large values, resulting in the judged edge is “thick”.
- It is sensitive to specific directions: Sobel operator only calculates gradients in the transverse and longitudinal directions. For oblique edges, it is easy to be omitted, and false positives are easy to appear in these two directions
Canny operator
Compared with simple Sobel operator, Canny operator considers more situations and has more accurate results. It is also a more commonly used edge detection algorithm in computer vision.
The Sobel operator in the previous section completes two steps: first, gaussian smoothing is used to deal with noise; second, the intensity and direction of image gradient are calculated. The disadvantage is that the results are not localized well enough. On the basis of Sobel operator (that is, Canny operator first needs to calculate the results of Sobel operator), the Canny operator continues to complete two steps:
- Non-maximum suppression
- The hysteresis threshold is used to filter the edges
Nonmaximum suppression
In the previous section, we came to the conclusion that localization of Sobel operator is not good enough. The problem is that we consider pixels whose gradient intensity is greater than the threshold value as edges. But in practice, assuming the gradient value of a region as shown in the figure below, only the position of the maximum should be judged as an edge.
As shown in the figure below, suppose ∣ ∇ G ∣ (x, y) | | \ nabla G (x, y) ∣ ∇ G ∣ (x, y) is a point (x, y) (x, y) (x, y) gradient values, is of great value to suppress the formula is as follows (x ‘x’ x ‘and’ x ‘x’ ‘ ‘x’ is XXX in the edge of the tangential direction of the adjacent point)
The following figure shows the changes of the edges after non-maximum pressing. It can be found that non-maximum pressing makes the edges more refined.
Hysteresis threshold
So far, we have judged the edge by a single threshold value, that is, the point whose gradient value after processing is larger than the threshold value is considered as the edge. However, it is difficult to select a single threshold. If the threshold is too high, edge discontinuity will be easily caused (left figure below), while if the threshold is too low, other noises will be introduced (right figure below).
The hysteresis threshold method solves this problem by setting two thresholds (a high threshold H and a low threshold L) :
- If the gradient value is less than L, it is not used as an edge
- If the gradient value is greater than H, it is regarded as a strong edge
- If the gradient value is between L and H, it is a weak edge
A strong edge is directly considered an edge, while a weak edge is selected as an edge if:
- It’s directly attached to the strong edge
- It is connected to a strong edge by adjacent weak edges
Canny edge detection algorithm flow
So far we have covered all of the processes in Canny, summarized as follows:
- Firstly, gaussian smoothing was performed on the image and the gradients of XXX and YYY directions were calculated
- Compute the value and direction of the gradient
- Non-maximum pressing – reduce edge width
- Hysteresis threshold method is used for screening
The final effect is as follows:
This paper summarizes
This paper introduces two kinds of edge detection operators, Sobel operator and Canny operator. Sobel operator is basic and simple, Canny operator is more commonly used and robust. Stay tuned for the Python implementation of the Canny edge detection algorithm in the next article.