1 /**
2  * file Smoothing.cpp
3  * brief Sample code for simple filters
4  * author OpenCV team
5  */
6 #include <iostream>
7 #include <vector>
8 
9 #include "opencv2/imgproc/imgproc.hpp"
10 #include "opencv2/imgcodecs.hpp"
11 #include "opencv2/highgui/highgui.hpp"
12 #include "opencv2/features2d/features2d.hpp"
13 
14 using namespace std;
15 using namespace cv;
16 
17 /// Global Variables
18 int DELAY_CAPTION = 1500;
19 int DELAY_BLUR = 100;
20 int MAX_KERNEL_LENGTH = 31;
21 
22 Mat src; Mat dst;
23 char window_name[] = "Smoothing Demo";
24 
25 /// Function headers
26 int display_caption( const char* caption );
27 int display_dst( int delay );
28 
29 
30 /**
31  * function main
32  */
main(void)33 int main( void )
34 {
35   namedWindow( window_name, WINDOW_AUTOSIZE );
36 
37   /// Load the source image
38   src = imread( "../data/lena.jpg", 1 );
39 
40   if( display_caption( "Original Image" ) != 0 ) { return 0; }
41 
42   dst = src.clone();
43   if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }
44 
45 
46   /// Applying Homogeneous blur
47   if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
48 
49   for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
50       { blur( src, dst, Size( i, i ), Point(-1,-1) );
51         if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
52 
53 
54   /// Applying Gaussian blur
55   if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
56 
57   for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
58       { GaussianBlur( src, dst, Size( i, i ), 0, 0 );
59         if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
60 
61 
62   /// Applying Median blur
63   if( display_caption( "Median Blur" ) != 0 ) { return 0; }
64 
65   for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
66       { medianBlur ( src, dst, i );
67         if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
68 
69 
70   /// Applying Bilateral Filter
71   if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
72 
73   for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
74       { bilateralFilter ( src, dst, i, i*2, i/2 );
75         if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
76 
77   /// Wait until user press a key
78   display_caption( "End: Press a key!" );
79 
80   waitKey(0);
81 
82   return 0;
83 }
84 
85 /**
86  * @function display_caption
87  */
display_caption(const char * caption)88 int display_caption( const char* caption )
89 {
90   dst = Mat::zeros( src.size(), src.type() );
91   putText( dst, caption,
92            Point( src.cols/4, src.rows/2),
93            FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );
94 
95   imshow( window_name, dst );
96   int c = waitKey( DELAY_CAPTION );
97   if( c >= 0 ) { return -1; }
98   return 0;
99 }
100 
101 /**
102  * @function display_dst
103  */
display_dst(int delay)104 int display_dst( int delay )
105 {
106   imshow( window_name, dst );
107   int c = waitKey ( delay );
108   if( c >= 0 ) { return -1; }
109   return 0;
110 }
111