1. blur (mean filtering)
From the perspective of frequency domain, mean filtering is a low-pass filter, and high-frequency signals will be filtered out. Mean filtering can help eliminate sharp image noise, and achieve image smoothing, blurring and other functions. The ideal mean filter is to replace each pixel in the image with the average value calculated for each pixel and its surrounding pixels.
The mean filter is generally implemented by convolving the following template and image.
That is, take the current pixel as the center, calculate the sum of all gray values in the window, and take the average value as the new gray value of the center pixel.
Mean filtering includes average mean filtering and weighted mean filtering. respectively as follows:
On the left is the average mean filter, and on the right is the weighted mean filter.
The mean filter can blur the image to obtain a rough description of the image.
2. boxFilter (box filter)
The principles of box filtering and mean filtering are similar, because mean filtering is a normalized representation of box filtering. In OpenCV, the template used for box filtering is as follows:
From the template of box filtering, it can be seen that if α = 1, then it is box filtering without normalization; if α != 1, then normalization is performed operate.
Take the 5*5 convolution kernel as an example, if normalize == true, then it is mean filtering, the template is as follows:
if normalize != true, then it is to calculate the neighborhood pixel sum, not average, and the convolution kernel is as follows:
3. GaussianBlur (Gaussian filter)
Gaussian filtering is a linear smoothing filter, suitable for eliminating Gaussian noise (Gaussian noise refers to a type of noise whose probability density function obeys a Gaussian distribution (that is, a normal distribution)). Gaussian filtering is a process of weighted average of the entire image, and the value of each pixel is obtained by weighted average of itself and other pixel values in the neighborhood. The specific operation of Gaussian filtering is: use a template (or convolution, mask) to scan each pixel in the image, and use the weighted average gray value of the pixels in the neighborhood determined by the template to replace the value of the pixel in the center of the template.
One-dimensional Gaussian distribution
Usually we use the standard normal distribution when we use it. At this time
, taking the 3*3 template as an example, the convolution kernel of Gaussian filtering is given.
From the convolution kernel, it can be seen that the weights of each pixel are not all the same. It highlights the weight of the center point after pixel smoothing, and has a better smoothing effect than mean filtering.
medianBlur (median filter)
Median filtering is a typical nonlinear filtering. It is a nonlinear signal processing technology based on sorting statistics theory that can effectively suppress noise. The basic idea is to replace the pixel with the median value of the gray value of the neighborhood of the pixel. The gray value of the pixel makes the surrounding pixel values close to the real value so as to eliminate isolated noise points. This method can preserve the edge details of the image while removing the impulse noise and salt and pepper noise. These excellent characteristics are not available in linear filtering.
Median filtering will take the pixel values of the current pixel and its surrounding adjacent pixels (there are a total of odd pixels), sort these pixel values, and then use the pixel value in the middle position as the pixel value of the current pixel.
Set its neighborhood to a size of 3×3, and sort the pixel values of the pixels in its 3×3 neighborhood (both in ascending and descending order). After sorting in ascending order, the sequence value is: [66,78,90,91, 93,94,95,97,101]. In this sequence, the value at the center position (also called the center point or median point) is "93", so replace the original pixel value 78 with this value as the new pixel value of the current point. The median filtering effect is as follows:
code example
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("./lena-noisedimage.png",cv2.IMREAD_COLOR)
print(img.shape)
img = cv2.resize(img,[256,256])
blur = cv2.blur(img,(3,3))
boxfliter = cv2.boxFilter(img,-1,(3,3),normalize=True)
Gaussian = cv2.GaussianBlur(img,(3,3),1)
media = cv2.medianBlur(img,5)
#kernel = np.array([0,-1,0,-1,5,-1,0,-1,0])
#fliter2d = cv2.filter2D(img,-1,kernel)# 不同的效果由卷积核决定
#res = np.hstack((img,blur,boxfliter,Gaussian,media,fliter2d))
res = np.hstack((img,blur,boxfliter,Gaussian,media))
cv2.imshow("lena",res)
cv2.waitKey(0)
cv2.destroyAllWindows()
Reference link: https://cloud.tencent.com/developer/article/1601443