Image Filtering
What is image filtering
Image filtering, which is to suppress the noise of the target image under the condition of preserving image detail characteristics, is an indispensable operation in image preprocessing, and its processing effect will directly affect the validity and reliability of subsequent image processing and analysis. (Excerpt from the network)
The purpose of image filtering
1, eliminate the noise mixed in the image
2. Extract image features for image recognition
Requirements for image filtering
1, can not damage image contour and Edge
2, image visual effect should be better
definition of the filter
The filter, as its name implies, is the device that filters the wave. (Excerpt from the network)
The above definition is for the physical device, but it is obviously applicable for image filtering.
Everybody used a magnifying glass, here's an example: you can see the magnified object in the process of moving the magnifying glass, and the filter is a lens that carries a weighted coefficient, where you can see the smoothed image through the lens and the movement of the lens through the lens and you can gradually get all the parts of the image.
Types of Filters
3 Linear Filters: Box filter, mean filter, Gaussian filter
2 Kinds of Nonlinear filter: Median filter, bilateral filter
Box Filter
The kernel used for box filtering:
When the normalize is true, the box filter is also the mean value filter. In other words, the mean filter is the special case after the square filter is normalized.
Normalization is the amount of processing to be scaled to a certain range, such as (0,1).
(function parsing is provided uniformly in the following article)
mean value filter
As mentioned above, the mean filter is the case that normalize is true, and at this point it is on average. The mean filter destroys the detail part of the image while denoising, and also makes the image more blurred.
Gaussian filter
Gaussian filtering is different, it can eliminate the noise well. In the Gaussian filtering process, each pixel is obtained by a weighted average of the other pixel values within and adjacent to the region.
From a mathematical point of view, the Gaussian blur process of an image is a convolution of an image and a normal distribution, which is called a Gaussian blur because the normal distribution is also called a Gaussian distribution.
Because the Fourier transform of the Gaussian function is another Gaussian function, Gaussian blur is a low-pass filter for the image.
n-dimensional normal distribution equation and two-dimensional spatial distribution are:
Median filter
The basic idea of median filtering is to replace the gray value of the pixel by the median value of the neighborhood gray value of the pixel point, which can keep the detail part of the image while removing the impulse noise and salt and pepper noise.
Median filtering takes longer than mean filter, but it is stronger in the ability to eliminate noise.
Bilateral filtering
The bilateral filtering is a kind of compromise processing combining the spatial proximity of the image with the similarity of the pixel value, taking into account the spatial information and the similarity of gray level to achieve the objective of preserving the edge and removing the noise.
function Prototypes
Box Filter
voidint ddepth, Size ksize, Point anchor = Point(-1,-1), boolnormalizetrueint borderType = BORDER_DEFAULT)
mean value filter
void blur(InputPointPoint(-1,-1), int borderType = BORDER_DEFAULT)
Gaussian filter
voiddoubledouble0, int borderType = BORDER_DEFAULT)
Median filter
voidint ksize)
Bilateral filtering
voidintdoubledouble sigmaSpace, int borderType = BORDER_DEFAULT)
Specific parameter resolution
InputArray src: 输入图像(源图像),Mat类型对象,图像深度应该是CV_8U、CV_16U、CV_16S、CV_32F、CV_64F之一。 而对于中值滤波而言,如果ksize为3或者5时,图像深度必须是CV_8U、CV_16U、CV_32F之一,如果孔径较大,则只能是CV_8U。
OutputArray dst: 输出图像(目标图像),和源图像的尺寸和类型均一样。
int ddepth: 输出图像的深度,-1表示原图像深度(即src.depth())。
Size ksize: 内核大小,用Size(w,h)来表示,w和h分别表示宽和高。
Point anchor: 锚点,也就是被平滑的点,如果该点坐标为负值表示取核的中心,因此默认的(-1,-1)就表示锚点在核中心。
boolnormalize: 标识符,为true时表示内核被其余区域归一化(normalized)了。
int borderType: 推断图像外部像素的某种边界模式。
double sigmaX: 表示高斯核函数在X方向的标准偏差。
double sigmaY: 表示高斯核函数在Y方向的标准偏差。 当sigmaY为0时,就将其设为sigmaX;如果两者均为0,则由ksize.with和ksize.height计算出来, 因此在高斯滤波函数中,ksize的w和h均必须是正数和奇数,或0,两者可以不同。
int ksize: 孔径的线性空间,必须是大于1的奇数。
int d: 过滤过程中每个像素邻域的直径,如果其值为非正数,则该值由sigmaSpace计算出来。
double sigmaColor: 颜色空间滤波器的sigma值,这个参数的值越大,就表明该像素邻域内有越宽广的颜色会被混合到一起,产生较大的半相等颜色区域。
double sigmaSpace: 坐标空间中滤波器的sigma值,坐标空间的标注方差。它的数值越大,意味着越远的像素会相互影响,从而使更大的区域中足够相似的颜色获取相同的颜色。 当d>0时,d制定了邻域大小与sigmaSpace无关。否则,d正比于sigmaSpace。
Example
Sample Code
#include <iostream>#include <opencv2\core\core.hpp>#include <opencv2\highgui\highgui.hpp>#include <opencv2\imgproc\imgproc.hpp>using namespace STD;using namespaceCv Mat G_srcimage;//Global source image//corresponding to the global box filter, mean filter, Gaussian filter, median filter, output image of bilateral filter and core value/parameter valueMat G_dstimgbox, G_dstimgblur, G_dstimggaussian, G_dstimgmedian, g_dstimgbilateral;intG_boxfilterval =5;intG_blurval = A;intG_gaussianblurval =5;intG_medianblurval = A;intG_bilateralfilterval = A;Static voidOn_boxfilter (int,void*);Static voidOn_blur (int,void*);Static voidOn_gaussianblur (int,void*);Static voidOn_medianblur (int,void*);Static voidOn_bilateralfilter (int,void*);intMain () {//Read image to G_srcimageG_srcimage = Imread ("D:\\opencv\\ms2.png");if(!g_srcimage.data) {printf("The picture you are reading does not exist ... \ n");return false; }//Clone original to 5 in the image required for filtering, all mat typeG_dstimgbox = G_srcimage.clone (); G_dstimgblur = G_srcimage.clone (); G_dstimggaussian = G_srcimage.clone (); G_dstimgmedian = G_srcimage.clone (); G_dstimgbilateral = G_srcimage.clone ();//Display originalNamedwindow (" Original " ",1); Imshow (" Original " ", g_srcimage);//Box filterNamedwindow (" box Filter" ",1); Createtrackbar ("Kernel value"," box Filter" ", &g_boxfilterval, -, On_boxfilter); On_boxfilter (G_boxfilterval,0); Namedwindow (" mean Filter" ",1); Createtrackbar ("Kernel value"," mean Filter" ", &g_blurval, -, On_blur); On_blur (G_blurval,0); Namedwindow (" Gaussian filter " ",1); Createtrackbar ("Kernel value"," Gaussian filter " ", &g_gaussianblurval, -, On_gaussianblur); On_gaussianblur (G_gaussianblurval,0); Namedwindow (" median filter" ",1); Createtrackbar ("Kernel value"," median filter" ", &g_medianblurval, -, On_medianblur); On_medianblur (G_medianblurval,0); Namedwindow (" Bilateral filtering " ",1); Createtrackbar ("Kernel value"," Bilateral filtering " ", &g_bilateralfilterval, -, On_bilateralfilter); On_bilateralfilter (G_bilateralfilterval,0);cout<<"When you press the" Q "key, the program exits ... \ n"; while(Char(Waitkey (1)) !=' Q ') {}return 0;}Static voidOn_boxfilter (int,void*) {Boxfilter (G_srcimage, G_dstimgbox,-1, Size (G_boxfilterval +1, G_boxfilterval +1)); Imshow (" box Filter" ", G_dstimgbox);}Static voidOn_blur (int,void*) {blur (G_srcimage, G_dstimgblur, Size (G_blurval +1, G_blurval +1), point (-1, -1)); Imshow (" mean Filter" ", G_dstimgblur);}Static voidOn_gaussianblur (int,void*) {Gaussianblur (G_srcimage, G_dstimggaussian, Size (G_gaussianblurval *2+1, G_gaussianblurval *2+1),0,0); Imshow (" Gaussian filter " ", G_dstimggaussian);}Static voidOn_medianblur (int,void*) {Medianblur (G_srcimage, G_dstimgmedian, G_medianblurval *2+1); Imshow (" median filter" ", G_dstimgmedian);}Static voidOn_bilateralfilter (int,void*) {Bilateralfilter (G_srcimage, G_dstimgbilateral, G_bilateralfilterval, G_bilateralfilterval *2, G_bilateralfilterval/2); Imshow (" Bilateral filtering " ", g_dstimgbilateral);}
Image Examples
Original
Box Filter
Mean Value Filter
Gaussian filter
Median filter
Bilateral filtering
Summarize
Through the above pictures of the observation: box and the mean is still relatively similar; Gauss's words for Windows 7 and Windows 10 friends should be more clear, the median looks like a smear feeling, and the bilateral words and the original image is very close, I do not see what the difference.
Extras
Shouldn't it be illegal to advertise on your own blog?
That is, to vote, to vote, to vote!
Please click here: Portal
Voting starts from number 10th and continues to number 20th, please!
Analysis of 5 kinds of image filtering of "OpenCV": box, mean value, Gauss, median, bilateral