foreword
This article introduces the code implementation of mean filtering, median filtering, and Gaussian filtering and their comparison of denoising effects in salt and pepper noise and Gaussian noise images
1. Mean filtering
Mean filtering refers to giving a template to the target pixel (a pixel with a specific coordinate) on the image. The template includes the target pixel itself and its surrounding adjacent pixels, and then replaces the original pixel value with the average value of all pixels in the template . .
Mean filtering is realized by convolution, and the common convolution kernel sizes are 3×3, 5×5, 7×7, etc. The following is a 3×3 convolution kernel, because the average value of all pixels is used to replace the original pixels value, so it can be considered that every weight on the template is the same, and here is a 3×3 convolution kernel, so they are all 1/9. The specific operation is to multiply the two matrices and then add them.
code show as below:
import cv2
# 读图
old_img = cv2.imread("lena.tif")
# 均值滤波
new_img = cv2.blur(old_img, (3, 3)) # 后面的卷积核大小可以自己定义,如(5, 5)
# 结果对比
cv2.imshow("old_img", old_img)
cv2.imshow("new_img", new_img)
cv2.waitKey(0)
The results are as follows, the left is the old image, and the right is the new image (the image after mean filtering), and the difference will be more obvious with 5×5.
2. Median filtering
The median filter is to sort the numbers within the coverage of the convolution kernel from small to large and then take the median to replace the value of the center position of the convolution kernel in the original image. As shown in the figure below, the size of the convolution kernel is 3×3, and the original image The value of the central position of the convolution kernel is originally 2, and the 9 numbers in the convolution kernel are sorted to get: 2, 5, 6, 7, 16 , 25, 28, 44, 80. So the 2 in the center is replaced with 16.
It should be noted that the median filter only needs to reach the size of the convolution kernel. As for the value of each position in the convolution kernel, it is not necessary to know, such as a 3×3 convolution kernel. When traversing to a certain coordinate of the original image, only It is necessary to sort the numbers in the range of 3×3 centered on it, and then take the median.
code show as below:
import cv2
# 读图
old_img = cv2.imread("lena.tif")
# 中值滤波
medianBlur_img = cv2.medianBlur(old_img, 3)
cv2.imshow("old_img", old_img)
cv2.imshow("medianBlur_img", medianBlur_img)
cv2.waitKey(0)
3. Gaussian filtering
In order to overcome the drawbacks of the simple local average method (image blur), many local smoothing algorithms that preserve edges and details have been proposed. Their starting points all focus on how to choose the size, shape and direction of the neighborhood, the parameters plus the average, and the weight coefficients of each store in the neighborhood.
Image Gaussian smoothing is also a method for smoothing images based on the idea of neighborhood averaging. In image Gaussian smoothing, when averaging images, pixels at different positions are given different weights. Gaussian smoothing is different from simple smoothing. It gives different weights to pixels at different positions when averaging pixels in the neighborhood. The Gaussian templates in the 3×3 and 5×5 fields shown in the figure below. Among them, 16 is because all the numbers in the 3×3 convolution kernel add up to 16, and 273 is the same.
The code is as follows:
import cv2
# 读图
old_img = cv2.imread("lena.tif")
# 高斯滤波
GaussianBlur_img = cv2.GaussianBlur(old_img, (3,3), 0) # 第一个参数:原图像;第二个参数:卷积核大小;第三个参数:X方向上的高斯核标准偏差。
# 显示
cv2.imshow("old_img", old_img)
cv2.imshow("GaussianBlur_img", GaussianBlur_img)
cv2.waitKey(0)
4. Comparison of the effect of removing salt and pepper noise
The salt and pepper noise image is as follows:
The effects of the three filters are as follows, from left to right are mean filter, median filter, and Gaussian filter.
So for salt and pepper noise , the median filter is a good choice
5. Comparison of the effect of removing Gaussian noise
The Gaussian noise image is as follows:
The three filter effects are as follows, from left to right are mean filter, median filter, and Gauss filter.
I went, but only a little bit, and the effect feels similar .
looking forward to criticism