[VTK Study Notes-09] Image edge detection (gradient operator, Canny operator, Laplacian operator)

Learning Tutorial: "Advanced VTK Graphics and Image Development" Zhang Xiaodong, Luo Huoling
Special thanks: Dongling Studio

5.4 Edge detection

Discontinuous gray values ​​in the image will produce edges, and edge detection of the image is the basis of the boundary-based image segmentation method.The gradient actually reflects the edge information of the image. Image edges are commonly detected by the first and second derivatives of the image.

5.4.1 Gradient operator

The gradient operator corresponds to the first derivative of the image. The first derivative of the image is generally approximated by difference operations. Since the gradient image is a vector and cannot be displayed directly, it is necessary to calculate the gradient size and 2-norm of each pixel in the image, which is the modulus of the vector.

When calculating the gradient, the intermediate difference method is used, that is, the difference of pixels in each direction is the difference between the two pixel values ​​before and after. The vtkImageGradient class defines a HandleBoundaries variable internally, which determines whether to calculate the boundary pixel gradient.

Note: Color images cannot be used directly to calculate gradients and need to be converted to grayscale images first.

The Sobel operator is also a commonly used gradient operator. It uses a 3×3 template to move over the image and calculates the gradient value corresponding to the center pixel at each position.

Process:
1. Use the Sobel operator to calculate the gradient image of the image
2. Extract the gradient component in the X direction and the gradient component in the Y direction
3. Since the calculated value of the Sobel operator may be a negative value, the absolute value of each component image is calculated
4 .Adjust the value range of the image to [0, 255] by vtkImageShiftScale before displaying it.

5.4.2 Canny operator

Calculation steps:
1. Eliminate the original image noise (Gaussian smoothing)
2. Calculate the gradient of each pixel in the image, and calculate the module value and direction (difference method)
3. Perform "non-maximum suppression" on the gradient. The gradient value of image edge points is usually a maximum value in the gradient direction, so detecting edges requires assigning non-maximum values ​​to 0 to suppress non-edge points.
4. Double threshold method detects edges and connects edges. Take two gradient thresholds high and low.
Assign pixels smaller than high in the gradient image to 0 to obtain the edge image L1, which can be close to the edge of the image but may have discontinuities.
Assign 0 to the pixels lower than low in the gradient image to obtain the edge image L2. This image is greatly affected by noise, but has more edge information.
When connecting edges, L1 is used as the basis to perform edge tracking on non-zero points. If there is an interruption during the tracking process, edges that can be connected are found from the corresponding pixels in L2 and their neighborhoods until the end.

5.4.3 Laplacian operator

The Laplacian operator is a second-order edge operator, which is the divergence of the gradient. It is also implemented through templates.

The Laplacian operator calculates the second derivative of the image and is sensitive to image noise. The result of the Laplacian operator is a scalar representing the width of the edge. However, it often produces double-pixel wide edges and cannot provide direction information, so it is less directly used for edge detection.

Guess you like

Origin blog.csdn.net/m0_51141265/article/details/132915549