你好每一个我都有一些问题。我想要计算车辆,一步一步地计算
这是我的代码
import cv2
backsub = cv2.BackgroundSubtractorMOG()
capture = cv2.VideoCapture("C:\Python27\code\car.avi")
best_id=0
i = 0
if capture:
while True:
ret, frame = capture.read()
if ret:
fgmask = backsub.apply(frame, None, 0.01)
contours, hierarchy = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
try: hierarchy = hierarchy[0]
except: hierarchy = []
for contour, hier in zip(contours, hierarchy):
(x,y,w,h) = cv2.boundingRect(contour)
if w > 20 and h > 20:
# figure out id
best_id+=1
cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2)
cv2.putText(frame, str(best_id), (x,y-5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (255, 0, 0), 2)
print(best_id)
cv2.imshow("Track", frame)
cv2.imshow("background sub", fgmask)
key = cv2.waitKey(10)
if key == ord('q'):
break
答案 0 :(得分:0)
您可以在图像上绘制一条线,每当边界框的质心越过该线时,您可以将计数增加1。