openCV实现轮廓检测

import cv2
import numpy as np
#轮廓检测
img = np.zeros((200,200), dtype=np.uint8)
img[50:150, 50:150] = 255
ret, thresh = cv2.threshold(img, 127, 255, 0)
#findContours会修改原图,最好使用img.copy()
#RETR_TREE会得到整体的层次结构,以此建立轮廓之间的关系
#cv2.RETR_EXTERNAL得到最外面的轮廓
image, contours, hierarchy = cv2.findContours(thresh,
                                              cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_SIMPLE)
color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img = cv2.drawContours(color, contours, -1, (255,0,255),2)
cv2.imshow("contours", color)
cv2.waitKey()
cv2.destroyAllWindows()

 

你可能感兴趣的:(openCV)