[OpenCV Getting Started Guide] Part 3 "Canny edge detection"

Source: Internet
Author: User
Document directory
  • 1.1 cvkan
  • 1.2 cvCreateTrackbar
  • 1.3 CvTrackbarCallback
[OpenCV Getting Started Guide] Part 3 "Canny edge detection"

The principle of image edge detection is to detect all the gray-scale points in the image, and these points are connected to form several lines, which can be called the edge of the image.

A multi-level edge detection algorithm developed by John F. Kan in 1986. Here, the mathematical principles and algorithms of the edge detection function will no longer be implemented. Interested readers can refer to professional books. This article mainly introduces how to perform Edge Detection on images in OpenCV, let's take a look at the prototype of this function.

OpenCV Getting Started Guide series Article address: http://blog.csdn.net/morewindows/article/category/1291764

 

I. Main Function 1.1 cvCanny

Function: Performs Edge Detection on the Image Using the Canny method.

Function prototype:

Void cvkan (

Const CvArr * image,

CvArr * edges,

Double threshold1, double threshold2,

Int aperture_size = 3

);

Function Description:

The first parameter indicates the input image, which must be a single channel grayscale image.

The second parameter indicates the output edge image, which is a single-channel black-and-white image.

The third parameter and the fourth parameter indicate the threshold. The small threshold in these two thresholds is used to control edge connections, A large threshold is used to control the initial division of a strong edge. That is, if the gradient of a pixel is greater than the upper limit, it is regarded as an edge pixel. If the gradient is smaller than the lower threshold, it is discarded. If the gradient of this point is between the two, we will keep it when this point is connected to a pixel above the upper limit. Otherwise, it will be deleted.

The fifth parameter indicates the Sobel operator size. The default value is 3, which indicates a 3*3 matrix. Sobel operators and Gaussian Laplace operators are common edge operators. For detailed mathematical principles, refer to professional books.

 

To better use the cvkan () function, we will introduce two more practical functions. These two functions are very helpful for subsequent program implementation.

 

1.2 cvCreateTrackbar

Function: Creates a trackbar and adds it to a specified window.

Function prototype:

IntcvCreateTrackbar (

Const char * trackbar_name,

Const char * window_name,

Int * value,

Intcount,

CvTrackbarCallback on_change

);

Function Description:

The first parameter indicates the trackbar name.

The second parameter indicates the window name. The trackbar is displayed in this window.

The third parameter indicates the position of the slider during creation.

The fourth parameter indicates the maximum value of the slider position, and the minimum value is fixed to 0.

The fifth parameter indicates the callback function. When the slider position changes, the system calls the callback function.

Note: The created trackbar is displayed at the top of the specified window by default. You can use the cvGetTrackbarPos () function to obtain the position information displayed by the trackbar and the cvSetTrackbarPos () function () to reset the position of the trackbar.

 

1.3 CvTrackbarCallback

Function: the callback function used by the cvCreateTrackbar () function.

Function Definition:

Typedef void (CV_CDECL * CvTrackbarCallback) (int pos)

Function Description:

When the position of the trackbar is changed, the system calls this callback function and sets the pos parameter to a value indicating the position of the trackbar.

 

Ii. Sample Code

The following is the code of the program used to detect the edge in OpenCV:

// Image of the Canny edge detection // MoreWindows (http://blog.csdn.net/MoreWindows) # include <opencv2/opencv. hpp> using namespace std; # pragma comment (linker, "/subsystem: \" windows \ "/entry: \" mainCRTStartup \ "") IplImage * g_pSrcImage, * g_pCannyImg; const char * pstrWindowsCannyTitle = "Edge Detection graph (http://blog.csdn.net/MoreWindows)"; // callback function void on_trackbar (int threshold) for CVST (g_pSrcImage, g_pCannyImg, threshold, threshold * 3, 3); cvShowImage (pstrWindowsCannyTitle, g_pCannyImg);} int main () {const char * pstrImageName = "001.jpg"; const char * pstrWindowsSrcTitle =" Source (http://blog.csdn.net/MoreWindows )"; const char * counter = "Threshold"; // load the grayscale image of the image from the file; gradient-grayscale g_pSrcImage = cvLoadImage (pstrImageName, weight); g_pCannyImg = cvCreateImage (cvGetSize (g_pSrcImage ), rows, 1); // create a window cvNamedWindow (rows, CV_WINDOW_AUTOSIZE); cvNamedWindow (pstrWindowsCannyTitle, CV_WINDOW_AUTOSIZE); // create a sliding bar int rows = 1; cvCreateTrackbar (rows, pstrWindowsCannyTitle, & nThresholdEdge, 100, on_trackbar); // display the image cvShowImage (pstrWindowsSrcTitle, g_pSrcImage) in the specified window; on_trackbar (1); // wait for the key event cvWaitKey (); cvDestroyWindow (pstrWindowsSrcTitle); cvDestroyWindow (pstrWindowsCannyTitle); cvReleaseImage (& g_pSrcImage); cvReleaseImage (& g_pCannyImg); return 0 ;}

Running effect:

 

This article introduces the edge detection method, which can effectively find all the edges in the image. We will also introduce the contour detection of images in OpenCV. You are welcome to continue browsing. Before performing contour detection on an image, you must first perform binarization on the image. Therefore, see [OpenCV Getting Started Guide] Article 4 binarization of the image.

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8239625

OpenCV Getting Started Guide series Article address: http://blog.csdn.net/morewindows/article/category/1291764

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.