Gaussian filter algorithm and routines

Gaussian filtering algorithm is a linear smoothing filtering method used to blur images or remove noise. It is a convolution operation based on the Gaussian function, which achieves a smoothing effect by performing a weighted average on each pixel of the image.

The following are the steps of the Gaussian filter algorithm:

  1. Define a two-dimensional Gaussian kernel (also called a filter) whose size is determined by the filter size and standard deviation (sigma). Common Gaussian kernel sizes are 3x3, 5x5, 7x7, etc.

  2. Apply a Gaussian kernel to each pixel of the image.

  3. For each pixel, a weighted average is calculated between it and the pixels in the surrounding neighborhood. The weight is determined based on the numerical distribution of the Gaussian kernel. The farther away from the central pixel, the smaller the weight of the pixel.

  4. Use the weighted average as the new value of the current pixel.

  5. Repeat steps 2-4 until the entire image is traversed.

The core idea of ​​the Gaussian filter algorithm is to use the Gaussian function to simulate the propagation of pixel values, so that the influence of surrounding pixels weakens as the distance increases. This smoothes the image while preserving the overall characteristics of the image.

In practical applications, the size and standard deviation of the Gaussian kernel can be adjusted as needed. Larger filter sizes and smaller standard deviations can produce stronger smoothing, but may blur details; smaller filter sizes and larger standard deviations can preserve more details, but may have weaker noise suppression. .

It should be noted that the Gaussian filter algorithm has a better denoising effect on Gaussian noise and some other types of noise. However, for isolated outlier noise such as salt and pepper noise, the Gaussian filter algorithm is not the best choice. Other filtering methods can be considered, such as median filtering.

The following is a routine to implement Gaussian filtering using Python and OpenCV:

import cv2

def gaussian_filter(image, kernel_size, sigma):
    # 应用高斯滤波器
    filtered_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
    return filtered_image

# 读取图像
image = cv2.imread('input.jpg')

# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 应用高斯滤波器
filtered_image = gaussian_filter(gray_image, kernel_size=5, sigma=1.0)

# 显示原图像和滤波后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Filtered Image', filtered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we define a function called gaussian_filter which accepts the image, filter size and sigma value as parameters and returns the Gaussian filtered image. Inside the function, we use the cv2.GaussianBlur function to perform Gaussian filtering operations, where the first parameter is the input image, the second parameter is the size of the filter, and the third parameter is the sigma value.

In practical applications, the appropriate filter size and sigma value can be selected according to needs. A smaller filter size and a larger sigma value can smooth the image better, but may result in a loss of detail; a larger filter size and a smaller sigma value can preserve more detail, but may not Effectively remove larger noise.

Please make sure to replace 'input.jpg' with your own image file path before running the sample code.

Guess you like

Origin blog.csdn.net/wangjiaweiwei/article/details/131668844
Recommended