opencv/samples/cpp/tutorial_code/ImgProc/Smoothing.cpp

108 lines
2.4 KiB
C++
Raw Normal View History

2011-06-18 04:13:53 +08:00
/**
* file Smoothing.cpp
2011-06-18 04:13:53 +08:00
* brief Sample code for simple filters
* author OpenCV team
*/
2016-02-15 21:37:29 +08:00
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
2011-06-18 04:13:53 +08:00
using namespace std;
using namespace cv;
/// Global Variables
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;
Mat src; Mat dst;
char window_name[] = "Smoothing Demo";
2011-06-18 04:13:53 +08:00
/// Function headers
int display_caption( const char* caption );
2011-06-18 04:13:53 +08:00
int display_dst( int delay );
2012-10-17 07:18:30 +08:00
/**
* function main
2011-06-18 04:13:53 +08:00
*/
int main( void )
2011-06-18 04:13:53 +08:00
{
namedWindow( window_name, WINDOW_AUTOSIZE );
2011-06-18 04:13:53 +08:00
/// Load the source image
2016-02-15 21:37:29 +08:00
src = imread( "../data/lena.jpg", IMREAD_COLOR );
2011-06-18 04:13:53 +08:00
if( display_caption( "Original Image" ) != 0 ) { return 0; }
dst = src.clone();
if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }
2012-10-17 07:18:30 +08:00
/// Applying Homogeneous blur
2011-06-18 04:13:53 +08:00
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
2012-10-17 07:18:30 +08:00
/// Applying Gaussian blur
2011-06-18 04:13:53 +08:00
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
/// Applying Median blur
if( display_caption( "Median Blur" ) != 0 ) { return 0; }
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ medianBlur ( src, dst, i );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
/// Applying Bilateral Filter
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
{ bilateralFilter ( src, dst, i, i*2, i/2 );
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
/// Wait until user press a key
display_caption( "End: Press a key!" );
waitKey(0);
return 0;
}
/**
* @function display_caption
*/
int display_caption( const char* caption )
2011-06-18 04:13:53 +08:00
{
dst = Mat::zeros( src.size(), src.type() );
2012-10-17 07:18:30 +08:00
putText( dst, caption,
Point( src.cols/4, src.rows/2),
FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) );
2011-06-18 04:13:53 +08:00
imshow( window_name, dst );
int c = waitKey( DELAY_CAPTION );
if( c >= 0 ) { return -1; }
return 0;
}
/**
* @function display_dst
*/
int display_dst( int delay )
{
imshow( window_name, dst );
int c = waitKey ( delay );
if( c >= 0 ) { return -1; }
2012-10-17 07:18:30 +08:00
return 0;
2011-06-18 04:13:53 +08:00
}