• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/imgcodecs.hpp"
3 #include "opencv2/highgui/highgui.hpp"
4 #include <stdlib.h>
5 #include <stdio.h>
6 
7 using namespace cv;
8 
help()9 static void help()
10 {
11 
12 printf("\nShow off image morphology: erosion, dialation, open and close\n"
13     "Call:\n   morphology2 [image]\n"
14     "This program also shows use of rect, ellipse and cross kernels\n\n");
15 printf( "Hot keys: \n"
16     "\tESC - quit the program\n"
17     "\tr - use rectangle structuring element\n"
18     "\te - use elliptic structuring element\n"
19     "\tc - use cross-shaped structuring element\n"
20     "\tSPACE - loop through all the options\n" );
21 }
22 
23 Mat src, dst;
24 
25 int element_shape = MORPH_RECT;
26 
27 //the address of variable which receives trackbar position update
28 int max_iters = 10;
29 int open_close_pos = 0;
30 int erode_dilate_pos = 0;
31 
32 // callback function for open/close trackbar
OpenClose(int,void *)33 static void OpenClose(int, void*)
34 {
35     int n = open_close_pos - max_iters;
36     int an = n > 0 ? n : -n;
37     Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
38     if( n < 0 )
39         morphologyEx(src, dst, MORPH_OPEN, element);
40     else
41         morphologyEx(src, dst, MORPH_CLOSE, element);
42     imshow("Open/Close",dst);
43 }
44 
45 // callback function for erode/dilate trackbar
ErodeDilate(int,void *)46 static void ErodeDilate(int, void*)
47 {
48     int n = erode_dilate_pos - max_iters;
49     int an = n > 0 ? n : -n;
50     Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an) );
51     if( n < 0 )
52         erode(src, dst, element);
53     else
54         dilate(src, dst, element);
55     imshow("Erode/Dilate",dst);
56 }
57 
58 
main(int argc,char ** argv)59 int main( int argc, char** argv )
60 {
61     char* filename = argc == 2 ? argv[1] : (char*)"../data/baboon.jpg";
62     if( (src = imread(filename,1)).empty() )
63         return -1;
64 
65     help();
66 
67     //create windows for output images
68     namedWindow("Open/Close",1);
69     namedWindow("Erode/Dilate",1);
70 
71     open_close_pos = erode_dilate_pos = max_iters;
72     createTrackbar("iterations", "Open/Close",&open_close_pos,max_iters*2+1,OpenClose);
73     createTrackbar("iterations", "Erode/Dilate",&erode_dilate_pos,max_iters*2+1,ErodeDilate);
74 
75     for(;;)
76     {
77         int c;
78 
79         OpenClose(open_close_pos, 0);
80         ErodeDilate(erode_dilate_pos, 0);
81         c = waitKey(0);
82 
83         if( (char)c == 27 )
84             break;
85         if( (char)c == 'e' )
86             element_shape = MORPH_ELLIPSE;
87         else if( (char)c == 'r' )
88             element_shape = MORPH_RECT;
89         else if( (char)c == 'c' )
90             element_shape = MORPH_CROSS;
91         else if( (char)c == ' ' )
92             element_shape = (element_shape + 1) % 3;
93     }
94 
95     return 0;
96 }
97