1 module app; 2 import std.stdio; 3 import std.format; 4 import std.random; 5 6 import opencvd; 7 8 // https://docs.opencv.org/4.1.0/df/d5e/samples_2cpp_2tutorial_code_2ImgProc_2Morphology_1_8cpp-example.html 9 // tested and working! 10 11 Mat src, erosion_dst, dilation_dst; 12 int erosion_elem = 0; 13 int erosion_size = 0; 14 int dilation_elem = 0; 15 int dilation_size = 0; 16 int max_elem = 2; 17 int max_kernel_size = 21; 18 19 int main() 20 { 21 erosion_dst = newMat(); 22 dilation_dst = newMat(); 23 24 src = imread( "LinuxLogo.jpg", IMREAD_COLOR ); 25 if( src.isEmpty() ) 26 { 27 "Could not open or find the image!".writeln; 28 return -1; 29 } 30 namedWindow( "Erosion Demo", WINDOW_AUTOSIZE ); 31 namedWindow( "Dilation Demo", WINDOW_AUTOSIZE ); 32 moveWindow( "Dilation Demo", src.cols, 0 ); 33 createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo", 34 &erosion_elem, max_elem, 35 cast(TrackbarCallback)(&Erosion)); 36 createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo", 37 &erosion_size, max_kernel_size, 38 cast(TrackbarCallback)(&Erosion)); 39 createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo", 40 &dilation_elem, max_elem, 41 cast(TrackbarCallback)(&Dilation)); 42 createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo", 43 &dilation_size, max_kernel_size, 44 cast(TrackbarCallback)(&Dilation)); 45 Erosion( 0, null ); 46 Dilation( 0, null ); 47 waitKey(0); 48 return 0; 49 } 50 void Erosion( int, void* ) 51 { 52 int erosion_type = 0; 53 if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; } 54 else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; } 55 else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; } 56 Mat element = getStructuringElement( erosion_type, 57 Size( 2*erosion_size + 1, 2*erosion_size+1 ), 58 Point( erosion_size, erosion_size ) ); 59 erode( src, erosion_dst, element ); 60 imshow( "Erosion Demo", erosion_dst ); 61 } 62 void Dilation( int, void* ) 63 { 64 int dilation_type = 0; 65 if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; } 66 else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; } 67 else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; } 68 Mat element = getStructuringElement( dilation_type, 69 Size( 2*dilation_size + 1, 2*dilation_size+1 ), 70 Point( dilation_size, dilation_size ) ); 71 dilate( src, dilation_dst, element ); 72 imshow( "Dilation Demo", dilation_dst ); 73 }