transferred from: http://blog.csdn.net/likezhaobin/article/details/6892176
Author: Catalonia
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) The Prewitt operator and the Sobel operator are the same principle, only the convolution template is given here.
4) The canny algorithm used in the canny algorithm implemented in this paper is relatively simple convolution operator, the expression is 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 of gradient amplitude the larger the element value 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 (this is only the process of image enhancement). In the canny algorithm, the non-maximal value suppression is an important step for edge detection, in layman's sense, to find the local maximum value of pixels, the corresponding gray value of the non-maximal point is set to 0, so that a large number of non-marginal points can be eliminated (this is my understanding).
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) Central Africa's biggest inhibition is to answer the question: "The current gradient value in the gradient direction is a local maximum value." "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. 3, the canny algorithm implementation process
As the main purpose of this paper is to learn and implement the algorithm, but for the image reading, video acquisition and other content is not elaborated. Therefore, the use of OPENCV algorithm library as a way to implement other functions (about OpenCV, the author will describe the other). First, show the color images that will be processed in this article.
Figure 2 The image to be processed
3.1 Image reading and grayscale
The second method described above is used in programming to achieve the grayscale of the image. The grayscale image data stored in the PTR array. The specific grayscale effect is shown in Figure 3.
[CPP]View Plain copy print? iplimage* colorimage = cvloadimage ("12.jpg",-1); Read in the image, get the color map pointer iplimage* opencvgrayimage; Defines the transformed grayscale pointer unsignedChar* PTR; Data head address pointing to imageif(Colorimage = = NULL)return;inti = colorimage->width * colorimage->height;BYTEData1; Intermediate process variablesBYTEData2;BYTEData3; PTR =NewUnsignedChar[i]; for(intj=0; j<colorimage->height; j + +)//RGB weighted average, weight reference OpenCV { for(intx=0; x<colorimage->width; x + +) {data1 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3]; B-component data2 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3 + 1]; G Component Data3 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3 + 2]; R Component Ptr[j*colorimage->width+x]= (BYTE) (0.072169*DATA1&NBSP;+&NBSP;0.715160*DATA2&NBSP;+&NBSP;0.212671*DATA3); } } Opencvgrayimage=cvcreateimageheader (cvgetsize (colorimage), colorimage- >depth, 1); cvsetdata (grayimage,ptr, grayimage->widthstep); //Generating grayscale cvnamedwindow ("Grayimage", Cv_window_autosize) based on data ; cvshowimage ("Grayimage", Opencvgrayimage); //Display grayscale image Cvwaitkey (0); Cvdestroywindow ("GrayImage");
iplimage* colorimage = cvloadimage ("12.jpg",-1); Read in the image, get the color map pointer iplimage* opencvgrayimage; Defines the transformed grayscale pointer unsigned char* ptr;
The first address of the data pointing to the image if (colorimage = = NULL) return;
int i = colorimage->width * colorimage->height; BYTE data1;
Intermediate process variable BYTE data2;
BYTE data3;
ptr = new unsigned char[i]; for (intj=0; j<colorimage->height; j + +)//RGB weighted average, weight reference OpenCV {for (intx=0; x<colorimage-> Width X + +) {data1 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3]; B-Component data2 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3 + 1]; G Component Data3 = (BYTE) Colorimage->imagedata[j*colorimage->widthstep + i*3 + 2];
R component ptr[j*colorimage->width+x]= (BYTE) (0.072169*data1 + 0.715160*data2 + 0.212671*data3);
}} opencvgrayimage=cvcreateimageheader (Cvgetsize (colorimage), colorimage->depth, 1); Cvsetdata (Grayimage,PTR, grayimage->widthstep);
Generate Grayscale Graph Cvnamedwindow ("Grayimage", Cv_window_autosize) based on data; Cvshowimage ("Grayimage", opencvgrayimage);
Show grayscale Figure Cvwaitkey (0); Cvdestroywindow ("Grayimage");
Figure 3 The Grayscale image
Gaussian filtering of 3.2 images
Based on the edge detection process described above, the next step is to Gaussian filter the image. A one-dimensional or two-dimensional Gaussian filter core can be obtained based on the method described in the previous blog post. Therefore, the image Gaussian filter can be implemented in two ways, the following is described in detail.
Define the general variables for this section first:
[CPP] view plain copy print? double nsigma = 0.4; Defines the standard deviation of the Gaussian function int nwidowsize = 1+2*ceil (3*nsigma); Defines the size of the filter window int ncenter = (nwidowsize)/2; Define the index of the center of the filter window
Double nsigma = 0.4; Defines the standard deviation of the Gaussian function
int nwidowsize = 1+2*ceil (3*nsigma); Defines the size of the filter window
int ncenter = (nwidowsize)/2; Define the index of the center of the filter window
variables required for all two methods:
[CPP] view plain copy print? int nWidth = OpenCvGrayImage->width; //gets the pixel width of the image int nHeight = opencvgrayimage->height; //get the pixel height of the image unsigned Char * nimagedata = New unsigned Char [Nwidth*nheight]; <