1 /** 2 * @file Morphology_2.cpp 3 * @brief Advanced morphology Transformations sample code 4 * @author OpenCV team 5 */ 6 7 #include "opencv2/imgproc/imgproc.hpp" 8 #include "opencv2/imgcodecs.hpp" 9 #include "opencv2/highgui/highgui.hpp" 10 #include <stdlib.h> 11 #include <stdio.h> 12 13 using namespace cv; 14 15 /// Global variables 16 Mat src, dst; 17 18 int morph_elem = 0; 19 int morph_size = 0; 20 int morph_operator = 0; 21 int const max_operator = 4; 22 int const max_elem = 2; 23 int const max_kernel_size = 21; 24 25 const char* window_name = "Morphology Transformations Demo"; 26 27 28 /** Function Headers */ 29 void Morphology_Operations( int, void* ); 30 31 /** 32 * @function main 33 */ 34 int main( int, char** argv ) 35 { 36 /// Load an image 37 src = imread( argv[1] ); 38 39 if( src.empty() ) 40 { return -1; } 41 42 /// Create window 43 namedWindow( window_name, WINDOW_AUTOSIZE ); 44 45 /// Create Trackbar to select Morphology operation 46 createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", window_name, &morph_operator, max_operator, Morphology_Operations ); 47 48 /// Create Trackbar to select kernel type 49 createTrackbar( "Element:\n 0: Rect - 1: Cross - 2: Ellipse", window_name, 50 &morph_elem, max_elem, 51 Morphology_Operations ); 52 53 /// Create Trackbar to choose kernel size 54 createTrackbar( "Kernel size:\n 2n +1", window_name, 55 &morph_size, max_kernel_size, 56 Morphology_Operations ); 57 58 /// Default start 59 Morphology_Operations( 0, 0 ); 60 61 waitKey(0); 62 return 0; 63 } 64 65 /** 66 * @function Morphology_Operations 67 */ 68 void Morphology_Operations( int, void* ) 69 { 70 71 // Since MORPH_X : 2,3,4,5 and 6 72 int operation = morph_operator + 2; 73 74 Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) ); 75 76 /// Apply the specified morphology operation 77 morphologyEx( src, dst, operation, element ); 78 imshow( window_name, dst ); 79 } 80