分隔边框

时间:2018-06-25 13:55:05

标签: python opencv contour bounding-box background-subtraction

在此问题中,我们试图实时检测WEBCAM视频中的人。该代码对于1个人来说可以正常工作,但是当一个以上的人输入时,该代码将失败。这是代码:-

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

kernel = np.ones((5,5), np.uint8)

background = None
while True:
    ret,frame = cap.read()
    gray = frame.copy()
    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (11,11), 0)

    if background is None:
        background = gray
        continue

    delta = cv2.absdiff(background, gray)
    thresh = cv2.threshold(delta, 5, 255, 
    cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

    thresh = cv2.dilate(thresh, kernel, iterations=2)
    _,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 
    cv2.CHAIN_APPROX_SIMPLE)

    if(len(contours)==0):
        continue

    #areas = [cv2.contourArea(c) for c in contours]
    #max_index = np.argmax(areas)
    #cnt=contours[max_index]

    #(x,y,w,h) = cv2.boundingRect(cnt)

    #if(1.0*(w*h)/(640*480)<0.75):
        #cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 3)
        #print("Area: ",w*h)

    for i in range(len(contours)):
        (x,y,w,h) = cv2.boundingRect(contours[i])
        if(w*h<=90000):
            cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 5)

    #cv2.imshow('thresh', thresh)

    cv2.imshow('frame', frame)
    if cv2.waitKey(1)==27:
        break

cap.release()
cv2.destroyAllWindows()

我认为问题在于代码无法区分所检测到的不同人员的不同轮廓,但是我可能不是唯一的原因。有人可以帮我吗?

0 个答案:

没有答案
相关问题