OPENCV Source Code Analysis (1): Filter Preface 1
Filtering and smoothing the image is a very important step in digital image processing and computer vision, so what is filtering? How does the filtering programming language come true? How it works. I intend to learn OpenCV on the source of filtering, and further enhance the practical ability of image processing.
First of all, we use 4 kinds of OPENCV common filtering algorithm function to realize the filtering function, let everybody have a perceptual understanding. The 4 filters are mean filter, Gaussian filter, median filter, and bilateral filter respectively. Using the opencv2.3.1 reference manual for the introduction of filtering in OpenCV, I use the vs2010+opencv2.3.1 here, a new Filter_test console project.
The source code and comments are as follows:
Header files that contain image processing and interface processing #include "stdafx.h" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp"
Using the CV namespace and standard namespace using namespace Std;
using namespace CV; Defines the global variable int delay_caption=1500; Delay at least 1.5s int delay_blur=100 when displaying the caption;
Display time delay 0.1s int max_kernel_length=31;//The maximum filter core length for each filter is SRC,DST;
Char window_name[]= "Filter Demo 1:";
function Description int display_caption (char *caption);
int display_dst (int delay); Main function int main (int argc, _tchar* argv[]) {Namedwindow (window_name,cv_window_autosize);//create a window//Will Le Na picture read-write to Matrix Src src=imread ("..
/images/lena.jpg ", 1);
Displays the Origin image word in the DST picture and displays the 1.5s if (display_caption ("Origin Image")!=0) {return 0;} in the window;
Dst=src.clone ();//Copy the Lena picture into the DST matrix and display it, 1.5s if (DISPLAY_DST (delay_caption)!=0) {return 0;} /* Use mean filter where the matrix filter function prototype is void blur (Inputarray src, outputarray DST, Size ksize, point Anchor=point ( -1,-1), int Bordertype=bor Der_default) where SRC is the pre-filter image matrix, DST is the filtered image matrix, Ksize is the filter rectangle core, anchor for the fixed punctuation, if the use of point ( -1,-1) is the default core points, Bordertype for the expansion of the edge mode of its filter formula is:
That is, using ksize pixels to average, it is easy to understand the */if (display_caption ("Homogeneous Blur")!=0) {return 0;} Display homogeneous blur word 1.5s for (int i=1;i<max_kernel_length;i=i+2) {blur (src,dst,size
(I,i), point ( -1,-1)),//using the mean filter function if (DISPLAY_DST (Delay_blur)!=0) {return 0;}
}//Gaussian filter, and similar to above 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;}
}//Median filter, and above similar 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;}
}//Bilateral filtering, and above similar 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;}
}//Press any key to stop display_caption ("end:press a key!");
Waitkey (0);
return 0;
}//middle display title, return 0 int display_caption (char *caption) {Dst=mat::zeros (Src.size (), Src.type () when no key is triggered; opencv2.3.1 in the picture to add Word with puttext function, you should know what the parameter is the meaning of Puttext (Dst,caption,point (SRC.COLS/4,SRC.ROWS/2), Cv_font_
hershey_complex,1, Scalar (255,255,255));
Imshow (WINDOW_NAME,DST);
Delay 1.5s, note that in C + + is Waitkey, the function parameter if it is non-positive, the direct stop in the bit//Set wait button trigger, otherwise delay at least parameter length ms int C=waitkey (delay_caption);
if (c>=0) {return-1;}
return 0;
}//Show filtered picture, return 0 int display_dst (int delay) {imshow (WINDOW_NAME,DST) when no key is triggered;
int C=waitkey (delay);
if (c>=0) {return-1;}
else return 0; }
For the specific 4 kinds of filter specific algorithms, you can view the source code, there is time I will go to specific learning, today this is actually nothing intrinsic, mainly to let everyone on the filter has a macro understanding, has been familiar with the development environment.
In the process of looking at the source code can not understand the function may be viewed on 2 sites, of which the first site is very full
Opencv2.3 version of the English Help file page: http://opencv.itseez.com/index.html
English page: Http://www.opencv.org.cn/index.php/Template:Doc
Original Image:
Filtered Image: