Contents
  1. I. Mean Filter
  2. 1.1 Principle of Mean Filtering
  3. 1.2 Mean Filtering in OpenCV
  4. II. Gaussian Blur

I. Mean Filter

1.1 Principle of Mean Filtering

  Mean filtering uses the principle of image convolution. As shown in the figure below, when the convolution kernel is a third-order unit matrix, mean filtering is performed: each third-order submatrix of the original image has its mean computed and assigned to the center element.

  Taking the top-left corner as an example: (A11A_{11}·1+A12A_{12}·1+A13A_{13}·1+  A21A_{21}·1+A22A_{22}·1+A23A_{23}·1+  A31A_{31}·1+A32A_{32}·1+A33A_{33}·1 )/ 9 ->A22A_{22}

Principle of Mean Filtering

1.2 Mean Filtering in OpenCV

  In OpenCV/C++, the blur function is provided to implement the mean filtering operation described above:

void blur(
	InputArray 	src,						//输入图像
	OutputArray dst,						//输出图像
	Size 		ksize,						//卷积核Size类型
	Point 		anchor=Point(-1,-1),		//Point类型的锚点(-1表示锚点在核中心)
	int 		borderType=BORDER_DEFAULT	//边界模式
)

  Here, Size(w, h) denotes the kernel size, where w is the pixel width and h is the pixel height.

  Based on the definition of the blur() function above, we can write a test program for mean filtering.

void MyDemo::blur_Demo(Mat& image) {
	Mat dst;
	blur(image, dst, Size(10, 10), Point(-1, -1));
	imshow("Blur", dst);
}

  The figure below shows the result with a convolution kernel of Size(10,10).

Mean Filtering in OpenCV

  The figure below shows the result with a convolution kernel of Size(1,15).

Mean Filtering in OpenCV (2)

II. Gaussian Blur

  Sometimes we do not want all coefficients of the convolution kernel to be the same during blurring. Gaussian blur is one method for addressing this kind of problem. The coefficients produced by Gaussian blur are largest at the center and become smaller farther from the center.

void cv::GaussianBlur(
	InputArray 	src,		//输入图片,可以使是任意通道数,该函数对通道是独立处理的
	OutputArray dst,		//输出图片
	Size 		ksize,		//高斯内核大小
	double 		sigmaX,		//高斯内核在X方向的标准偏差
	double 		sigmaY,		//高斯内核在Y方向的标准偏差
	int 		borderType	//判断图像边界的模式
)

The number of rows and columns of ksize may differ, but both must be positive odd numbers. If sigmaY is 0, it will take the same value as sigmaX; if both are 0, they are computed from the number of rows and columns of ksize.

Sample program:

void MyDemo::gaussianBlur_Demo(Mat& image) {
	Mat dst;
	GaussianBlur(image, dst, Size(5, 5), 15);
	imshow("GaussianBlur", dst);
}

II. Gaussian Blur