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