"OpenCV" Neighborhood Filter: box, Gaussian, median, bilateral filtering

Source: Internet
Author: User

transferred from: http://blog.csdn.net/xiaowei_cqu/article/details/7785365

Author: xiaowei_cqu

Neighborhood Filtering (convolution)
The neighborhood operator value determines the final output of this pixel using the value of the pixels around a given pixel. The image on the left of the picture and the middle image convolution Shancheng the right image. The green pixels in the target image are computed by the blue-flagged pixels in the original image.


Universal Linear neighborhood filtering is a commonly used neighborhood operator, with input pixel weighting to get output pixels:


The weight of the kernel is " filter coefficient ". The above equation can be précis-writers as:



The simplest linear filter of "Square filter" is the moving average or box filter, which is output with the average pixel value in the window, and the kernel function is:

In fact, the equivalent of the image and all the element value of 1 kernel function convolution and then scale scaling.
Code The Blur function in OpenCV is a standard box filter: [CPP] view plain copy print?   void Cv::blur (inputarray src, outputarray DST, Size Ksize, point anchor, int bordertype)   {boxfilter (src, DST,-1, Ksize, anchor, true, Bordertype); }

and the Boxfilter function source code is as follows:[CPP]View Plain copy print? CV::P tr<cv::filterengine> Cv::createboxfilter (intSrctype,intDsttype, Size Ksize, point anchor,BOOLNormalizeintBordertype) {intSdepth = Cv_mat_depth (srctype);intcn = CV_MAT_CN (srctype), sumtype = cv_64f;if( sdepth <= CV_32S &&  (!normalize | |            ksize.width*ksize.height <=  (sdepth  == CV_8U ?  (1<<23)  :                sdepth == CV_16U ?  (1 << 15)  :  (1  << 16))  )            sumtype =  CV_32S;       sumtype = cv_maketype ( sumType, cn ) ;          Ptr<BaseRowFilter> rowFilter =  Getrowsumfilter (srctype, sumtype, ksize.width, anchor.x );        ptr<basecolumnfilter> columnfilter = getcolumnsumfilter (sumType,            dsttype, ksIze.height, anchor.y, normalize ? 1./(ksize.width*ksize.height)  : 1);           returnPtr<filterengine> (NewFilterengine (ptr<basefilter> (0), RowFilter, Columnfilter, Srctype, Dsttype, Sumtype, Bordertype)); The difference between Blur and Boxfilter here is that Blur is the normalized boxfilter, the kernel function of boxfilter: which,[CPP]View Plain copy print?   Blur (SRC, DST, Size (1, 1), point ( -1,-1));   Blur (SRC, DST, Size (4, 4), point ( -1,-1));   Blur (SRC, DST, Size (8, 8), point ( -1,-1)); Blur (SRC, DST, Size (+), point ( -1,-1));

The result of the experiment is that the image is filtered with a 1*1,4*4,8*8,16*16 standard box for an image:


"Gaussian filter"Gaussian filter is a kind of linear smoothing filter which chooses weights according to the shape of Gaussian function. It is effective in removing noise that obeys a normal distribution.
Commonly used 0 mean discrete Gaussian filter functions:

The 2D image is represented as:
Code [CPP]View Plain copy print?                                       /****************************************************************************************\ Gaussian Blur \****************************************************************************************/CV:: Mat Cv::getgaussiankernel ( intN DoubleSigma intKtype) { Const intSmall_gaussian_size = 7; Static Const floatSmall_gaussian_tab[][small_gaussian_size] = {{1.f}, {0.25f, 0.5f, 0.25f}, {0.0625f, 0 .25f, 0.375f, 0.25f, 0.0625f}, {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}}; Const float* Fixed_kernel = n 2 = = 1 && n <= small_gaussian_size && sigma <= 0?          Small_gaussian_tab[n>>1]: 0;       Cv_assert (Ktype = = cv_32f | | ktype = = cv_64f); Mat kernel (n, 1, ktype); float* CF = ( float*) Kernel.data; Double* cd = ( Double*) Kernel.data; DoubleSigmax = sigma > 0? Sigma: ((n-1) *0.5-1) *0.3 + 0.8; Doublescale2x = -0.5/(Sigmax*sigmax); Doublesum = 0; intI for(i = 0; i < n; i++) { Doublex = i-(n-1) *0.5; Doublet = Fixed_kernel? ( Double) Fixed_kernel[i]: Std::exp (scale2x*x*x); if(Ktype = = cv_32f) {Cf[i] = ( float) T;           Sum + = Cf[i]; } Else{Cd[i] = t;           Sum + = Cd[i]; }} sum = 1./sum; for(i = 0; i < n; i++) { if(Ktype = = cv_32f) Cf[i] = ( float) (cf[i]*sum); ElseCd[i] *= sum; } returnKernel } CV::P tr<cv::filterengine> Cv::creategaussianfilter ( intType, Size ksize, DoubleSIGMA1, DoubleSIGMA2, intBordertype) { intDepth = cv_mat_depth (type); if(sigma2 <= 0) sigma2 = sigma1; Automatic detection of kernel size from sigma if(ksize.width <= 0 && sigma1 > 0) ksize.width = Cvround (sigma1* (depth = cv_8u? 3:4) * * + 1) | if( ksize.height <= 0 && sigma2 > 0 )             ksize.height = cvround (sigma2* (Depth == CV_8U  ? 3 : 4) *2 + 1) |1;          cv_assert (  ksize.width > 0 && ksize.width % 2 == 1 & &           ksize.height > 0 &&  ksize.height % 2 == 1 );          sigma1  = std::max ( sigma1, 0. );   &NBSP;&NBSP;&NBSP;&NBSP;SIGMA2&NBSP;=&NBSP;STD: : Max ( sigma2, 0. );          Mat kx =  Getgaussiankernel ( ksize.width, sigma1, std::max (depth, cv_32f)  );        mat ky;        if(Ksize.height = = Ksize.width && std::abs (SIGMA1-SIGMA2) < Dbl_epsilon) KY = kx; ElseKY = Getgaussiankernel (Ksize.height, Sigma2, Std::max (depth, cv_32f)); returnCreateseparablelinearfilter (type, type, KX, KY, point ( -1,-1), 0, Bordertype); } voidCv::gaussianblur (Inputarray _src, Outputarray _dst, Size ksize, DoubleSIGMA1, DoubleSIGMA2, intBordertype) {Mat src = _src.getmat ();       _dst.create (Src.size (), Src.type ()); Mat DST = _dst.getmat (); if(Bordertype! = border_constant) { if(src.rows = = 1) ksize.height = 1; if(Src.cols = = 1) ksize.width = 1; } if(Ksize.width = = 1 && ksize.height = = 1) {Src.copyto (DST); return; } #ifdef Have_tegra

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.