1 #include "opencv2/highgui/highgui.hpp"
2 #include "opencv2/imgproc/imgproc_c.h"
9 IplConvKernel* element = 0;
10 int element_shape = CV_SHAPE_RECT;
12 //the address of variable which receives trackbar position update
14 int open_close_pos = 0;
15 int erode_dilate_pos = 0;
17 // callback function for open/close trackbar
18 void OpenClose(int pos)
20 int n = open_close_pos - max_iters;
21 int an = n > 0 ? n : -n;
22 element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
25 cvErode(src,dst,element,1);
26 cvDilate(dst,dst,element,1);
30 cvDilate(src,dst,element,1);
31 cvErode(dst,dst,element,1);
33 cvReleaseStructuringElement(&element);
34 cvShowImage("Open/Close",dst);
37 // callback function for erode/dilate trackbar
38 void ErodeDilate(int pos)
40 int n = erode_dilate_pos - max_iters;
41 int an = n > 0 ? n : -n;
42 element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
45 cvErode(src,dst,element,1);
49 cvDilate(src,dst,element,1);
51 cvReleaseStructuringElement(&element);
52 cvShowImage("Erode/Dilate",dst);
56 int main( int argc, char** argv )
58 char* filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
59 if( (src = cvLoadImage(filename,1)) == 0 )
62 printf( "Hot keys: \n"
63 "\tESC - quit the program\n"
64 "\tr - use rectangle structuring element\n"
65 "\te - use elliptic structuring element\n"
66 "\tc - use cross-shaped structuring element\n"
67 "\tSPACE - loop through all the options\n" );
69 dst = cvCloneImage(src);
71 //create windows for output images
72 cvNamedWindow("Open/Close",1);
73 cvNamedWindow("Erode/Dilate",1);
75 open_close_pos = erode_dilate_pos = max_iters;
76 cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
77 cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
83 OpenClose(open_close_pos);
84 ErodeDilate(erode_dilate_pos);
90 element_shape = CV_SHAPE_ELLIPSE;
91 else if( (char)c == 'r' )
92 element_shape = CV_SHAPE_RECT;
93 else if( (char)c == 'c' )
94 element_shape = CV_SHAPE_CROSS;
95 else if( (char)c == ' ' )
96 element_shape = (element_shape + 1) % 3;
100 cvReleaseImage(&src);
101 cvReleaseImage(&dst);
104 cvDestroyWindow("Open/Close");
105 cvDestroyWindow("Erode/Dilate");