/*
@author:Isadora
@time:2021/7/17
*/
轮廓检测
1 import cv2 2 import numpy as np 3 4 #生成200*200的黑色空白图像 5 img = np.zeros((200,200), dtype=np.uint8) 6 img[50:150, 50:150] = 255 7 8 #阈值分割 9 ret, thresh = cv2.threshold(img, 127, 255, 0) 10 11 #寻找轮廓函数 12 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 13 14 #颜色空间转换 15 color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) 16 17 #绘制轮廓 18 img = cv2.drawContours(color, contours, -1, (0,255,0), 2) 19 cv2.imshow("contours", color) 20 cv2.waitKey() 21 cv2.destroyAllWindows()