Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / samples / c / edge.c
1 #ifdef _CH_
2 #pragma package <opencv>
3 #endif
4
5 #ifndef _EiC
6 #include "cv.h"
7 #include "highgui.h"
8 #endif
9
10 char wndname[] = "Edge";
11 char tbarname[] = "Threshold";
12 int edge_thresh = 1;
13
14 IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;
15
16 // define a trackbar callback
17 void on_trackbar(int h)
18 {
19     cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
20     cvNot( gray, edge );
21
22     // Run the edge detector on grayscale
23     cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 3);
24   
25     cvZero( cedge );
26     // copy edge points
27     cvCopy( image, cedge, edge );
28
29     cvShowImage(wndname, cedge);
30 }
31
32 int main( int argc, char** argv )
33 {
34     char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
35     
36     if( (image = cvLoadImage( filename, 1)) == 0 )
37         return -1;
38
39     // Create the output image
40     cedge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 3);
41
42     // Convert to grayscale
43     gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
44     edge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
45     cvCvtColor(image, gray, CV_BGR2GRAY);
46
47     // Create a window
48     cvNamedWindow(wndname, 1);
49
50     // create a toolbar 
51     cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);
52
53     // Show the image
54     on_trackbar(0);
55
56     // Wait for a key stroke; the same function arranges events processing
57     cvWaitKey(0);
58     cvReleaseImage(&image);
59     cvReleaseImage(&gray);
60     cvReleaseImage(&edge);
61     cvDestroyWindow(wndname);
62
63     return 0;
64 }
65
66 #ifdef _EiC
67 main(1,"edge.c");
68 #endif