1 #include "opencv2/highgui/highgui_c.h"
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 static void OpenClose(int pos)
20 int n = open_close_pos - max_iters;
21 int an = n > 0 ? n : -n;
24 element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
27 cvErode(src,dst,element,1);
28 cvDilate(dst,dst,element,1);
32 cvDilate(src,dst,element,1);
33 cvErode(dst,dst,element,1);
35 cvReleaseStructuringElement(&element);
36 cvShowImage("Open/Close",dst);
39 // callback function for erode/dilate trackbar
40 static void ErodeDilate(int pos)
42 int n = erode_dilate_pos - max_iters;
43 int an = n > 0 ? n : -n;
46 element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 );
49 cvErode(src,dst,element,1);
53 cvDilate(src,dst,element,1);
55 cvReleaseStructuringElement(&element);
56 cvShowImage("Erode/Dilate",dst);
59 static void help(void)
61 printf( "This program demonstrated the use of the morphology operator, especially open, close, erode, dilate operations\n"
62 "Morphology operators are built on max (close) and min (open) operators as measured by pixels covered by small structuring elements.\n"
63 "These operators are very efficient.\n"
64 "This program also allows you to play with elliptical, rectangluar and cross structure elements\n"
66 "./morphologyc [image_name -- Default baboon.jpg]\n"
68 "\tESC - quit the program\n"
69 "\tr - use rectangle structuring element\n"
70 "\te - use elliptic structuring element\n"
71 "\tc - use cross-shaped structuring element\n"
72 "\tSPACE - loop through all the options\n" );
75 int main( int argc, char** argv )
81 filename = argc == 2 ? argv[1] : (char*)"baboon.jpg";
82 if( (src = cvLoadImage(filename,1)) == 0 )
84 printf("Cannot load file image %s\n", filename);
91 dst = cvCloneImage(src);
93 //create windows for output images
94 cvNamedWindow("Open/Close",1);
95 cvNamedWindow("Erode/Dilate",1);
97 open_close_pos = erode_dilate_pos = max_iters;
98 cvCreateTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
99 cvCreateTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
105 OpenClose(open_close_pos);
106 ErodeDilate(erode_dilate_pos);
112 element_shape = CV_SHAPE_ELLIPSE;
113 else if( (char)c == 'r' )
114 element_shape = CV_SHAPE_RECT;
115 else if( (char)c == 'c' )
116 element_shape = CV_SHAPE_CROSS;
117 else if( (char)c == ' ' )
118 element_shape = (element_shape + 1) % 3;
122 cvReleaseImage(&src);
123 cvReleaseImage(&dst);
126 cvDestroyWindow("Open/Close");
127 cvDestroyWindow("Erode/Dilate");