代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2
import numpy as np
img = cv2.imread("a28.png")
ret,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV,0)
cv2.imshow("thresh",thresh)
cv2.waitKey(500)
# cv2.destroyAllWindows()
#腐蚀
kernel = np.ones((3,3),dtype=np.uint8)
erosion = cv2.erode(thresh,kernel,iterations=1) #literations 为迭代次数
# res = np.hstack(erosion)
cv2.imshow('erosion',erosion)
cv2.waitKey(0)
# cv2.destroyAllWindows()
#膨胀
dige_dilate = cv2.dilate(thresh,kernel,iterations=1)
cv2.imshow('dilate',dige_dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
image-20230304192050442

image-20230304192105659