python+opencv:均值滤波的手动实现

翻译:

平滑空间过滤器

邻域平均(平均滤波器)

  • 最基本的滤波器,用于图像模糊/平滑和降噪
  • 用(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))

前后对比:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值