OPENCV例程2 :CANNY運算元邊緣檢測

pamxy發表於2013-08-07

轉自:http://yu2xiangyang.blog.163.com/blog/static/37315638201091242739364/

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

char wndname[] = "Edge";
char tbarname[] = "Threshold";
int edge_thresh = 1;

IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;

// define a trackbar callback
void on_trackbar(int h)
{
    //cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
    //cvNot( gray, edge );

    // Run the edge detector on grayscale
    cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 3);

    cvZero( cedge );
    //copy edge points
    cvCopy( image, cedge, edge);   //使邊緣為彩色圖象,如果直接複製edge到cedge,顯示的是黑白邊緣

    cvShowImage(wndname, cedge);
}

int main( int argc, char** argv )
{
   

 image = cvLoadImage( "E:\lena.jpg", 1);


    // Create the output image
    cedge = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 3);

    // Convert to grayscale
    gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
    edge = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
    cvCvtColor(image, gray, CV_BGR2GRAY);

    // Create a window
    cvNamedWindow(wndname, 1);

    // create a toolbar
    cvCreateTrackbar( tbarname , wndname, &edge_thresh, 100, on_trackbar);

    // Show the image
    on_trackbar(0);

    // Wait for a key stroke; the same function arranges events processing
    cvWaitKey(0);
    cvReleaseImage(&image);
    cvReleaseImage(&gray);
    cvReleaseImage(&edge);
    cvDestroyWindow(wndname);

    return 0;
}

OPENCV例程2 :CANNY運算元邊緣檢測 - 貝殼 - yu2xiangyang的部落格

相關文章