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) #二值化
contours, hirerarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) #将原图转为"彩色图"(BGR三通道)
img = cv2.drawContours(color, contours, -1, (0, 255, 0), 2) #在彩色图上绘制绿色轮廓
cv2.imshow("thr", img)
cv2.waitKey()