当前位置:   article > 正文

opencv中常用的线性滤波器--boxFilter(),blur(),GaussianBlur()_opencv boxfilter

opencv boxfilter

1. boxFilter()


下面是opencv官方对boxFilter()函数的介绍。如果均衡化(即normalize==ture,这也是默认值),则其本质是均值滤波。

C++:  void  boxFilter (InputArray  src, OutputArray  dst, int  ddepth, Size  ksize, Point  anchor=Point(-1,-1), bool normalize=true, int  borderType=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 as src.
  • ddepth – the output image depth (-1 to use src.depth()).
  • ksize – blurring kernel size.
  • anchor – anchor point; default value Point(-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}


2. blur()


调用blur()等效于调用将normalize=true的boxFilter().

Blurs an image using the normalized box filter.

C++:  void  blur (InputArray  src, OutputArray  dst, Size  ksize, Point  anchor=Point(-1,-1), int borderType=BORDER_DEFAULT  )
Python:   cv2. blur (src, ksize [, dst [, anchor [, borderType ] ] ] ) → dst
Parameters:
  • src – input image; it can have any number of channels, which are processed independently, but the depth should be CV_8UCV_16UCV_16SCV_32F or CV_64F.
  • dst – output image of the same size and type as src.
  • ksize – blurring kernel size.
  • anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
  • borderType – border mode used to extrapolate pixels outside of the image.

The function smoothes an image using the kernel:

\texttt{K} =  \frac{1}{\texttt{ksize.width*ksize.height}} \begin{bmatrix} 1 & 1 & 1 &  \cdots & 1 & 1  \\ 1 & 1 & 1 &  \cdots & 1 & 1  \\ \hdotsfor{6} \\ 1 & 1 & 1 &  \cdots & 1 & 1  \\ \end{bmatrix}

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

3.GaussianBlur()

高斯滤波可以消除高斯噪声,广泛应用于图像处理的减噪过程。需要注意的是opencv中的GaussianBlur()是高斯低通滤波器,用来模糊减噪,所以叫高斯模糊。

整数模板用的比较多,常见的3x3或者5x5的整数模板如下。更多高斯滤波的讲解可以参考下面这篇博客http://blog.csdn.net/yansmile1/article/details/46275791

4. 应用实例

分别调用上面提到的三个函数对一副图像进行模糊操作,选取的kernel size为5x5。代码如下

  1. #include<opencv.hpp>
  2. #include<iostream>
  3. int main(void)
  4. {
  5. cv::Mat src = cv::imread("d:/Opencv Picture/Lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
  6. if (!src.data)
  7. {
  8. std::cout << "image read error!!!\n";
  9. }
  10. cv::imshow("src",src);
  11. //call boxFilter()
  12. cv::Mat boxFilterDst;
  13. cv::boxFilter(src, boxFilterDst, -1, cv::Size(5, 5));
  14. cv::imshow("boxFilterResult", boxFilterDst);
  15. //call blur()
  16. cv::Mat blurDst;
  17. cv::blur(src, blurDst, cv::Size(5, 5));
  18. cv::imshow("blurResult", blurDst);
  19. //call GaussianBlur()
  20. cv::Mat GaussianDst;
  21. cv::GaussianBlur(src, blurDst, cv::Size(5, 5),0.8,0.8);
  22. cv::imshow("GaussianBlurResult", blurDst);
  23. cvWaitKey(0);
  24. return 0;
  25. }

运行结果如下,明显在相同的kernel size的情况下,GaussianBlur()的结果对原图的失真比较少(当然还和sigmaX & sigmaY有关),而boxFilter & blur()得到的结果是相同的,因为都是均值滤波。



声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号