5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺

import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
%matplotlib inline 

# 读取
img=cv2.imread('lena.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 图像二值化
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)

# 图像腐蚀操作
# 首先设置卷积核大小,3x3,值置1,格式为numpy中的整数uint8,0到255.
kernel = np.ones((3,3),np.uint8) 
# 腐蚀操作,可以设置迭代次数,改动iterations看结果,0为不腐蚀
erosion = cv2.erode(thresh1,kernel,iterations = 0)

cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 集合对比
kernel = np.ones((3,3),np.uint8)
erosion_0 = cv2.erode(thresh1,kernel,iterations = 0)
erosion_1 = cv2.erode(thresh1,kernel,iterations = 1)
erosion_2 = cv2.erode(thresh1,kernel,iterations = 2)
erosion_3 = cv2.erode(thresh1,kernel,iterations = 3)
erosion_4 = cv2.erode(thresh1,kernel,iterations = 100)
res = np.hstack((erosion_0,erosion_1,erosion_2,erosion_3,erosion_4))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

0

1

2

 3

 100,100轮,腐蚀没了

 对比

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值