# -*- coding: utf-8 -*- import datetime import cv2 from cv2 import waitKey import getopt import sys def check_frame(frame): #image = frame[176:234,280:426] image = frame[180:230,250:370] #frame[176:234,280:426] #cv2.imshow('src',image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6)) counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel) image_h = image.shape[0] contours, hierarchy = cv2.findContours(counter,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) for c in contours: x, y, w, h = cv2.boundingRect(c) if image_h*0.98 > h > image_h * 0.3: #print((x, y, w, h)) cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.imshow('frame',image) waitKey(0) # 程序入口 if __name__ == '__main__': check_frame(cv2.imread(sys.argv[1]))