import numpy as np
import cv2
blank_image = np.zeros((40,40,3), np.uint8)
blank_image.fill(255)
#cv2.imshow('i', blank_image)
#cv2.waitKey(0)
im = cv2.imread('img.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[4]
cnts = cv2.drawContours(im,[cnt],0,(255,0,0), -1)
cv2.imshow('i', im)
cv2.waitKey(0)
for a in cnt:
print(a) #this contour is a 3D numpy array
源图像:
我使用此代码:
1创建一个40x40像素的白色画布
2使用Opencv函数findContours找到了数字的轮廓(在本例中是5)。在