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
5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺
于 2022-04-25 17:07:22 首次发布