wangzhibo
2022-03-23 8afe7455067996bf8bca087418425c2bb7d6d492
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- coding: utf-8 -*-
import datetime
import cv2
from cv2 import waitKey
import getopt
import sys
from _image_clip_setting import image_clip
 
 
def check_frame(frame):
    image = frame[image_clip[0]:image_clip[1], image_clip[2]:image_clip[3]]
    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)
    contours, hierarchy = cv2.findContours(counter,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contours_count = 0
    image_h = image.shape[0]
    for c in contours:
        x, y, w, h = cv2.boundingRect(c)
        if hierarchy[0][contours_count][3] ==0 and h > image_h * 0.3:
        #if image_h*0.98 > h > image_h * 0.6:
            print((x, y, w, h))
            cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
        contours_count += 1
    cv2.imshow('frame',image)
    waitKey(0)
 
# 程序入口
if __name__ == '__main__':
    check_frame(cv2.imread(sys.argv[1]))