opencv实现多目标跟踪

目录

opencv实现多目标跟踪代码


多目标跟踪和单目标差不多,只是实例化的追踪器不同。

tracker = cv2.legacy.MultiTracker_create() #多目标跟踪
bbox1 = cv2.selectROI('tracking', image)
bbox2 = cv2.selectROI('tracking', image)
bbox3 = cv2.selectROI('tracking', image)
ok = tracker.add(cv2.legacy.TrackerKCF_create(),image,bbox1)
ok = tracker.add(cv2.legacy.TrackerKCF_create(),image, bbox2)
ok = tracker.add(cv2.legacy.TrackerKCF_create(),image, bbox3)

opencv实现多目标跟踪代码

这个有缺陷,就是我必须首先给定视频中有几个人的存在,给出的bbox数值一旦确定,当视频中人数增加时是无法识别的。

import numpy as np
import cv2
import sys
'''
if len(sys.argv) != 2:
 print('Input video name is missing')
 exit()
'''
print('Select multiple tracking targets')
cv2.namedWindow("tracking")
camera = cv2.VideoCapture(r"E:\Python-Code/videodataset/enn.mp4")
#camera = cv2.VideoCapture(0)
tracker = cv2.legacy.MultiTracker_create() #多目标跟踪
# a= cv2.Tracker_c
init_once = False
ok, image=camera.read()
if not ok:
 print('Failed to read video')
 exit()
bbox1 = cv2.selectROI('tracking', image)
bbox2 = cv2.selectROI('tracking', image)
bbox3 = cv2.selectROI('tracking', image)
bbox4 = cv2.selectROI('tracking', image)
while camera.isOpened():
 ok, image=camera.read()
 if not ok:
  print ('no image to read')
  break
 if not init_once:
  ok = tracker.add(cv2.legacy.TrackerKCF_create(),image,bbox1)
  ok = tracker.add(cv2.legacy.TrackerKCF_create(),image, bbox2)
  ok = tracker.add(cv2.legacy.TrackerKCF_create(),image, bbox3)
  ok = tracker.add(cv2.legacy.TrackerKCF_create(), image, bbox4)
  init_once = True
 ok, boxes = tracker.update(image)
 for newbox in boxes:
  p1 = (int(newbox[0]), int(newbox[1]))
  p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
  cv2.rectangle(image, p1, p2, (0,0,255))
 cv2.imshow('tracking', image)
 k = cv2.waitKey(1)
 if k == 27 : break # esc pressed

你可能感兴趣的:(目标跟踪,目标跟踪,opencv,计算机视觉)