Cvcanny function of Image edge detection--OPENCV

Source: Internet
Author: User
Tags cmath

Cvcanny function of Image edge detection--OPENCV

Category: C/

void Cvcanny (const cvarr* image, cvarr* edges, double threshold1, double threshold2, int aperture_size=3); Image single-channel input images. Edges the output image of a single-channel storage edge threshold1 The first threshold threshold2 the second threshold Aperture_sizesobel operator kernel size (see Cvsobel).

The function Cvcanny uses the CANNY algorithm to discover the edges of the input image and identify those edges in the output image. Small thresholds in threshold1 and threshold2 are used to control edge connections, and large thresholds are used to control the initial segmentation of strong edges.

    • Note: Cvcanny only accepts single-channel images as input.
    • External links: A OPENCV implementation of the classical canny self-tuning threshold algorithm See the segmentation threshold of adaptive determination canny algorithm in OPENCV
    • Reference OpenCV Chinese official website: Http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86#Canny

Note: The Cvcanny function in OpenCV uses the Cvsobel differential calculation. Effect of Cvcanny function for OpenCV

Click (here) to collapse or open

#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include #include <cmath>
using namespace Std;
using namespace CV;
int main (int argc, char * * argv)
{
Iplimage * PIMG=NULL;
Iplimage * PCANNYIMG=NULL;
if (argc ==2&& (Pimg=cvloadimage (argv[1],0))!=0)
{
Pcannyimg=cvcreateimage (Cvgetsize (PIMG), ipl_depth_8u,1);
Cvcanny (pimg,pcannyimg,50,150,3);
Create window
Cvnamedwindow ("src", 1);
Cvnamedwindow ("Canny", 1);
Display image
Cvshowimage ("src", pImg);
Cvshowimage ("Canny", pcannyimg);
Cvwaitkey (0); Wait for the button
Destroying Windows
Cvdestroywindow ("src");
Cvdestroywindow ("canny");
Release image
Cvreleaseimage (&AMP;PIMG);
Cvreleaseimage (&AMP;PCANNYIMG);
return 0;
}
return-1;
}

Important: Canny principles: Links and Content: http://blog.csdn.net/likezhaobin/article/details/6892176

OPENCV2 version:

Canny edge detection. CPP: Defines the entry point of the console application.
//

#include "stdafx.h"


#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include #include <cmath>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

#pragma comment (lib, "Opencv_core2410d.lib")
#pragma comment (lib, "Opencv_highgui2410d.lib")
#pragma comment (lib, "Opencv_imgproc2410d.lib")

using namespace Std;
using namespace CV;

int main (int argc, char * * argv)
{
Iplimage * PIMG=NULL;
Iplimage * PCANNYIMG=NULL;


Cv::mat src = cv::imread ("swan.jpg");
if (Src.empty ())
return-1;

Cv::mat bw;
Cv::cvtcolor (SRC, BW, cv_bgr2gray);

Mat Canny_mat (Src.size (), cv_8u);

Cvcanny (pimg,pcannyimg,50,150,3);
Cv::canny (bw,canny_mat,50,150,3);

Imshow ("Canny", Canny_mat);

Cvwaitkey (0); Wait for the button


return 0;

}

After Edge detection:

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 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) 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.

Cvcanny function of Image edge detection--OPENCV

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.