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

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

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轮,腐蚀没了

 对比

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/733664.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-27
下一篇2022-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存