1 #include "opencv2/imgproc/imgproc.hpp" 2 #include "opencv2/highgui/highgui.hpp" 3 4 using namespace std; 5 using namespace cv; 6 7 /// 全局变量 8 int DELAY_CAPTION = 1500; 9 int DELAY_BLUR = 100; 10 int MAX_KERNEL_LENGTH = 31; 11 12 Mat src; Mat dst; 13 char window_name[] = "Filter Demo 1"; 14 15 /// 函数申明 16 int display_caption( char* caption ); 17 int display_dst( int delay ); 18 19 /** 20 * main 函数 21 */ 22 int main( int argc, char** argv ) 23 { 24 namedWindow( window_name, CV_WINDOW_AUTOSIZE ); 25 26 /// 载入原图像 27 src = imread( "../images/lena.jpg", 1 ); 28 29 if( display_caption( "Original Image" ) != 0 ) { return 0; } 30 31 dst = src.clone(); 32 if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; } 33 34 /// 使用 均值平滑 35 if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; } 36 37 for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) 38 { blur( src, dst, Size( i, i ), Point(-1,-1) ); 39 if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } 40 41 /// 使用高斯平滑 42 if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; } 43 44 for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) 45 { GaussianBlur( src, dst, Size( i, i ), 0, 0 ); 46 if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } 47 48 /// 使用中值平滑 49 if( display_caption( "Median Blur" ) != 0 ) { return 0; } 50 51 for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) 52 { medianBlur ( src, dst, i ); 53 if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } 54 55 /// 使用双边平滑 56 if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; } 57 58 for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) 59 { bilateralFilter ( src, dst, i, i*2, i/2 ); 60 if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } 61 62 /// 等待用户输入 63 display_caption( "End: Press a key!" ); 64 65 waitKey(0); 66 return 0; 67 } 68 69 int display_caption( char* caption ) 70 { 71 dst = Mat::zeros( src.size(), src.type() ); 72 putText( dst, caption, 73 Point( src.cols/4, src.rows/2), 74 CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) ); 75 76 imshow( window_name, dst ); 77 int c = waitKey( DELAY_CAPTION ); 78 if( c >= 0 ) { return -1; } 79 return 0; 80 } 81 82 int display_dst( int delay ) 83 { 84 imshow( window_name, dst ); 85 int c = waitKey ( delay ); 86 if( c >= 0 ) { return -1; } 87 return 0; 88 }