2 找图像连通域_每日习题-图像处理-腐蚀、膨胀(2020.4.23)

题目地址:gzr2017/ImageProcessing100Wen

# 第47题 膨胀
# 对经过大津二值化处理后的图像进行膨胀处理
def dilate(image):
    H, W = image.shape
    k = ([0, 1, 0],
        [1, 0, 1],
        [0, 1, 0])
    result = np.zeros((H+2,W+2)).astype(np.uint8)
    result[1:H+1, 1:W+1] = image
    mid = result.copy()
    for h in range(1, H+1):
        for w in range(1, W+1):
            near_max = np.max(k * mid[h-1:h+2, w-1:w+2])
            if near_max == 255:
                result[h, w] = 255
    result = result[1:H+1, 1:W+1].astype(np.uint8)
    return result

# 第48题 膨胀
# 对经过大津二值化处理后的图像进行膨胀处理
def erode(image):
    H, W = image.shape
    k = ([0, 1, 0],
        [1, 0, 1],
        [0, 1, 0])
    result = np.zeros((H+2,W+2)).astype(np.uint8)
    result[1:H+1, 1:W+1] = image
    mid = result.copy()
    for h in range(1, H+1):
        for w in range(1, W+1):
            near_sum = np.sum(k * mid[h-1:h+2, w-1:w+2])
            if near_sum < 255*4:
                result[h, w] = 0
    result = result[1:H+1, 1:W+1].astype(np.uint8)
    return result

今天完成了第47与第48题,学习了用python实现腐蚀与膨胀操作的方法,稍微提醒一下,腐蚀与膨胀都是针对白色连通域而言的,而非黑色连通域

如有任何问题或错误,欢迎在评论区讨论和指正

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值