均值模糊
均值模糊是一种最常见的模糊方法,它通过将像素值替换为周围像素的平均值来减少图像的细节。对于每个像素,我们通过取卷积核中像素的平均值来替换其值,例如3x3的卷积核为:
1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9
代码示例:
import cv2
img = cv2.imread('image.jpg')
# 模糊
blur = cv2.blur(img, (3, 3))
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
高斯模糊
高斯模糊是另一种常见的模糊方法,它基于高斯核对图像进行加权平均,以消除图像中的噪声。高斯核比均值核更高效,因为它根据像素距离分配不同的权重。
代码示例:
import cv2
img = cv2.imread('image.jpg')
# 高斯模糊
blur = cv2.GaussianBlur(img, (3, 3), 0)
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
中值模糊
中值模糊是一种非线性滤波方法,用于减少图像上的噪声。它将每个像素的值替换为其邻域像素值的中值。与均