Opencv image blur and smooth (meitu noise reduction)

Averaging average blur smoothing

Calculate the average value of all pixels in the area covered by the convolution box to get the result of the convolution
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

image = imread('image.jpg')
show(image)

Insert picture description here

#设置3个不同大小的卷积窗口
kernelsizes = [(3,3),(9,9),(15,15)]
#定义一张画布
plt.figure(figsize=(15,15))
for i,kernel in enumerate(kernelsizes):
    #设置子图,母图1行3列  
    plt.subplot(1,3,i+1)
    # 平均平滑
    blur = cv2.blur(image, kernel)
    # 不显示坐标
    plt.axis('off')
    # 设置子图标题
    plt.title('Blurred'+str(kernel))
    #显示平滑后的子图
    plt.imshow(blur)
plt.show()

Insert picture description here

Gaussian Blur Smooth

Now replace the convolution kernel with a Gaussian kernel (in simple terms, the box remains unchanged, the original value of each box is
equal, and the value inside is now in accordance with the Gaussian distribution, the center of the box has the largest value, and the rest The box
decreases according to the distance from the center element to form a Gaussian hill bag. The original average is now a
weighted average, and the weight is the value in the box)

kernelsizes = [(3,3),(9,9),(15,15)]
plt.figure(figsize=(15,15))
for i,kernel in enumerate(kernelsizes):
    plt.subplot(1,3,i+1)
    # 平均平滑
    blur = cv2.GaussianBlur(image, kernel, 0)  #0代表卷机核标准差为0
    # 不显示坐标
    plt.axis('off')
    # 设置标题
    plt.title('Blurred'+str(kernel))
    plt.imshow(blur)
plt.show()

Insert picture description here

Median median blur

As the name implies, the median value of the pixel corresponding to the convolution box is used to replace the value of the center pixel.

image = imread('image2.jpg')
show(image)

Insert picture description here

plt.figure(figsize=(15,15))
for i,kernel in enumerate((3,9,15)):
    plt.subplot(1,3,i+1)
    # 平均平滑
    blur = cv2.medianBlur(image, kernel)
    # 不显示坐标
    plt.axis('off')
    # 设置标题
    plt.title('Blurred'+str(kernel))
    plt.imshow(blur)
plt.show()

Insert picture description here

Bilateral bilateral filtering

It can effectively remove noise while keeping the boundary clear. We already know that the Gaussian filter is to find the Gaussian weighted average of the pixels in the vicinity of the center point. This Gaussian filter only considers the spatial relationship between pixels, but does not consider the relationship between pixel values ​​(pixel similarity). So this method does not consider whether a pixel is located on the boundary. Therefore, the boundary will not be blurred, and this is not what we want.

Bilateral filtering uses both spatial Gaussian weights and gray value similarity Gaussian weights. The spatial Gaussian function ensures that only the pixels in the neighboring area have an effect on the center point, and the gray value similarity Gaussian function ensures that only the gray value of the center pixel is similar to that will be used for the blur operation. So this method will ensure that the border will not be blurred, because the gray value changes at the border are relatively large.

image = imread('wood.jpg')
show(image)

Insert picture description here

params = [(11,21,7),(11,41,21),(15,75,75)]
plt.figure(figsize=(15,15))
# 邻域直径,灰度值相似性高斯函数标准差,空间高斯函数标准差
for i,(diameter,sigmaColor,sigmaSpace) in enumerate(params):
    plt.subplot(1,3,i+1)
    # 平均平滑
    blur = cv2.bilateralFilter(image, diameter,sigmaColor,sigmaSpace)
    # 不显示坐标
    plt.axis('off')
    # 设置标题
    plt.title('Blurred'+str((diameter,sigmaColor,sigmaSpace)))
    plt.imshow(blur)
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/cyj5201314/article/details/114992017