所以我有可以跟踪多个对象的代码(如下所示),我将如何使其绘制出这些对象移动的路径。可以针对仅跟踪一个对象的实例量身定制答案。在此先感谢(忽略任何制表符错误,我建议将其制表符,因为堆栈溢出存在使整个文本成为代码的问题)
'from __future__ import print_function
import sys
import cv2
from random import randint
trackerTypes = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
def createTrackerByName(trackerType):
# Create a tracker based on tracker name
if trackerType == trackerTypes[0]:
tracker = cv2.TrackerBoosting_create()
elif trackerType == trackerTypes[1]:
tracker = cv2.TrackerMIL_create()
elif trackerType == trackerTypes[2]:
tracker = cv2.TrackerKCF_create()
elif trackerType == trackerTypes[3]:
tracker = cv2.TrackerTLD_create()
elif trackerType == trackerTypes[4]:
tracker = cv2.TrackerMedianFlow_create()
elif trackerType == trackerTypes[5]:
tracker = cv2.TrackerGOTURN_create()
elif trackerType == trackerTypes[6]:
tracker = cv2.TrackerMOSSE_create()
elif trackerType == trackerTypes[7]:
tracker = cv2.TrackerCSRT_create()
else:
tracker = None
print('Incorrect tracker name')
print('Available trackers are:')
for t in trackerTypes:
print(t)
return tracker
videopath = 'vide.mp4'
cap = cv2.VideoCapture(videopath)
success, frame = cap.read()
bboxes = []
colors = []
while True:
bbox = cv2.selectROI('MultTracker', frame)
bboxes.append(bbox)
colors.append(bbox)
print('press q to quit')
print('press any other letter to select next object')
k = cv2.waitKey(0) & 0xFF
if (k == 113):
break
if not success:
print('failed to read video')
sys.exot(1)
print('bound boxes {}'.format(bboxes))
trackerType = 'CSRT'
multiTracker = cv2.MultiTracker_create()
for bbox in bboxes:
multiTracker.add(createTrackerByName(trackerType), frame, bbox)
while cap.isOpened():
success, frame = cap.read()
if not success:
break
success, boxes = multiTracker.update(frame)
for i, newbox in enumerate(boxes):
p1 = (int(newbox[0]), int(newbox[1]))
p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
cv2.rectangle(frame, p1, p2, colors[i],2,1)
cv2.imshow('multitracker', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
答案 0 :(得分:0)
您可以按索引创建和排列并推入对象中心。 我不喜欢OpenCV中的MultiTracker API。这太原始了。