The principle of canny edge detection algorithm and its VC implementation (I.)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/likezhaobin/article/details/6892176

The edge of image refers to the part where the brightness of the image is changed significantly, and the gray section of the region can be regarded as a step, which changes from one gray value to another in a very small buffer area to a gray scale with a large difference. The edge of the image is focused on the most information of the image, the determination and extraction of image edge is very important for the whole image scene recognition and understanding, and is also an important feature that the image segmentation relies on, the edge detection is mainly the measurement, detection and localization of the gray scale change of the image, since the 1959 edge detection has been proposed, After more than 50 years of development, there are many different methods of edge detection. According to the author's understanding and Practice, this paper describes the principle of edge detection, based on which the implementation of the canny detection algorithm is discussed in detail.

The contents of this article are programmed to verify, in the implementation process, there are any errors or shortcomings of the common discussion (this article does not describe the boring theoretical proof and mathematical deduction, only from the implementation of the algorithm and improvement of the principle and engineering description).

1. Edge detection principle and procedure

In the previous blog post, the author introduced the Basic principles of two-dimensional image edge detection in a gradual manner, starting with the leap detection of one-dimensional function. The conclusion is: to realize the edge detection of image, it is to use the discrete gradient approximation function to find the gray-level transition position of the image gray matrix based on the two-dimensional gray matrix gradient vector, and then connect the points in the image to form the so-called Image edge (image edge is a general term, including the edge of the two-dimensional image, Primitives such as corner points, textures, etc.).

In the actual situation, the ideal grayscale step and its line edge image is seldom seen, and most of the sensor components have low-frequency filtering characteristics, which will make the step edge into a slope edge, it appears that the intensity of the change is not instantaneous, but across a certain distance. This makes the first work in edge detection is filtering.

1) Filtering : The algorithm of edge detection is mainly based on the first and second derivative of image strength, but the derivative is usually sensitive to noise, so the filter must be used to improve the performance of noise-related edge detectors. the common filtering methods mainly include Gaussian filtering, that is, using the discrete Gaussian function to produce a set of normalized Gaussian nuclei (see the "Gaussian filtering principle and its programming discretization implementation method"), and then based on the Gaussian kernel function of the image gray matrix of each point weighted summation (concrete program implementation see below).

2) Enhancement : The basis of edge enhancement is to determine the value of the neighborhood intensity of each point of the image. The enhancement algorithm can highlight the point where the intensity value of the image Gray Point neighborhood is significantly changed. when the specific programming is implemented, it can be determined by calculating the gradient amplitude.

3) Detection : After enhanced image, often in the neighborhood there are many points in the gradient value is relatively large, and in particular applications, these points are not the edge we are looking for, so we should adopt some method to choose these points. in practical engineering, the commonly used method is to detect by means of threshold value.

2, Canny edge detection algorithm principle

Johncanny proposed the canny operator in 1986, which is similar to the Marr (LoG) edge detection method, and also belongs to the method of first smoothing the derivative number. This section introduces the principle of the canny detection algorithm based on the edge detection process described above.

2.1 Grayscale of the original image

The canny algorithm typically processes images in grayscale, so if the camera gets a color image, it first has to be grayscale. The grayscale of a color graph is weighted average according to the sampled values of each channel of the image. In the RGB format of the color map as an example, usually the use of grayscale methods mainly include:

Method 1:gray= (R+G+B)/3;

Method 2:gray=0.299r+0.587g+0.114b; (this parameter takes into account the physiological characteristics of the human eye)

Note 1: As for other formats of color images, can be converted to RGB based on the corresponding conversion relationship and then grayscale;

NOTE 2: When programming, be aware that the order of RGB in the image format is usually BGR.

2.2 Gaussian filtering of images

The realization of Gaussian filter can be realized by using two one Gaussian core two times weighting, or by a two-Gaussian core one-time convolution.

1) Gaussian core implementation

The Gaussian function is discretized, and the one-dimensional kernel vector can be obtained by determining the parameters.

The Ivigos function is discretized, and the two-dimensional kernel vectors can be obtained by determining the parameters.

Note 1: The value of the parameter sigma is described in the previous blog post.

Note 2: After the Gaussian kernel is obtained, the whole nucleus should be normalized.

2) Image Gaussian filter

Gaussian filtering of the image sounds very iffy, in fact, according to the pixel points to be filtered and the gray values of the neighboring points according to a certain parameter rules for weighted average. This effectively filters out the high-frequency noise superimposed in the ideal image.

Often filtering and edge detection are contradictory concepts, and suppressing noise can blur the edges of the image, which increases the uncertainty of edge positioning, while improving the sensitivity of edge detection and noise. The actual engineering experience shows that the kernel determined by Gauss function can provide a good compromise between anti-noise interference and precise positioning of edge detection. This is called Gaussian image filtering, the implementation code is shown below.

2.3 Calculating the amplitude and direction of the gradient using the finite difference of the first-order biased derivative

About the image gray merit gradient can be approximated by using first-order finite difference, so that the two matrices of the partial derivative of the image in the X and Y directions can be obtained. There are several common gradient operators:

1) Roberts operator

The formula is calculated for its x and y-directional partial derivative, and the gradient amplitude of each point can be expressed by a mathematical formula:

2) Sobel operator

The above three matrices are the X-to-convolution template of the operator, the Y-to-convolution template, and the neighborhood point marker matrix of the processing point, whereby the gradient amplitude of each point can be expressed by the mathematical formula:

3) Prewitt operator

As with the Sobel operator principle, only its convolution template is given here.

4) The method used by the canny algorithm

The convolution operator used in the canny algorithm implemented in this paper is relatively simple and is expressed as follows:


The first derivative matrix of the X-direction and y-direction, the mathematical expression of the gradient amplitude and the gradient orientation are:


After finding these matrices, the next step of the detection process can be carried out.

2.4 Non-maximal value suppression for gradient amplitudeThe larger the value of the element in the image gradient amplitude matrix, the greater the gradient value of the point in the image, but this does not mean that the point is the edge (which is only a process of image enhancement). In the canny algorithm, the non-maximal value suppression is an important step for edge detection. In the popular sense, it means to find the local maximum value of the pixel, and set the gray value corresponding to the non-maximum point to 0 ., which would remove a large portion of the non-marginal point (which I understand).


Fig. 1 Principle of non-maximal value suppression

As shown in Figure 1, to suppress a non-maximal value, it is first to determine whether the gray value of Pixel C is the largest in its 8-value neighborhood. The direction of the Blue line in Figure 1 is the gradient direction of the C point, so that it can be determined that its local maximum value is definitely distributed on this line, that is, outside the C point, the intersection of the gradient direction dTmp1 and dTmp2 the value of the two points may also be the local maximum value. Therefore, judging the gray scale of C point and the gray size of these two points can determine whether the C point is the local maximum gray point in its neighborhood. If it is judged that the C point grayscale value is less than either of these two points, it means that the C point is not a local maximum value, then you can exclude the C point as the edge. This is how non-maximum-value suppression works.

The author believes that in the process of understanding the following two points need to be noted:

1) The biggest suppression in Central Africa is to answer the question: "is the current gradient value a local maximum in the gradient direction?" "Therefore, the gradient value of the current position is compared with the gradient value on both sides of the gradient direction;

2) The gradient direction is perpendicular to the edge direction.

But in fact, we can only get the value of the 8 points of the C-Point neighborhood, and DTMP1 and DTMP2 are not in it, to get these two values will need to the two points at both ends of the known Gray Line interpolation, that is, according to the G1 and G2 in Figure 1 to interpolate DTmp1, according to G3 and G4 to interpolate DTMP2 , which is to use its gradient direction, which is the reason why the gradient direction matrix Thita is required in the canny algorithm above.

When the non-maximal value is suppressed, a binary image is obtained, and the non-edge point grayscale value is 0, and the local grayscale maximum point of the edge may be set to a grayscale of 128. As can be seen from the specific test image below, such a test result contains many false edges caused by noise and other causes. Further processing is therefore required. 2.5 detecting and connecting edges with a dual-threshold algorithm

The method of reducing the number of false edges in the canny algorithm is to adopt the double threshold method. Select two thresholds (the selection method for thresholds is discussed in the extension), according to the high threshold is worth an edge image, such an image contains very few false edges, but because the threshold is high, the resulting image edge may not be closed, the problem is not resolved to use another low threshold value.

In a high-threshold image, the edge is linked to a contour, and when the endpoint of the contour is reached, the algorithm looks for a point in the 8 neighborhood point of the breakpoint that satisfies the low threshold, and then collects the new edge from that point until the entire image edge is closed.

The above is for the entire canny edge detection algorithm principle analysis, next we carry out VC algorithm implementation and effect analysis.

The principle of canny edge detection algorithm and its VC implementation (I.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.