samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp#
Erosion and Dilation sample code 

Check the corresponding tutorial for more details
1/**
2 * @file Morphology_1.cpp
3 * @brief Erosion and Dilation sample code
4 * @author OpenCV team
5 */
6
7#include "opencv2/imgproc.hpp"
8#include "opencv2/highgui.hpp"
9#include <iostream>
10
11using namespace cv;
12using namespace std;
13
14/// Global variables
15Mat src, erosion_dst, dilation_dst;
16
17int erosion_elem = 0;
18int erosion_size = 0;
19int dilation_elem = 0;
20int dilation_size = 0;
21int const max_elem = 3;
22int const max_kernel_size = 21;
23
24/** Function Headers */
25void Erosion( int, void* );
26void Dilation( int, void* );
27
28//![main]
29/**
30 * @function main
31 */
32int main( int argc, char** argv )
33{
34 /// Load an image
35 CommandLineParser parser( argc, argv, "{@input | LinuxLogo.jpg | input image}" );
36 src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR );
37 if( src.empty() )
38 {
39 cout << "Could not open or find the image!\n" << endl;
40 cout << "Usage: " << argv[0] << " <Input image>" << endl;
41 return -1;
42 }
43
44 /// Create windows
45 namedWindow( "Erosion Demo", WINDOW_AUTOSIZE );
46 namedWindow( "Dilation Demo", WINDOW_AUTOSIZE );
47 moveWindow( "Dilation Demo", src.cols, 0 );
48
49 /// Create Erosion Trackbar
50 createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse \n 3: Diamond", "Erosion Demo",
51 &erosion_elem, max_elem,
52 Erosion );
53
54 createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
55 &erosion_size, max_kernel_size,
56 Erosion );
57
58 /// Create Dilation Trackbar
59 createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse \n 3: Diamond", "Dilation Demo",
60 &dilation_elem, max_elem,
61 Dilation );
62
63 createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
64 &dilation_size, max_kernel_size,
65 Dilation );
66
67 /// Default start
68 Erosion( 0, 0 );
69 Dilation( 0, 0 );
70
71 waitKey(0);
72 return 0;
73}
74//![main]
75
76//![erosion]
77/**
78 * @function Erosion
79 */
80void Erosion( int, void* )
81{
82 int erosion_type = 0;
83 if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
84 else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
85 else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
86 else if( erosion_elem == 3) { erosion_type = MORPH_DIAMOND; }
87
88 //![kernel]
89 Mat element = getStructuringElement( erosion_type,
90 Size( 2*erosion_size + 1, 2*erosion_size+1 ),
91 Point( erosion_size, erosion_size ) );
92 //![kernel]
93
94 /// Apply the erosion operation
95 erode( src, erosion_dst, element );
96 imshow( "Erosion Demo", erosion_dst );
97}
98//![erosion]
99
100//![dilation]
101/**
102 * @function Dilation
103 */
104void Dilation( int, void* )
105{
106 int dilation_type = 0;
107 if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
108 else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
109 else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
110 else if( dilation_elem == 3) { dilation_type = MORPH_DIAMOND; }
111
112 Mat element = getStructuringElement( dilation_type,
113 Size( 2*dilation_size + 1, 2*dilation_size+1 ),
114 Point( dilation_size, dilation_size ) );
115
116 /// Apply the dilation operation
117 dilate( src, dilation_dst, element );
118 imshow( "Dilation Demo", dilation_dst );
119}
120//![dilation]