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:
where
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_8U, CV_16U, CV_16S, CV_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:
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<opencv.hpp>
#include<iostream>
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()得到的結果是相同的,因為都是均值濾波。