1, edge detection of OPENCV image

Source: Internet
Author: User

This uses the canny function for edge detection

function definition:

void Const cvarr* image,cvarr* edges,double threshold1,doubleint aperture_size=3 );

Parameter description:

Image  // input single-channel images (which can be color images) can be modified with Cvcvtcolor () for multi-channel images.  /// output edge image, also single channel, but black /white/ / First threshold // Second threshold, in which a small threshold is used to control the edge connection, A large threshold is used to control the initial segmentation of strong edges.  // operator kernel size

1. Create a window to display the original image and the output image

1 Cvnamedwindow ("Example1", cv_window_autosize); 2 Cvnamedwindow ("Example2", cv_window_autosize);

2. Read the original image

1 iplimage* Img_rgb = Cvloadimage ("e:\\test.jpg");

3. Create a file structure for grayscale images

1 iplimage* img_gray = Cvcreateimage (Cvsize (img_rgb->width, img_rgb->height),2             img_rgb->Depth,3             1 4          );

4. Convert RGB original image to grayscale image

1 cvcvtcolor (Img_rgb, Img_gray, Cv_bgr2gray);

5, edge detection of grayscale image, and save to an image file

1  -  Max 3);

6. Release Image memory

1 cvreleaseimage (&Img_rgb); 2 cvreleaseimage (&img_gray); 3 cvreleaseimage (&IMG_CNY);

7. Close the window

1 Cvdestroywindow ("Example1"); 2 Cvdestroywindow ("Example2");

Full code:

1 /************************************************************************/2 /*Canny edge detection*/3 /************************************************************************/4#include"cv.h"5#include"highgui.h"6 7iplimage*Docanny (8iplimage*inch,9         DoubleLowthresh,Ten         DoubleHighthresh, One         DoubleAperture A     ) - { -     //canny can only handle grayscale graphs the     if(inch->nchannels! =1) -         return 0; -  -iplimage* out=Cvcreateimage ( +Cvgetsize (inch), -             inch-depth, +             1 A         ); at  -Cvcanny (inch, out, Lowthresh, Highthresh, aperture); -  -     return  out; -  - } in  - intMainintargcChar**argv) to { +Cvnamedwindow ("Example1", cv_window_autosize); -Cvnamedwindow ("Example2", cv_window_autosize); the  *iplimage* Img_rgb = Cvloadimage ("e:\\test.jpg"); $iplimage* Img_gray = Cvcreateimage (Cvsize (Img_rgb->width, img_rgb->height),Panax NotoginsengImg_rgb->depth, -             1 the          ); +  A Cvcvtcolor (Img_rgb, Img_gray, Cv_bgr2gray); the  +iplimage* IMG_CNY = Docanny (Img_gray, -, Max,3); -  $Cvshowimage ("Example1", Img_gray); $Cvshowimage ("Example2", IMG_CNY); -  -Cvwaitkey (0); the  -Cvreleaseimage (&Img_rgb);WuyiCvreleaseimage (&Img_gray); theCvreleaseimage (&img_cny); -  WuCvdestroywindow ("Example1"); -Cvdestroywindow ("Example2"); About  $      return 0; -}

1, edge detection of OPENCV image

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.