"OPENCV Study Note 1" 5 Kinds of image filter discrimination: box, mean, Gaussian, median, bilateral

Source: Internet
Author: User
Tags image filter types of filters

Image filtering
  1. 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.

  2. The purpose of image filtering
    A, eliminate the noise mixed in the image
    B. Extracting image features for image recognition

  3. Requirements for image filtering
    A, can not damage the image contour and Edge
    b, image visual effect should be better

  4. 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.

    Everyone has 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 with a weighted coefficient, where you can see the smoothed image through the lens, through the lens and with the lens movement you can gradually all the image parts

  5. Types of filters
    3 Linear Filters: Box filter, mean filter, Gaussian filter
    2 Kinds of Nonlinear filter: Median filter, bilateral filter

  6. Box Filter
    The kernel used for box filtering:

    When the normaize 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 work to be processed to scale to a certain range, such as (0, 1).

  7. 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.

  8. Gaussian filter
    Gaussian filtering is different, it can eliminate the noise well. In the Gaussian filtering process, each pixel is obtained by weighted average of the other pixel values in itself and within the domain.
    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:

  9. Median filter
    The basic idea of median filtering is to replace the gray value of the pixel by the median value of the gray value of the pixel field, 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.

  10. Bilateral filtering
    The bilateral filtering is a kind of compromise processing combining the spatial proximity of images with the similarity of pixel values, taking into account the spatial domain information and the similarity of gray scale, to achieve the purpose of preserving edge and removing noise.

  11. Function prototypes

A, Box filter

    voidint ddepth, Size ksize, Point anchor = Point(-1,-1boolnormalizetrueint borderType = BORDER_DEFAULT)    

B, mean value filter

    void blur(InputPointPoint(-1,-1), int borderType = BORDER_DEFAULT)

C, median filter

   voidint ksize)

D, bilateral filtering

    voidintdoubledoubleint borderType = BORDER_DEFAULT)

12. Specific parameter analysis

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。

13. 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);}

14. Image Filter Demo





"OPENCV Study Note 1" 5 Kinds of image filter discrimination: box, mean, Gaussian, median, bilateral

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.