Summary of image denoising filtering algorithms (Python)


Preface

Previous article:Types of image data noise and corresponding noise generated by Python, summarizes common image noise and noise generation methods, mainly used In data augmentation, as a way of filling the data set, model overfitting can be avoided. If you want to understand the image data enhancement algorithm, you can read this article written by me:Summary of image data enhancement algorithms (Python).

This article will introduce commonly used image denoising filtering algorithms, using example codes and processing effects to display them together, so that the effects of each algorithm can be seen more intuitively. This article is more practical, so it will not involve too many principles and theoretical calculation formulas of each algorithm. Use this article to quickly understand and implement these algorithms, and become proficient in this knowledge in the most efficient way.

The blogger has been focusing on data mining for five years, and has participated in dozens of mathematical modeling, large and small, and understands the principles of various models, the modeling process of each model, and various problem analysis methods. The purpose of this column is to quickly use various mathematical models, machine learning, deep learning, and code from scratch. Each article contains practical projects and runnable code. Bloggers keep up with various digital and analog competitions. For each digital and analog competition, bloggers will write the latest ideas and codes into this column, as well as detailed ideas and complete codes. I hope friends in need will not miss the column carefully created by the author.


1. Sources of image noise

With the continuous development of image acquisition technology, we often face various forms of image noise in practical applications, which will affect the quality of the image and the clarity of features. In order to effectively improve the quality of images, a variety of denoising methods have emerged in the field of image processing, which use different principles and technical means to deal with various noise situations. In the following introduction, we will discuss in detail some commonly used image denoising methods, including mean filtering, median filtering, Gaussian filtering, etc., as well as their applicable scenarios and characteristics. Let’s take a deeper look at how to effectively deal with image noise and improve the accuracy and quality of image processing. Image noise can be caused by a variety of factors:

  1. Sensor noise: Image capture devices such as cameras and scanners inherently introduce noise due to small uncertainties in the device's electronics and signal processing.

  2. Environmental conditions: Environmental factors such as insufficient light, strong light, shadows, etc. may cause increased noise in the image.

  3. Interference during transmission or storage: During the image transmission or storage process, the signal may be affected by factors such as electromagnetic interference, compression algorithms, etc., resulting in noise.

  4. Old or damaged equipment: Aged, damaged, or unstable image capture equipment may cause various noises in the image.

  5. Signal sampling error: During the signal sampling process, noise will be introduced due to insufficient sampling rate or quantization error.

  6. Electromagnetic interference: Interference from electromagnetic waves such as electrical equipment and wireless signals may affect image quality.

  7. Sensor characteristics: Different types of sensors (e.g. CMOS, CCD) have different characteristics, and they may introduce specific types of noise during signal processing.

2. Image denoising algorithm

1. Mean filtering

Mean filtering is a commonly used image processing technique. Its principle is to replace the value of each pixel in the image with the average of surrounding pixel values. This smoothes the image and reduces the impact of noise. The specific implementation steps are as follows:

  1. Determine the filter size: The size of the mean filter is determined by a parameter, usually represented by a positive odd number, such as 3x3, 5x5, etc. This value determines the area covered by the filter on the image.

  2. Place filter on each pixel of image: Center the current pixel and overlay the filter on the image.

  3. Calculate the average of surrounding pixels: average all pixel values ​​in the filter coverage area to obtain a new pixel value.

  4. Assign new pixel value to current position: Replace the original pixel value with the calculated new pixel value.

Mean filtering is mainly used to remove random noise in images, such as Gaussian noise or salt and pepper noise. It can effectively reduce the impact of noise while retaining the overall structure of the image.

Using the mean filter algorithm to remove Gaussian noise from images can be achieved through the following steps:

1. Apply mean filter:

# 设置卷积核大小,通常选择奇数
kernel_size = 3
# 使用均值滤波进行去噪
denoised_image = cv2.blur(image, (kernel_size, kernel_size))

 2. Display the original image and the denoised image

cv2.imshow('Original Image', image)
cv2.imshow('Denoised Image', denoised_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

However, mean filtering may not be effective for some specific types of noise (such as periodic noise), because these noises will form specific patterns in the image, and mean filtering can only blur the image and cannot target specific patterns. noise is processed. In general, mean filtering is a simple and effective denoising method, suitable for removing random noise in most cases. But in specific cases, other types of filters or denoising techniques may need to be considered. The larger the convolution kernel, the more pixels will participate in the mean operation, that is, the current calculation is the average of the pixel values ​​​​of more points, the better the denoising effect, and of course the longer the calculation time will be. , and at the same time make the image distortion more serious. Therefore, in actual processing, it is necessary to strike a balance between distortion and denoising effects, and select a convolution kernel of appropriate size.

2. Median filtering

Median filtering is a nonlinear filtering method commonly used in image processing. Its basic idea is to replace the gray value of the pixel with the median value of the neighborhood gray value of the pixel, thereby achieving the purpose of removing noise. There are two steps in total:

  • Position the filter window at a certain pixel of the image.

  • Sort the pixel values ​​in the window by size, and take the middle value as the new value of the pixel.

Image noise categories that can be processed:

Median filtering is mainly suitable for the following types of image noise:

  1. Salt and pepper noise: The black and white pixels that appear in the image.

  2. Striated noise: Horizontal stripes that appear in the image.

  3. Speckle noise: Random white dots scattered throughout the image.

However, median filtering also has some limitations. For example, it will blur the image while retaining image details. The convolution kernel selection has the same problem as the mean filter algorithm. The larger the convolution kernel, the pixels involved in the mean operation will be The more, that is, the current calculation is the average of the pixel values ​​of more points, the better the denoising effect, of course, the longer the calculation time will be, and the more serious the image distortion will be:

gray_image = cv2.cvtColor(noisy_image, cv2.COLOR_BGR2GRAY)
denoised_image = cv2.medianBlur(gray_image, 5)  # 5表示核的大小,可以根据需要调整

cv2.imshow('Original Image', noisy_image)
cv2.imshow('Denoised Image', denoised_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

3.Gaussian filter

Gaussian filter is a linear smoothing filter that uses Gaussian function as the weight function for image filtering. The basic idea of ​​Gaussian filtering is to reduce the impact of noise while retaining the edge information of the image by performing a weighted average of the pixels surrounding each pixel in the image. The Gaussian filter uses a Gaussian function to calculate the weight. Pixels closer to the center point have greater weights, and pixels farther away have smaller weights.

Gaussian filtering is mainly used to remove high-frequency noise in images to make the image smoother. It can also be used to blur the image or reduce the detail information of the image. Gaussian filtering has a very significant effect on removing Gaussian noise and can effectively make the image smoother:

# 设置高斯滤波的内核大小,通常选择一个奇数值
kernel_size = 5
blurred_image = cv2.GaussianBlur(noisy_image, (kernel_size, kernel_size), 0)

cv2.imshow('Original Image', noisy_image)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

4. Bilateral filtering

Bilateral Filtering is a nonlinear filter used for image denoising. Unlike Gaussian filtering, bilateral filtering takes into account the spatial distance between pixels and the similarity between pixel values.

Principles and functions:

  1. Spatial distance weight: In addition to the similarity between pixel values, bilateral filtering also takes into account the spatial distance between pixels. This means that nearby pixels have a greater impact on filtering, while pixels further away have less impact.

  2. Similarity Weight: The bilateral filter uses a Gaussian function to measure the similarity between pixel values. If two pixels have very similar values, they have a greater weight. If their values ​​differ greatly, they have less weight.

  3. Preserve edge information: Compared with general smoothing filters, bilateral filters can preserve the edge information of the image. This is because it takes into account the similarity of pixels at the edges.

Bilateral filters can effectively handle various types of noise, including Gaussian noise, salt and pepper noise, etc.

# 应用双边滤波
filtered_image = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)

 

Restoring and denoising the details of some famous paintings can better reflect this:

before repair

After repair:

 

 Compared with Gaussian filter repair algorithm:

Bilateral filters are particularly useful in situations where image details and edges need to be preserved.

5.NL-Means(Non-Local Means)

NL-Means (Non-Local Means) is an image denoising algorithm that reduces noise levels by utilizing information from similar areas in the image without losing the detailed information of the image. The NL-Means algorithm can not only handle Gaussian noise, but also effectively handle other types of noise such as Poisson noise.

The basic idea of ​​NL-Means algorithm is that for each pixel in the image, denoising is performed by calculating the similarity between the area around the pixel and other pixel areas. Similarity calculations can be based on differences in pixel intensities or can be performed using feature vectors. The basic principles of the NL-Means algorithm are as follows:

  1. Similarity comparison: For each pixel in the image, the NL-Means algorithm first searches for areas similar to the pixel in the local area of ​​the image. The measure of similarity can be based on differences in pixel intensities or can be performed using feature vectors.

  2. Weight calculation: For each similar area, the algorithm will calculate a weight value, which represents the similarity between the similar area and the target pixel. The higher the similarity, the greater the weight value.

  3. Weighted average: Using the calculated weight value, the NL-Means algorithm performs a weighted average of the pixel values ​​of all similar areas to obtain the estimated value of the target pixel.

  4. Repeat processing: The above steps process every pixel in the image to achieve the overall denoising effect.

In this way, the NL-Means algorithm is able to retain the detailed information of the image and avoid the blurring effect while reducing the noise level.

def denoise_nl_means(image, h=10, hForColor=10, templateWindowSize=7, searchWindowSize=21):
    denoised_image = cv2.fastNlMeansDenoisingColored(image, None, h, hForColor, templateWindowSize, searchWindowSize)
    return denoised_image

Noise pictures:

 Picture after NL-Means denoising:

 It can be seen that the NL-Means algorithm retains the detailed information of the image and the image is not so blurry.

Summarize

To sum up, image denoising is an important task in the field of image processing. Its purpose is to eliminate or weaken the noise in the image to make the image clearer and easier to analyze. Different denoising algorithms are suitable for different types and strengths of noise, so in practical applications it is necessary to choose the appropriate method according to the specific situation. At the same time, for specific image processing tasks, it may also be necessary to combine multiple denoising techniques to obtain the best results. In practice, by understanding the principles and characteristics of various denoising algorithms, they can be better applied to solve practical problems, thereby improving the quality and efficiency of image processing.


Guess you like

Origin blog.csdn.net/master_hunter/article/details/134057588