Digital image processing (14) Canny edge detection

Premise: A troublesome problem in edge extraction based on differential operators (Sobel, etc.) is how to select an appropriate threshold so that edges can be extracted from details. Depending on the selection of the threshold, the extracted boundary information is different.
Canny gives three indicators for evaluating edge detection performance:

  1. A good signal-to-noise ratio means that the probability of judging a non-edge point as an edge point is low, and the probability of judging an edge point as a non-edge point is low;
  2. Good positioning performance, that is, the detected edge point should be as close as possible to the center of the actual edge;
  3. There is only a unique response to a single edge, i.e. the probability of multiple responses from a single edge is low, and spurious boundaries are maximally suppressed.

To put it simply, it is hoped that while improving the sensitivity to the edge of the scene, the method that can suppress noise is the best edge extraction method.

Canny algorithm process

  1. Gaussian filtering.
    The main purpose of filtering is to reduce noise, and Gaussian filtering will smooth (blur) the image. The Gaussian distribution (normal distribution) surface is a curved surface with a large middle and small sides. The general filter size is 5 × 5 5\times 55×5

  2. Calculate the image gradient and magnitude
    Use the Sobel operator (not necessarily using Sobel) in the horizontal and vertical directions to calculate the horizontal edge G ( x ) G(x)G ( x ) and the vertical edgeG ( y ) G(y)G ( y ) . Then calculate the gradient value and gradient direction for each pixel (the gradient direction is the normal direction of the edge).
    Edge _ G radient ( G ) = ( G x ) 2 + ( G y ) 2 Edge\_Gradient(G)=\sqrt{(G_x)^2+(G_y)^2}Edge_Gradient(G)=(Gx)2+(Gy)2
    Angle ( θ ) = arctan ⁡ ( G y G x ) Angle(\theta)=\arctan(\frac{G_y}{G_x})Angle(θ)=arctan (GxGy)
    insert image description here

  3. Non-maximum suppression
    is an important step in edge detection during non-maximum suppression. In the popular sense, it is to find the local maximum value of the pixel, and compare the central pixel x with the two pixel values ​​​​along its corresponding gradient direction. If If the center pixel is the maximum value, it is reserved, otherwise the center pixel is set to 0. In this way, non-maximum values ​​can be suppressed, and the pixels with the largest local gradient can be reserved to refine the edges.
    insert image description here

As shown in the figure above, it is necessary to compare the central pixel with the two pixels in the gradient direction, but since the pixels in the image are discrete two-dimensional matrices, there are no real pixels in the gradient direction. For this non-existent pixel, it must be obtained by interpolating the points on both sides.
Due to the use of interpolation to solve the gradient value, the amount of calculation is relatively large. In John Canny's paper proposing the Canny operator, an approximate processing method was used for non-maximum suppression. Quantize the gradient direction of each point to 0°, 45°, 90°, 135°.
insert image description here
As shown in the figure above, the quantization process is to process the gradient direction calculated in the previous step. If the gradient direction belongs to area 1, it will be quantized to 0°; if the gradient direction belongs to area 2, it will be quantized to 45°. ; If the gradient direction belongs to region 3, it is quantized to 90°; if the gradient direction belongs to region 4, it is quantized to 135°.
insert image description here
The above picture is a schematic diagram of non-maximum suppression. The image uses the color depth to represent the gradient amplitude. After NMS, the original edge is refined, which means that ** is a single point response at the real edge **.
4. Hysteresis Thresholding
After using non-maximum suppression, the remaining pixels can more accurately represent the actual edges in the image. But there are still some edge pixels due to noise and color variation. To solve this problem, it is necessary to filter edge pixels with weak gradient values ​​and keep edge pixels with high gradient values.
Set a high threshold and a low threshold.
a. If the magnitude of a certain pixel exceeds the high threshold, the pixel is retained as an edge pixel.
b. If the magnitude of a certain pixel is smaller than the low threshold, the pixel is excluded.
c. If the magnitude of a certain pixel is between two thresholds, the pixel is classified as edge or non-edge according to the connectivity. Specifically: if the pixel is adjacent to a pixel point determined to be an edge, it is determined to be an edge; otherwise, it is not an edge.
Reference blog: https://blog.csdn.net/leonardohaig/article/details/122141568
Code address: https://github.com/jinay1991/imagefilters
If you have any questions, please leave a message in the comment area.

Guess you like

Origin blog.csdn.net/qq_41596730/article/details/127608577