"OpenCV Getting Started" chapter III Canny edge detection

Source: Internet
Author: User
Tags function definition
"OpenCV Getting Started" chapter III Canny edge detection

The image edge detection principle is to detect the image of all the gray value changes in the larger point, and these points are connected to form a number of lines, these lines can be called the edge of the image.

The canny edge detection operator is a multi-level edge detection algorithm developed by John F. Canny in 1986. The mathematical principle and algorithm realization of Canny edge detection here is no longer, interested readers can consult the professional books, this article mainly describes how to opencv images in the Canny edge detection, the following to see 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 function: Using canny method to detect image edge

Function Prototypes:

void Cvcanny (

Const cvarr* Image,

cvarr* edges,

Double Threshold1,double Threshold2,

int aperture_size=3

);

Function Description:

The first parameter represents an input image and must be a single-channel grayscale.

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

The third and fourth parameters represent thresholds, and the small thresholds in these two thresholds are used to control edge connections, and large thresholds are used to control the initial segmentation of strong edges, which is considered to be an edge pixel if the gradient of a pixel is larger than the upper limit, and is discarded if it is less than the lower threshold value. If the gradient of the point is between the two, we will retain it if the point is connected to a pixel above the upper limit, otherwise it is deleted.

The fifth parameter represents the Sobel operator size, and the default is 3, which represents a 3*3 matrix. Sobel operators and Laplace operators are commonly used edge operators, and detailed mathematical principles can be consulted in professional books.

To better use the Cvcanny () function, the following two useful functions are described below, and these two functions are very helpful for the subsequent implementation of the program.

1.2 Cvcreatetrackbar

function function: Create TrackBar and add to specified window

Function Prototypes:

Intcvcreatetrackbar (

Const char* Trackbar_name,

Const char* Window_name,

int* value,

intcount,

Cvtrackbarcallback On_change

);

Function Description:

The first parameter represents the name of the trackbar.

The second parameter represents the window name, and the trackbar is displayed within this window.

The third parameter represents the position of the slider at the time of creation.

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

The fifth parameter represents a callback function. The callback function is called when there is a change in the position of the slider.

Note: The created TrackBar is displayed by default at the top of the specified window, and can be obtained through function cvgettrackbarpos () to get the location information TrackBar displayed, as well as through the function Cvsettrackbarpos () To reset the display location of the trackbar.

1.3 Cvtrackbarcallback

function function: The callback function used by the Cvcreatetrackbar () function

function definition:

typedef void (CV_CDECL *cvtrackbarcallback) (int pos)

Function Description:

When the trackbar position is changed, the system calls the callback function and sets the parameter pos to the value representing the trackbar position.

two. Sample program code

The following is a program code for using canny edge detection in OPENCV:[CPP]  View plain copy//image canny edge detection   //by 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 Diagram (http://blog.csdn.net/MoreWindows) ";  //cvcreatetrackbar callback function    Void on_trackbar ( Int threshold)    {       //canny edge detection         cvcanny (g_psrcimage, g_pcannyimg, threshold, threshold * 3, 3);        cvshowimage (pstrwindowscannytitle, g_pcannyimg);  }   Int main ()    {       const char *pstrimagename =   "001.jpG ";       const char *pstrWindowsSrcTitle = " original (HTTP// blog.csdn.net/morewindows) ";       const char *pstrWindowsToolBar  =  "Threshold";          //loading images from a file in grayscale cv_load_image_grayscale  -  grayscale        g_psrcimage = cvloadimage (pstrimagename, cv_load_ Image_grayscale);       g_pcannyimg = cvcreateimage (CvGetSize (g_ Psrcimage),  ipl_depth_8u, 1);          //Create window        cvnamedwindow (pstrwindowssrctitle, cv_window_autosize);        cvnamedwindow (pstrwindowscannytitle, cv_window_autosize);     //Create slide bar        int nThresholdEdge = 1;        Cvcreatetrackbar (pstrWindowstoolbar, pstrwindowscannytitle, &nthresholdedge, 100, on_trackbar);          //displaying images in the specified window

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.