我使用混合高斯算法,而不是使用阈值清除阴影,然后使用轮廓找到白色物体。然后我在它们周围画了一个矩形。现在,我想给这些矩形加上数字,并希望数字跟踪矩形以计数人数。
如果您可以给我一些计数的信息,那么这就是我使用的代码。
import numpy as np
import cv2
import sys
video_path = 'video.avi'
cv2.ocl.setUseOpenCL(False)
version = cv2.__version__.split('.')[0]
print(version)
cap = cv2.VideoCapture(video_path)
if version == '2' :
fgbg = cv2.BackgroundSubtractorMOG2()
if version == '3':
fgbg = cv2.createBackgroundSubtractorMOG2()
while (cap.isOpened):
ret, frame = cap.read()
if ret==True:
fgmask = fgbg.apply(frame)
ret1,th1 = cv2.threshold (fgmask,150,200,cv2.THRESH_BINARY)
if version == '2' :
(contours, hierarchy) = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if version == '3' :
(im2, contours, hierarchy) = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if cv2.contourArea(c) < 500:
continue
(x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('foreground and background',th1)
cv2.imshow('rgb',frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()