翻译:
平滑空间过滤器
邻域平均(平均滤波器)
- 最基本的滤波器,用于图像模糊/平滑和降噪
- 用(x,y)邻域内强度的平均值替换像素(x,y)处的强度
- 我们也可以使用加权平均,赋予邻近区域内的一些像素比其他像素更重要的位置,这样可以减少模糊效果
- 邻域平均模糊边缘
缺点:不能很好地保护图像的细节,在去噪过程中会破坏细节,导致图像模糊;
代码实现:
imgDog = cv2.imread("Dog.png")
row_Dog, col_Dog, channel_Dog = imgDog.shape
# 假设kernel的大小为5*5的矩阵,padding就是5//2 = 2
# padding与接下来的I[a:b,c:d]有关
kernel = np.ones((5,5))
out_Dog = np.zeros((row_Dog, col_Dog,channel_Dog))
for r in range(2, row_Dog - 2):
for c in range(2, col_Dog - 2):
for channel in range(channel_Dog):
out_Dog[r, c, channel] = np.sum(kernel * imgDog[r-2:r+3, c-2:c+3, channel]) // (5 ** 2)
cv2.imwrite("output_Dog.png", np.uint8(out_Dog))
前后对比: