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

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。代码如下

#include
#include

int main(void)
{
	cv::Mat src = cv::imread("d:/Opencv Picture/Lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);

	if (!src.data)
	{
		std::cout << "image read error!!!\n";
	}
	cv::imshow("src",src);

	//call boxFilter()
	cv::Mat boxFilterDst;
	cv::boxFilter(src, boxFilterDst, -1, cv::Size(5, 5));
	cv::imshow("boxFilterResult", boxFilterDst);

	//call blur()
	cv::Mat blurDst;
	cv::blur(src, blurDst, cv::Size(5, 5));
	cv::imshow("blurResult", blurDst);

	//call GaussianBlur()
	cv::Mat GaussianDst;
	cv::GaussianBlur(src, blurDst, cv::Size(5, 5),0.8,0.8);
	cv::imshow("GaussianBlurResult", blurDst);

	cvWaitKey(0);
	
	return 0;
}

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

opencv中常用的线性滤波器--boxFilter(),blur(),GaussianBlur()_第1张图片


你可能感兴趣的:(opencv,数字图像处理基础)