Gaussian blur based on opencv

Gaussian Blur

The blur and smoothness of an image mean the same level, and the smoothing process is a blurring process. The image denoising can be achieved by blurring and smoothing the image (there are other methods for image denoising). So how can we blur and smooth an image? Image blur smoothing is the process of averaging the image matrix. Compared to image sharpening (differential process), image smoothing is an integral process. The image smoothing process can be achieved by convolving the original image with an integral operator.

The simplest integral operator is the all-ones operator. The all-ones operator
Insert picture description here
can be used to perform blur and smooth operations on the image and has a certain denoising ability.

The use of Gaussian operator for blur processing is the Gaussian blur that we often hear. Gaussian filtering is a linear smoothing filter, which is suitable for eliminating Gaussian noise and is widely used in the noise reduction process of image processing. In layman's terms, filtering Gaussian is the process of weighted averaging the entire image. The value of each pixel is obtained by weighted average of itself and other pixel values ​​in the neighborhood.

The specific operation of Gaussian filtering is to scan each pixel in the image with a template (or convolution, mask), and use the weighted average gray value of the pixels in the neighborhood determined by the template to replace the value of the center pixel of the template.

Here we look at Gaussian function: σ is the standard deviation of the Gaussian distribution of the formula
Insert picture description here
of 5 * 5 elements each Gaussian rounding operator can obtain the following matrix
Insert picture description here
to see the image in some areas directly above the Gaussian blur matrix, the actual The above uses the Gaussian approximation operator with a standard deviation of 2.

void GaussianBlur(Mat& src, Mat& result, int baseKernel, double delta)
{
    
    
	//高斯核半径
	int kerR = baseKernel / 2;
	//高斯核因子
	Mat kernel = Mat_<double>(baseKernel, baseKernel);
	//归一化参数
	double alpha = 1 / ((2*22/7)*delta*delta);
	//核函数生成
	for (int i = -kerR; i <= kerR; i++)
	{
    
    
		for (int j = -kerR; j <= kerR; j++)
		{
    
    
			kernel.at<double>(i + kerR, j + kerR) = exp(-(i*i+j*j)/(2*delta*delta))*alpha;
		}
	}
	result = src.clone();
	double pix;
	for (int i = kerR; i < src.rows - kerR; i++)
	{
    
    
		for (int j = kerR; j < src.cols - kerR; j++)
		{
    
    
			pix = 0;
			for (int pi = -kerR; pi <= kerR; pi++)
			{
    
    
				for (int pj = -kerR; pj <= kerR; pj++)
				{
    
    
					pix += src.at<uchar>(i+pi, j+pj)*kernel.at<double>(kerR+pi,kerR+pj);
				}
			}
			result.at<uchar>(i-kerR, j-kerR) = pix;
		}
	}
}

In opencv, the system provides such an API:

GaussianBlur(Mat src,Mat dst,Size(),sigmaX,sigmaY)

Parameter explanation:
src: input image, which can be of Mat type, image depth is CV_8U, CV_16U, CV_16S, CV_32F, CV_64F
dst: output image, with the same type and size as the input image
Size: Gaussian kernel size, ksize.width and ksize .height can be different but these two values ​​must be positive odd numbers. If these two values ​​are 0, their values ​​will be calculated by sigma.
double sigmaX: the standard deviation of the Gaussian kernel function in the X direction.
double sigmaY: the standard deviation of the Gaussian kernel function in the Y direction. If sigmaY is 0, the function will automatically set the value of sigmaY to the same value as sigmaX. Both sigmaX and sigmaY are 0, and these two values ​​will be determined by ksize. Width and ksize.height are calculated.

When sigmaX=1 and sigmaxY=9,

	Mat image = imread("E:\\picture\\lena.jpg");
	imshow("原始图", image);

	//Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
	Mat dstimg;
	GaussianBlur(image, dstimg,Size(15,15),1,9);
	imshow("Gauss", dstimg);

	imwrite("E:\\picture\\lenaout.jpg", dstimg);
	waitKey(0);

Insert picture description here
Similarly, when sigmaX=9 and sigmaY=1,

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36587495/article/details/108554504