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,调试循环次数,次数为100时,完全膨胀,全白
kernel = np.ones((3,3),np.uint8)
dilate_0 = cv2.dilate(thresh1,kernel,iterations = 0)
dilate_1 = cv2.dilate(thresh1,kernel,iterations = 1)
dilate_2 = cv2.dilate(thresh1,kernel,iterations = 2)
dilate_3 = cv2.dilate(thresh1,kernel,iterations = 3)
dilate_4 = cv2.dilate(thresh1,kernel,iterations = 4)
dilate_5 = cv2.dilate(thresh1,kernel,iterations = 100)
res = np.hstack((dilate_0,dilate_1,dilate_2,dilate_3,dilate_4,dilate_5))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
6.opencv-python;cv2库;形态学;图像膨胀
于 2022-04-25 17:22:22 首次发布