Current location - Music Encyclopedia - Chinese History - Image analysis: gradient operator in edge detection
Image analysis: gradient operator in edge detection
Edge detection is the first step of image segmentation method based on boundary. Edge is the place where the gray value between two different adjacent areas is discontinuous or abrupt. Therefore, the detection edge is the place where the gray level changes obviously. By calculating the differential of gray level, the obvious change of gray level at the edge position can be detected. First-order differential and second-order differential are usually used to detect edges. At the edge position, the amplitude of the first differential will have a local extreme value, and the amplitude of the second differential will have a zero crossing point. This paper mainly introduces the first-order differential operator-gradient operator in edge detection, including Roberts, Prewitt and Sobel.

If we want to calculate the gradient map, we must design the template convolution. First, you must know the coordinate system of the image when calculating. The templates corresponding to many blog posts do not match the coordinate system. We mainly use computational coordinate system in convolution operation.

The coordinate system used by the photographer on the left is often used for image calculation. Its coordinate origin is in the upper left corner, and the X axis extends horizontally to the right. Y is vertical and extends downward. It can not only represent this image, but also represent the values of pixels in coordinates.

Lena coordinate system on the right is often used for screen display, because screen scanning is carried out from left to right and from top to bottom. The origin is in the upper left corner of the image, the vertical axis marks the rows of the image, and the horizontal axis marks the columns of the image. It can not only represent this image, but also represent the image value at the intersection of rows and columns.

The first thing we need to know is that the gradient is a vector. If it is a vector, it has a direction and a size. The gradient direction points to the direction where the function changes the fastest, and the magnitude is its modulus and maximum rate of change. For a binary function, its gradient at this point is, or, it is:

Wherein, the amplitude and direction angle of the gradient vector are

The following figure shows the mathematical expression of the grayscale map. The gray value of a pixel is that it has eight neighborhoods.

The gradient of the image at this point is

In ...

That is, the horizontal direction corresponding to the image and the vertical direction corresponding to the water image.

To understand the generation of gradient graph, we must first understand the process of template convolution.

Template convolution is a way of template operation, and its steps are as follows:

(1) roams the template in the input image and overlaps the center of the template with the pixel position in the image;

(2) Multiplying each coefficient on the template with the gray level of each corresponding pixel under the template;

(3) Add all products (in order to keep the gray range, the result is often divided by the sum of template coefficients, and if the template sum of gradient operator is 0, division is not necessary);

(4) Assign the above operation result (the response output of the template) to the pixel corresponding to the central position of the template in the output image.

In fact, the generation of gradient map is the same as template convolution, but the difference is that it needs to calculate the gradient amplitude of this point after template convolution, and take this amplitude as the pixel value to complete the calculation. .

The following figure shows the horizontal template and vertical template used to generate gradient graph:

For example, if you only want to generate a gradient map in the horizontal direction, then only the gradient in the horizontal direction is calculated, and the gray value of the corresponding point on the gradient map is

Generally, a template is used in the horizontal direction and a template is used in the vertical direction, and then merged, so the gray value of the gradient map at this point is

It is the magnitude of the gradient we mentioned above, which is calculated with 2 as the norm, corresponding to Euclidean distance. Because it involves square sum and square root operations, the amount of calculation is relatively large. How to simplify the calculation? Change it to an approximate calculation method! ! ! )

When calculating the actual slope map output, a simple calculation method is adopted, and the quota is 1 (corresponding to the urban distance), namely

Another simple method is to take it as a specification (corresponding to the chessboard distance), that is

First, understand the design of the lower gradient operator, which is generally horizontal and vertical. The horizontal formwork is folded in half after transposition, which is the vertical direction.

Its essence is a diagonal gradient operator, and the corresponding horizontal and vertical gradients are

The gray value of the output gradient image is

Advantages: accurate edge positioning

Disadvantages: (1) does not describe the gray level changes in horizontal and vertical directions, but only focuses on the diagonal direction, which is easy to cause omissions. (2) poor robustness. Because the points themselves participate in gradient calculation, noise interference can not be effectively suppressed.

Suitable for images with obvious edges and less noise.

Prewitt operator is a typical template, and its template center corresponds to the original image coordinates that need gradient, and the corresponding gray values of 8 neighborhood pixels are shown in the following table:

After convolution by the horizontal template of Prewitt operator, the corresponding horizontal gradient is

After convolution by the vertical template of Prewitt operator, the corresponding vertical gradient is

The gray value of the output gradient image is

Prewitt operator introduces an operation similar to local average, which has a smoothing effect on noise and can suppress noise better than Roberts operator.

After convolution by the horizontal template of Prewitt operator, the corresponding horizontal gradient is

After convolution by the vertical template of Prewitt operator, the corresponding vertical gradient is

The gray value of the output gradient image is

Sobel operator introduces an operation similar to local weighted average, and its edge location ratio is better than Prewitt operator.

Python calls OpenCV interface to realize Sobel operator edge detection.