1 /** 2 * @file filter2D_demo.cpp 3 * @brief Sample code that shows how to implement your own linear filters by using filter2D function 4 * @author OpenCV team 5 */ 6 7 #include "opencv2/imgproc/imgproc.hpp" 8 #include "opencv2/imgcodecs.hpp" 9 #include "opencv2/highgui/highgui.hpp" 10 #include <stdlib.h> 11 #include <stdio.h> 12 13 using namespace cv; 14 15 /** 16 * @function main 17 */ 18 int main ( int, char** argv ) 19 { 20 /// Declare variables 21 Mat src, dst; 22 23 Mat kernel; 24 Point anchor; 25 double delta; 26 int ddepth; 27 int kernel_size; 28 const char* window_name = "filter2D Demo"; 29 30 int c; 31 32 /// Load an image 33 src = imread( argv[1] ); 34 35 if( src.empty() ) 36 { return -1; } 37 38 /// Create window 39 namedWindow( window_name, WINDOW_AUTOSIZE ); 40 41 /// Initialize arguments for the filter 42 anchor = Point( -1, -1 ); 43 delta = 0; 44 ddepth = -1; 45 46 /// Loop - Will filter the image with different kernel sizes each 0.5 seconds 47 int ind = 0; 48 for(;;) 49 { 50 c = waitKey(500); 51 /// Press 'ESC' to exit the program 52 if( (char)c == 27 ) 53 { break; } 54 55 /// Update kernel size for a normalized box filter 56 kernel_size = 3 + 2*( ind%5 ); 57 kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size); 58 59 /// Apply filter 60 filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT ); 61 imshow( window_name, dst ); 62 ind++; 63 } 64 65 return 0; 66 } 67