opencv notes: Gaussian filtering and median filtering for salt and pepper noise processing

 

Table of contents

1. Introduction to salt and pepper noise

2. The principle and implementation of Gaussian filtering

 2.1. The principle of Gaussian filtering

   2.2. Gaussian filter API 

3. The principle and implementation of median filtering

  3.1. The principle of median filtering

  3.2. Median filter API

4. Processing results of salt and pepper noise by Gaussian filter and median filter


In digital image processing, noise will lead to image quality degradation and loss of information, so it is necessary to use image noise reduction filtering algorithm to reduce the impact of noise on the image. Among them, salt and pepper noise is a type of noise that often occurs because it can be caused by signal interference during transmission or sensor failure. Gaussian filtering and median filtering are two common image filtering algorithms, both of which can effectively deal with salt and pepper noise. This article will introduce the implementation principles of Gaussian filtering and median filtering algorithms, compare their effects on salt and pepper noise, and draw a conclusion based on the advantages and disadvantages of the two methods, in order to provide some useful reference for the practice of image processing.

1. Introduction to salt and pepper noise

Salt and pepper noise is also called impact noise (or impulse noise). It appears as discretely distributed pure white or black pixels on the image, and the position of appearance is usually random and discontinuous. Since under normal circumstances, gray pixels with maximum/minimum values ​​are unlikely to appear in the image, such pixels can be regarded as noise.

The salt and pepper noise may be caused by environmental interference (such as electromagnetic interference), internal timing errors of the sensor (ADC), and so on.

2. The principle and implementation of Gaussian filtering

 2.1. The principle of Gaussian filtering

Gaussian filtering is a common filtering method, and its kernel form is

G(x,y)=\frac{1}{2\pi \sigma ^{2}}e^{\frac{-(x^2+y^2)}{2\sigma ^2}}

 where (x,y)is the coordinate of the point in the image, \sigmais the standard deviation, and the Gaussian mask is calculated using this function. Both x and y are coordinate values ​​representing the origin of coordinates with the center point of the kernel. Here is an introduction to \sigmathe function. When \sigmait is small, the coefficient in the center of the generated Gaussian mask is relatively large, while the surrounding coefficients are relatively small, so the smoothing effect on the image is not obvious. When \sigmait is larger, the coefficients of the generated mask have little difference, which is similar to the mean mask, and the smoothing effect on the image is more obvious.

The main function of Gaussian filtering is to eliminate Gaussian noise, that is, noise that conforms to a normal distribution.


 
  2.2. Gaussian filter API 

dst=cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType)

         ● dst is the return value, indicating the processing result obtained after Gaussian filtering.

         ● src is the image to be processed, that is, the source 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 Gaussian filter kernel. ksize.width and ksize.height can be different, but they both must be positive and odd, or zero, and then calculated from sigmaX and sigmaY.

        ● sigmaX Gaussian kernel standard deviation in the X direction.

        ● sigmaY Gaussian kernel standard deviation in the Y direction.

If sigmaY is zero, it is set equal to sigmaX; if both sigmas are zero, they are calculated from ksize.width and ksize.height respectively; for full control over the result, regardless of possible future modifications to all these semantics , it is recommended to specify all ksize, sigmaX and sigmaY.

3. The principle and implementation of median filtering

  3.1. The principle of median filtering

The median filter is a commonly used nonlinear filter. Its basic principle is to select the median value of each pixel value in a neighborhood of the pixel to be processed to replace the pixel to be processed. Its main function is to make the gray value of the pixel Close to the surrounding pixels to eliminate isolated noise points.

The median filter can effectively protect the boundary information of the image while eliminating noise, and will not cause a lot of blur to the image (compared to the mean filter).

The effect of the median filter is greatly affected by the size of the filtering window, and there is a contradiction between eliminating noise and protecting image details: the smaller the filtering window, the better the protection of image details, but the filtering effect on noise Not very good; on the contrary, a larger window size has a better noise filtering effect, but it will cause a certain blur to the image (in this case, an adaptive median filter is needed). In addition, according to the principle of the median filter, if the number of noise points in the filtering window is greater than the number of pixels in the entire window, the median filter cannot filter out the noise well. 


  3.2. Median filter API

dst=cv2.medianBlur(src,ksize)

        ● dst is the return value, indicating the processing result obtained after median filtering.

         ● src is the image to be processed, that is, the source 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. It should be noted that the core size must be an odd number greater than 1, such as 3, 5, 7, etc.

4. Processing results of salt and pepper noise by Gaussian filter and median filter

# image filtering
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

# 直接读取图像(灰度图像、彩色)
img1 = cv.imread("E:\\loaddown\\opencv_test\\1.jpg")
noisy_img = np.copy(img1) #加噪声的图像
# 给图像加入椒盐噪声
s_vs_p = 0.5 #设置椒盐噪声的数目比例
amount = 0.04 #设置添加噪声图像像素的数目

#添加salt噪声
num_salt = np.ceil(amount * img1.size * s_vs_p)
coords = [np.random.randint(0, i-1, int(num_salt)) for i in img1.shape] #设置salt噪声的坐标位置
noisy_img[coords[0], coords[1], :] = [255,255,255]

#添加pepper噪声
num_pepper = np.ceil(amount * img1.size * (1 - s_vs_p))
coords = [np.random.randint(0, i-1, int(num_pepper)) for i in img1.shape]
noisy_img[coords[0], coords[1], :] = [0,0,0]

# 两种滤波:高斯滤波
gaussianBlur_img_0 = cv.GaussianBlur(noisy_img, (5,5), 0)
gaussianBlur_img_3 = cv.GaussianBlur(noisy_img, (5,5), 3)
#中值滤波
medianBlur_img_3 = cv.medianBlur(noisy_img,3)
medianBlur_img_5 = cv.medianBlur(noisy_img,5)
# 显示图像

fig,axes = plt.subplots(nrows=1, ncols=2, figsize=(16,12), dpi=400)
axes[0].imshow(img1[:,:,::-1])
axes[0].set_title("orignal")
axes[1].imshow(noisy_img[:,:,::-1])
axes[1].set_title("noisy_img")
plt.show()
fig,axes = plt.subplots(nrows=1, ncols=2, figsize=(16,12), dpi=400)
axes[0].imshow(gaussianBlur_img_0[:,:,::-1])
axes[0].set_title("gaussianBlur_img_0")
axes[1].imshow(gaussianBlur_img_3[:,:,::-1])
axes[1].set_title("gaussianBlur_img_3")
plt.show()
fig,axes = plt.subplots(nrows=1, ncols=2, figsize=(16,12), dpi=400)
axes[0].imshow(medianBlur_img_3[:,:,::-1])
axes[0].set_title("medianBlur_img_3")
axes[1].imshow(medianBlur_img_5[:,:,::-1])
axes[1].set_title("medianBlur_img_5")
plt.show()

The output is:

 

  It can be seen that in general, median filtering is better than Gaussian filtering in removing salt and pepper noise. Looking at the Gaussian filter alone, \sigmathe larger the value, the more obvious the smoothness of the image. Just looking at the median filter, increasing the window size will also increase the effect of image smoothing.

Guess you like

Origin blog.csdn.net/panpan_jiang1/article/details/130953461