1 #include "opencv2/highgui/highgui.hpp"
2 #include "opencv2/imgproc/imgproc.hpp"
7 IplConvKernel* element = 0;
8 int element_shape = CV_SHAPE_RECT;
10 //the address of variable which receives trackbar position update
12 int open_close_pos = 0;
13 int erode_dilate_pos = 0;
15 // callback function for open/close trackbar
16 void OpenClose(int pos)
18 int n = open_close_pos - max_iters;
19 int an = n > 0 ? n : -n;
20 element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
23 cvErode(src,dst,element,1);
24 cvDilate(dst,dst,element,1);
28 cvDilate(src,dst,element,1);
29 cvErode(dst,dst,element,1);
31 cvReleaseStructuringElement(&element);
32 cvShowImage("Open/Close",dst);
35 // callback function for erode/dilate trackbar
36 void ErodeDilate(int pos)
38 int n = erode_dilate_pos - max_iters;
39 int an = n > 0 ? n : -n;
40 element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
43 cvErode(src,dst,element,1);
47 cvDilate(src,dst,element,1);
49 cvReleaseStructuringElement(&element);
50 cvShowImage("Erode/Dilate",dst);
54 int main( int argc, char** argv )
56 char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
57 if( (src = cvLoadImage(filename,1)) == 0 )
60 printf( "Hot keys: \n"
61 "\tESC - quit the program\n"
62 "\tr - use rectangle structuring element\n"
63 "\te - use elliptic structuring element\n"
64 "\tc - use cross-shaped structuring element\n"
65 "\tSPACE - loop through all the options\n" );
67 dst = cvCloneImage(src);
69 //create windows for output images
70 cvNamedWindow("Open/Close",1);
71 cvNamedWindow("Erode/Dilate",1);
73 open_close_pos = erode_dilate_pos = max_iters;
74 cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
75 cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
81 OpenClose(open_close_pos);
82 ErodeDilate(erode_dilate_pos);
88 element_shape = CV_SHAPE_ELLIPSE;
89 else if( (char)c == 'r' )
90 element_shape = CV_SHAPE_RECT;
91 else if( (char)c == 'c' )
92 element_shape = CV_SHAPE_CROSS;
93 else if( (char)c == ' ' )
94 element_shape = (element_shape + 1) % 3;
102 cvDestroyWindow("Open/Close");
103 cvDestroyWindow("Erode/Dilate");