The principle summary and OPENCV Code implementation of box filter, mean filter, median filter, Gaussian filter and bilateral filter for image smoothing technology

Source: Internet
Author: User

Image Smoothing refers to the direct operation of each pixel data of the source image to achieve the purpose of smoothing the image. Essentially is the master convolution accounting sub-realization, convolution accounting sub-related knowledge you can refer to my blog http://blog.csdn.net/wenhao_ir/article/details/51691410

Image smoothing, also known as blur or filtering, is one of the most commonly used techniques in image processing, and it is necessary to use the filter kernel (convolution accounting) to implement different filtering techniques according to the kernel function of the filter. The following describes the general principles of several commonly used image smoothing methods and the implementation code under OPENCV.

First, box filter (mean value filter)

OPENCV provides boxfilter functions and blur functions for Image box filtering (mean filtering) operations. The functions of the two functions are the same, ha.

Mean filter is the use of template accounting sub (convolution accounting sub) to calculate the average gray value of the point domain pixels to replace the gray level of the point. The mean filter algorithm is simple, the calculation speed is faster, but the mean filter itself has inherent defects, that is, it can not well protect the image details, in the image denoising and also destroy the details of the image, so that the image becomes blurred, can not be very good to remove the noise point.

Mean filter is linear filter, linear filter is easy to implement, and easy to analyze from the angle of frequency response, but if noise is particle noise (such as salt and pepper noise) rather than Gaussian noise , linear filter can not remove noise . If the image has extreme points, such as the noise generated by salt and pepper noise, the linear filter simply converts the noise into a flat but still visible particle, the best solution is to filter out the noise by means of nonlinear filtering ( For example, the median filter to be introduced later in this article ).

The above narrative will be verified in the running results of the code provided below!

OPENCV provides boxfilter functions and blur functions for image box filtering (mean filtering) operations, and theBoxfilter function prototype is as follows:

void Boxfilter (Inputarray src, outputarray dst, int ddepth, Size ksize, point Anchor=point ( -1,-1), bool Normalize=true, in T bordertype=border_default)

The official documentation explains the parameters as follows:

Parameters:
Src–source image.
Dst–destination image of the same size and type as SRC.

ksize–smoothing kernel size.
Anchor–anchor Point. The default value point ( -1,-1) means the anchor are at the kernel center.
Normalize–flag specifying whether the kernel is a normalized by it area or not.
Bordertype–border mode used to extrapolate pixels outside of the image.

Let me explain further the meanings of these parameters:

src: source image

DST : Target image

ddepth: This parameter even the official document does not explain, from the following example to see 1, what exactly meaning I do not know.

ksize: convolutional accounting sub-size, as I said in the blog http://blog.csdn.net/wenhao_ir/article/details/51691410, usually take 3 order, The specific convolution core matrix has been set by the box filter algorithm, so here you only need to fill the size.

anchor: Pointing anchor position, that is, each time the convolution accounting sub-calculation is completed, where the point is replaced by the calculated value. If a negative value is taken, then the point in the middle is replaced. If you don't understand what I'm saying, take a look at the convolution kernel algorithms mentioned in http://blog.csdn.net/wenhao_ir/article/details/51691410.

Normalize: is the convolution kernel normalized, my understanding is this: for example, third-order operators, then if you want to be normalized, multiply each element by a one-nineth, because of the nine elements. (Of course, it is only my understanding, I do not dare to confirm)

bordertype: Image boundary processing method. In the convolution accounting sub-image processing before, the source image must be expanded a bit, in order to ensure that every pixel in the source image is accounted sub-processing, the cause of the problem I am in the blog http://blog.csdn.net/wenhao_ir/article/ Details/51691410 in the detailed instructions, we can refer to. The Bordertype is to specify the boundary processing method:

The selectable values for this parameter are defined in the imgproc.hpp of OpenCV, as follows:

//! Various border interpolation methods
enum {border_replicate=ipl_border_replicate, border_constant=ipl_border_constant,
Border_reflect=ipl_border_reflect, Border_wrap=ipl_border_wrap,
Border_reflect_101=ipl_border_reflect_101, border_reflect101=border_reflect_101,
Border_transparent=ipl_border_transparent,
Border_default=border_reflect_101, border_isolated=16};

To be streamlined, it's actually the following:

Border_replicate
Border_constan
Border_reflect
Border_wrap
Border_reflect_101
Border_reflect101
Border_transparent

The meanings of these parameters are explained in the filter.cpp of OpenCV:

/*
Various border types, image boundaries is denoted with ' | '

* Border_replicate:aaaaaa|abcdefgh|hhhhhhh
* BORDER_REFLECT:FEDCBA|ABCDEFGH|HGFEDCB
* BORDER_REFLECT_101:GFEDCB|ABCDEFGH|GFEDCBA
* BORDER_WRAP:CDEFGH|ABCDEFGH|ABCDEFG
* BORDER_CONSTANT:IIIIII|ABCDEFGH|IIIIIII with some specified ' I '
*/

Note that, in my understanding, the first ' | ' in the above explanation Represents the left boundary, and the second ' | ' Represents the right boundary, the left boundary expands by 6 pixels, and the right bounds expands by 7 pixels.

The official documentation explains the Blur function as follows:

The call blur (SRC, DST, ksize, Anchor, Bordertype) are equivalent to Boxfilter (SRC, DST, Src.type (), anchor, True, Bordertype)

So two functions to achieve the same function, that is, the mean filter! I have actually tested it, and the final result is really consistent.

OK, next use the source code of the two functions:

The image download links used in the source code are:

http://pan.baidu.com/s/1kUEDw5x

Http://pan.baidu.com/s/1gfCuMgb

Http://pan.baidu.com/s/1o8aSIuQ

OPENCV version 2.4.9//AC QQ2487872782 #include <opencv2/opencv.hpp> #include <iostream> #include <vector  > Using namespace std;    using namespace CV;  int main () {Mat srcImage1 = Imread ("flower3_pepper.jpg");      Mat srcImage2 = Imread ("flower3_gauss.jpg");  Imshow ("Add the original of pepper Noise", srcImage1);      Imshow ("Add the original of Gaussian noise", srcImage2);      Mat Dstimage1,dstimage2,dstimage3,dstimage4;  Dstimage1.create (Srcimage1.size (), Srcimage1.type ()); Dstimage2.create (Srcimage2.size (), Srcimage2.type ());  Dstimage3.create (Srcimage1.size (), Srcimage1.type ());        Dstimage4.create (Srcimage2.size (), Srcimage2.type ());  Boxfilter (SrcImage1, DstImage1,-1, Size (3, 3));  Boxfilter (SrcImage2, DstImage2,-1, Size (3, 3));    Blur (SrcImage1, DstImage3, Size (3, 3)), Blur (SrcImage2, DstImage4, Size (3, 3));  Imshow ("Boxfilter Handling Pepper Noise", dstImage1);      Imshow ("Boxfilter Processing Gaussian noise" ", dstImage2);  Imshow ("Blur Handling Pepper Noise", dstImage3);        Imshow ("Blur Processing Gaussian noise" ", dstImage4);        Waitkey (0); return 0; }   

The results of the operation are as follows:


From the running results, it is true that the function of Boxfilter and blur function is the same, and it is true that the mean filter has a bad effect on pepper noise, but the processing effect of Gaussian noise is OK.

Two-median filter

Median filtering refers to the use of template accounting sub (what is called template Accounting sub?) Please refer to my blog http://blog.csdn.net/wenhao_ir/article/details/51691410) The sorting of all pixel values in the overlay area, where the pixel values in the middle are used to update the values of the current image point. such as the common accounting sub-3x3, the template area within the elements of 9, sorted by A1,A2,A3,A4,A5,A6,A7,A8,A9, median filter is the value of the current pixel is sorted by 9 elements after the 5th position A5 value to replace the value of the current pixel point. Median filtering is not as linear filter as the mean filter, it is nonlinear filter, mean filter is not good for salt and pepper noise, but median filter is very effective.

OPENCV provides the Medianblur function to realize the median filter of the image, and its realization principle can be obtained by the basic constant time optimization algorithm presented by Simon Perreault and others in the paper "Median Filtering in Constant." Following the improvement and development, Priy Adarshan Kolte and others in the paper "A Fast Median filter using ALTIVEC" proposed a faster median filter implementation algorithm. It is important to note that the order of the template operator should be an odd number because the intermediate value is to be taken.

The following is the source code for median filtering using the Medianblur function:

The image download links used in the source code are:
Http://pan.baidu.com/s/1gfCuMgb
Http://pan.baidu.com/s/1o8aSIuQ

OPENCV version 2.4.9  //AC QQ2487872782 #include <opencv2/opencv.hpp>  #include <iostream>  # Include<vector>    using namespace std;  using namespace CV;    int main ()  {      Mat srcImage1 = Imread ("flower3_pepper.jpg");  Mat srcImage2 = Imread ("flower3_gauss.jpg");      Imshow ("Add the original of pepper Noise", srcImage1);  Imshow ("Add the original of Gaussian noise", srcImage2);      Mat Dstimage1,dstimage2;      Dstimage1.create (Srcimage1.size (), Srcimage1.type ());  Dstimage2.create (Srcimage2.size (), Srcimage2.type ());   Medianblur (srcimage1,dstimage1,3);//3 represents the template core is the 3-order Medianblur (srcimage2,dstimage2,3);//3 represents the template core is the 3-order    imshow ("" Medianblur Processing Pepper Noise "", dstImage1);  Imshow ("Medianblur Processing Gaussian noise" ", dstImage2);      Waitkey (0);        return 0;  }  

The results of the operation are as follows:


From the running results can be seen that the median filter is really good for salt and pepper noise filtering effect, the reason as long as it is easy to know the principle of median filter ~

Tri-Gaussian filtering

Gaussian filtering is the convolution of each pixel of the input array with the Gaussian kernel, and the convolution as the output pixel value. The process of smoothing the image after Gaussian filtering depends on the standard deviation. Its output is a weighted average of the neighborhood pixels, and the higher the pixel weight is, the closer the center is. Therefore, the smoothing effect is better than the mean filter. In image processing, there are generally two ways to achieve Gaussian filtering, one is the use of discrete window sliding window convolution (white is the use of convolution accounting method), the other is the use of Fourier transform (see my blog OpenCV using Fourier transform and inverse transform to achieve the image convolution algorithm, and attached to the convolution core/ Template Accounting Sub-understanding!), the most common is the first sliding window implementation, only when the discretization of the window is very large, with a very large number of sliding window calculation, it may be considered based on the Fourier transform implementation method. Gaussian filtering is one of the most useful filters, and the advantages are usually as follows five points:

⑴ Gaussian function is a single-valued function, Gaussian filtering uses the weighted mean of the pixel neighborhood to replace the pixel value of the point, and the pixel weight decreases monotonically as the distance changes to reduce distortion.

⑵ Gaussian function has rotational symmetry, Gaussian filtering in all directions is the same degree of smoothness, for the presence of noise we can hardly estimate its directionality, so that the smooth performance will not be biased in any direction.

The Fourier transform spectrum of the ⑶ Gaussian function is single-lobe, and Gaussian filtering makes the smoothed image unaffected by unwanted high-frequency signals while preserving most of the required signals.

⑷ Gaussian filtering smoothing degree is determined by the variance σ, the larger the σ, the wider the band, the better the smoothness, for the image of the noise is dedicated to the control parameters can be set.

⑸ Gaussian function is separable, Ivigos function convolution can be carried out in two steps, first the image and a Gaussian function function convolution operation, and then the convolution result and direction perpendicular to the same Gaussian function convolution.

Above five points, at present I can only understand ⑴, ⑷, ⑸ Point, and 2nd and 3rd really do not know why. And the "wider the band, the better the smoothness" in point ⑷, I don't understand. The first ⑸ point is actually the theory of stochastic process to say oh.

OPENCV provides the Gaussianblur function to implement the Gaussian filtering of the image, the function prototype is as follows:

void Gaussianblur (Inputarray src,outputarray DST, Size ksize, double Sigmax, double sigmay=0,int Bordertype=border_defau LT);

The first three parameters and the last parameter I will not say, and the above has been the filter function of the same. The fourth parameter represents the standard variance in the x direction, and if it is 0, it is computed automatically by the order of the kernel, and the fifth parameter represents the standard variance of y upward, and if 0, it is calculated automatically by the order of the kernel.

The Gaussianblur function sample source code is as follows:

The image download links used in the source code are:
Http://pan.baidu.com/s/1gfCuMgb
Http://pan.baidu.com/s/1o8aSIuQ

OPENCV version 2.4.9  //AC QQ2487872782 #include <opencv2/opencv.hpp>  #include <iostream>  # Include<vector>    using namespace std;  using namespace CV;    int main ()  {      Mat srcImage1 = Imread ("flower3_pepper.jpg");  Mat srcImage2 = Imread ("flower3_gauss.jpg");      Imshow ("Add the original of pepper Noise", srcImage1);  Imshow ("Add the original of Gaussian noise", srcImage2);      Mat Dstimage1,dstimage2;      Dstimage1.create (Srcimage1.size (), Srcimage1.type ());  Dstimage2.create (Srcimage2.size (), Srcimage2.type ());   Gaussianblur (Srcimage1,dstimage1,size (3, 3), 0,0);//3 represents a 3-step Gaussianblur (Srcimage2,dstimage2,size (3, 3), 0,0);// 3 for the template nucleus is the 3-order    imshow ("Gaussianblur Processing pepper Noise", dstImage1);  Imshow ("Gaussianblur Processing Gaussian noise" ", dstImage2);      Waitkey (0);        return 0;  }  

The results of the operation are as follows:


From the running results we can see that the Gaussian filter on the salt and pepper noise effect is not good, but the Gaussian noise effect is not bad!


Four-sided filtering

The bilateral filter is a nonlinear filtering method. Bilateral filtering assigns two parts weighting coefficients to each neighborhood pixel, the first part weighted coefficient is the same as the Gaussian filter, and the weight of the second part depends on the gray difference between the domain pixel and the current pixel. This is the meaning of the bilateral, that is, in giving weight to consider two aspects.

The bilateral filter holds too much high-frequency information ( you ask me why?) I'm not sure, either . Therefore, the bilateral filter can not filter out the high frequency noise in the color image cleanly, and can only better filter the low frequency information. Therefore, for impulse noise, the bilateral filter is not filtered.

The Bilateralfilter function is provided in OPENCV to achieve bilateral filtering, and the function prototype is as follows:

void Bilateralfilter (Inputarray src, outputarray dst, int D, double sigmacolor, double sigmaspace, int bordertype=border _default);

The official explanations are as follows:

Parameters:

Src–source 8-bit or floating-point, 1-channel or 3-channel image.
Dst–destination image of the same size and type as SRC.
D–diameter of each pixel neighborhood this is used during filtering. If It is non-positive, it's computed from Sigmaspace.
Sigmacolor–filter Sigma in the color space. A larger value of the parameter means that farther colors within the pixel neighborhood (see Sigmaspace) would be mixed to Gether, resulting in larger areas of semi-equal color.
Sigmaspace–filter Sigma in the coordinate space. A larger value of the parameter means that farther pixels would influence each other as long as their colors is close Enou GH (see Sigmacolor). When d>0, it specifies the neighborhood size regardless of sigmaspace. Otherwise, D is proportional to sigmaspace.

I try to translate the following:

src-Source Image

dst-Target Image

The radius of the D-calculation, the number of pixels within the radius, is included in the calculation, and if 1 is supplied, it is automatically calculated from the sigmaspace of the parameter below.

sigmacolor-This parameter is actually referred to as the "difference" refers to the pixel neighborhood range, I said above is "the weight of the second part depends on the domain pixel and the current pixel gray difference", then this neighborhood to take how much, it is determined by this parameter!

sigmaspace-This parameter is the same as σ in the Gaussian filter function. If the D value is greater than 0, then this value will not affect the result of the calculation, if d<=0, then the value of D is determined by the Sigmaspace parameter.

PS: The last two parameters as of the end of my blog when I am not sure if that is the meaning, so only for your reference ~ and so on after the need to thoroughly understand the principle of further to modify the blog post supplement.

The Bilateralfilter sample code is as follows:

The image download links used in the source code are:
Http://pan.baidu.com/s/1gfCuMgb
Http://pan.baidu.com/s/1o8aSIuQ

OPENCV version 2.4.9  //AC QQ2487872782 #include <opencv2/opencv.hpp>  #include <iostream>  # Include<vector>    using namespace std;  using namespace CV;    int main ()  {      Mat srcImage1 = Imread ("flower3_pepper.jpg");  Mat srcImage2 = Imread ("flower3_gauss.jpg");      Imshow ("Add the original of pepper Noise", srcImage1);  Imshow ("Add the original of Gaussian noise", srcImage2);      Mat Dstimage1,dstimage2;      Dstimage1.create (Srcimage1.size (), Srcimage1.type ());  Dstimage2.create (Srcimage2.size (), Srcimage2.type ());   Bilateralfilter (srcimage1,dstimage1,7,20.0,2.0);//3 represents a 3-step bilateralfilter (srcimage2,dstimage2,7,20.0,2.0);// 3 for the template nucleus is the 3-order    imshow ("Bilateralfilter Processing pepper Noise", dstImage1);  Imshow ("Bilateralfilter Processing Gaussian noise" ", dstImage2);      Waitkey (0);        return 0;  }  

The results of the operation are as follows:


From the running results, it is obvious that bilateral filtering can not deal with pepper noise, but it seems that the effect of Gaussian noise is general!

------------------------------------------
Welcome everyone to join the image recognition technology Exchange Group: 271891601, in addition, the special welcome Chengdu engaged in image recognition work of friends, my QQ number 2487872782

The principle summary and OPENCV Code implementation of box filter, mean filter, median filter, Gaussian filter and bilateral filter for image smoothing technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.