Gauss filtering

1. Concept introduction

 Gaussian filtering is a kind of 线性平滑滤波noise reduction process that is suitable for eliminating Gaussian noise and is widely used in image processing.

 In layman's terms, Gaussian filtering is that 对整幅图像进行加权平均的过程the value of each pixel is obtained by weighted average of itself and other pixel values ​​in the neighborhood.

 Gaussian filtering is: use a template (or convolution, mask) to scan each pixel in the image, and replace it 具体操作with the pixels in the neighborhood determined by the template .   For mean filtering and box filtering, the weight of each pixel in its neighborhood is equal. On this basis, the sum of different weights of each pixel value in the neighborhood is calculated.加权平均灰度值模板中心像素点的值

而在高斯滤波中,会将中心点的权重值加大,远离中心点的权重值减小

2. Basic principles

 In Gaussian filtering, the value of the convolution kernel is no longer 1. For example, a 3×3 convolution kernel might look like Figure 2-1.
insert image description here
Figure 2-1 Example of Gaussian filter convolution kernel

 In Figure 2-2, Gaussian convolution is performed on the pixel with a pixel value of 226 at the 4th row and 3rd column position in the leftmost image 运算规则. The pixels of are calculated according to different weights.
insert image description here
Figure 2-2 Gaussian convolution example

 In actual calculation, the convolution kernel used is shown in Figure 2-3.
insert image description here
Figure 2-3 The convolution kernel in the actual calculation

 Use the convolution kernel in Figure 2-3 to perform Gaussian filtering on the pixel at the position of the fourth row and third column with a pixel value of 226. The calculation method is: new
value =(40×0.05+107×0.1+5×0.05)
+(198×0.1+226×0.4+223×0.1)
+(37×0.05+68×0.1+193×0.05)
=164

 In actual use, Gauss Filtering may use convolution kernels of different sizes. For example, in Figures 2-4 are convolution kernels of 3×3, 5×5, and 7×7 sizes. 在高斯滤波中,核的宽度和高度可以不相同,但是它们都必须是奇数.
insert image description here
Figure 2-4 Convolution kernels of different sizes

每一种尺寸的卷积核都可以有多种不同形式的权重比例. For example, the same 5×5 convolution kernel may have two different weight ratios as shown in Figure 2-5.
insert image description here
Figure 2-5 Convolution kernels of the same size can have different weight ratios

在实际计算中,卷积核是归一化处理的. This processing can be expressed as the convolution kernel in decimal form on the far left of Figure 2-4, or as shown in Figure 2-5 Fractional form.
 It should also be noted that in some materials, the given convolution kernel has not been normalized. At this time, the convolution kernel may be represented as the convolution kernel shown in the middle and right of Figure 2-4. Such a convolution The kernel is used to illustrate the problem, and normalization is often required in actual use. 严格来讲,使用没有进行归一化处理的卷积核进行滤波,得到的结果往往是错误的.

3. Function syntax

In OpenCV, the function to realize Gaussian filtering is cv2.GaussianBlur(), the syntax format of this function is:
dst = cv2.GaussianBlur( src, ksize, sigmaX, sigmaY, borderType )

Parameter analysis :
dst: The return value indicates the processing result obtained after Gaussian filtering.
src: is the image that needs to be processed, that is, the original image. It can have any number of channels and can process each channel independently. Image depth should be one of CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
ksize: is the size of the filter kernel. The filter kernel size refers to the height and width of its neighborhood image during the filtering process. 需要注意,滤波核的值必须是奇数.
sigmaX: The standard deviation of the convolution kernel in the horizontal direction (X-axis direction), which controls the weight ratio. As shown in Figure 2-5 below, the convolution kernels determined by different sigmaX have different standard deviations in the horizontal direction.
insert image description here
Figure 2-5 Convolution kernel determined by different sigmaX

sigmaY : the standard deviation of the convolution kernel in the vertical direction (Y-axis direction). If the value is set to 0, only the value of sigmaX is used; if both sigmaX and sigmaY are 0, it is calculated from ksize.width and ksize.height. Where:
  sigmaX = 0.3×[(ksize.width-1)×0.5-1] + 0.8
  sigmaY = 0.3×[(ksize.height-1)×0.5-1] + 0.8
borderType: Border style, this value determines how way to deal with borders. In general, you don't need to consider this value, just use the default value.

 In this function, sigmaY 和 borderType 是可选参数。sigmaX 是必选参数,但是可以将该参数设置为 0,让函数自己去计算 sigmaX 的具体值.

4. Program example

Perform Gaussian filtering on a noisy image and display the filtering results.

import cv2
Gn=cv2.imread("Gaussian_noise.jpg") 
Gf=cv2.GaussianBlur(Gn,(3,3),0,0)
cv2.imshow("噪声图像",Gn)
cv2.imshow("高斯滤波处理结果图像",Gf)
cv2.waitKey()
cv2.destroyAllWindows()

After the program is running, Figure 4-1 is the noise image, and Figure 4-2 is the processed image after Gaussian filtering. insert image description here
Figure 4-1 Noise image

insert image description here
Figure 4-2 Gaussian filter processing result image

Guess you like

Origin blog.csdn.net/weixin_51571728/article/details/121527964