【opencv一日一练】 boxFilter

来源:互联网 发布:ubuntu只有客人会话 编辑:程序博客网 时间:2024/05/01 06:02

boxFilter

Blurs an image using the box filter.

C++: voidboxFilter(InputArraysrc, OutputArraydst, int ddepth, Sizeksize, Pointanchor=Point(-1,-1), bool normalize=true, intborderType=BORDER_DEFAULT )
Python:cv2.boxFilter(src, ddepth, ksize[, dst[, anchor[, normalize[, borderType]]]]) → dst
Parameters:
  • src – input image.
  • dst – output image of the same size and type assrc.
  • ddepth – the output image depth (-1 to usesrc.depth()).
  • ksize – blurring kernel size.
  • anchor – anchor point; default valuePoint(-1,-1) means that the anchor is at the kernel center.
  • normalize – flag, specifying whether the kernel is normalized by its area or not.
  • borderType – border mode used to extrapolate pixels outside of the image.

The function smoothes an image using the kernel:

\texttt{K} =  \alpha \begin{bmatrix} 1 & 1 & 1 &  \cdots & 1 & 1  \\ 1 & 1 & 1 &  \cdots & 1 & 1  \\ \hdotsfor{6} \\ 1 & 1 & 1 &  \cdots & 1 & 1 \end{bmatrix}

where

\alpha = \fork{\frac{1}{\texttt{ksize.width*ksize.height}}}{when \texttt{normalize=true}}{1}{otherwise}

Unnormalized box filter is useful for computing various integral characteristics over each pixel neighborhood, such as covariance matrices of image derivatives (used in dense optical flow algorithms, and so on). If you need to compute pixel sums over variable-size windows, use integral() .

See also

blur(),bilateralFilter(),GaussianBlur(),medianBlur(),integral()

 

////////////////////////////////////////    中文解释  /////////////////////////////////////

方框滤波(box Filter)


方框滤波(box Filter)被封装在一个名为boxblur的函数中,即boxblur函数的作用是使用方框滤波器(box filter)来模糊一张图片,从src输入,从dst输出。

函数原型如下:

 

参数详解:

  • 第一个参数,InputArray类型的src,输入图像,即源图像,填Mat类的对象即可。该函数对通道是独立处理的,且可以处理任意通道数的图片,但需要注意,待处理的图片深度应该为CV_8U, CV_16U, CV_16S, CV_32F 以及 CV_64F之一。
  • 第二个参数,OutputArray类型的dst,即目标图像,需要和源图片有一样的尺寸和类型。
  • 第三个参数,int类型的ddepth,输出图像的深度,-1代表使用原图深度,即src.depth()。
  • 第四个参数,Size类型(对Size类型稍后有讲解)的ksize,内核的大小。一般这样写Size( w,h )来表示内核的大小( 其中,w 为像素宽度, h为像素高度)。Size(3,3)就表示3x3的核大小,Size(5,5)就表示5x5的核大小
  • 第五个参数,Point类型的anchor,表示锚点(即被平滑的那个点),注意他有默认值Point(-1,-1)。如果这个点坐标是负值的话,就表示取核的中心为锚点,所以默认值Point(-1,-1)表示这个锚点在核的中心。
  • 第六个参数,bool类型的normalize,默认值为true,一个标识符,表示内核是否被其区域归一化(normalized)了。
  • 第七个参数,int类型的borderType,用于推断图像外部像素的某种边界模式。有默认值BORDER_DEFAULT,我们一般不去管它。

///  -----------------------------

blur 函数的说明中有这么一段:

The call blur(src,dst,ksize, anchor,borderType) is equivalent toboxFilter(src, dst, src.type(), anchor,true,borderType) .

就是说当boxFilter的第六个参数设为true的时候,其实就相当于blur的效果了,当然,官方文档中有一点小错误,boxFilter的第三个参数是depth,可以用-1或者src.depth(),

而不是src.type()。

 

代码例子:

<span style="font-size:18px;">imshow("src",src);boxFilter(src,dst,src.depth(),Size(5,5));// 相当于 blur(src,dst,Size(5,5));imshow("dst - boxFilter - Size(5,5)",dst);</span>


代码相当简单,这次我换了一张图片,看一下效果。

 

 

 参考:

http://blog.csdn.net/poem_qianmo/article/details/22745559

http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/gausian_median_blur_bilateral_filter/gausian_median_blur_bilateral_filter.html#smoothing 

http://docs.opencv.org/doc/tutorials/imgproc/gausian_median_blur_bilateral_filter/gausian_median_blur_bilateral_filter.html#smoothing 

 

 

 

0 0
原创粉丝点击