一、单目标跟踪
1. 跟踪器建立
tracker_types = ['MIL', 'KCF', 'CSRT', 'DaSiamRPN', 'GOTURM']
def createTypeTracker(type):
if type == tracker_types[0]:
tracker = cv2.TrackerMIL_create()
elif type == tracker_types[1]:
tracker = cv2.TrackerKCF_create()
elif type == tracker_types[2]:
tracker = cv2.TrackerCSRT_create()
elif type == tracker_types[3]:
tracker = cv2.TrackerDaSiamRPN_create()
elif type == tracker_types[4]:
tracker = cv2.TrackerGOTURN_create()
else:
tracker = None
return tracker
2. 读取视频的第一帧,选择跟踪的目标
videoPth = 'JetFlyby.mp4'
if __name__ == '__main__':
tracker_type = 'MIL'
tracker = createTypeTracker(tracker_type)
cap = cv2.VideoCapture(videoPth)
ret, firstFrame = cap.read()
box = cv2.selectROI('select ROI @1st Frame', firstFrame, fromCenter=True)
print(box)
3. 初始化跟踪器
ok = tracker.init(firstFrame, box)
while cap:
ret, frame = cap.read()
if not ret:
print('read video error!')
break
4. 随视频更新
timer = cv2.getTickCount()
ok, box = tracker.update(frame)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
if ok:
pt1 = (int(box[0]), int(box[1]))
pt2 = (int(box[0] + box[2]), int(box[1] + box[3]))
cv2.rectangle(frame, pt1, pt2, (0, 0, 255), 2, 1)
else:
cv2.putText(frame, 'track failed!', (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0))
二、多目标跟踪
1. 跟踪器建立
注意与单目标跟踪代码的区别! 这里我的OpenCV版本是4.5.5,多目标跟踪模块在cv2.legacy中。
trackerTypes = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
def createTypeTracker(trackerType):
if trackerType == trackerTypes[0]:
tracker = cv2.legacy.TrackerBoosting_create()
elif trackerType == trackerTypes[1]:
tracker = cv2.legacy.TrackerMIL_create()
elif trackerType == trackerTypes[2]:
tracker = cv2.legacy.TrackerKCF_create()
elif trackerType == trackerTypes[3]:
tracker = cv2.legacy.TrackerTLD_create()
elif trackerType == trackerTypes[4]:
tracker = cv2.legacy.TrackerMedianFlow_create()
elif trackerType == trackerTypes[5]:
tracker = cv2.TrackerGOTURN_create()
elif trackerType == trackerTypes[6]:
tracker = cv2.legacy.TrackerMOSSE_create()
elif trackerType == trackerTypes[7]:
tracker = cv2.legacy.TrackerCSRT_create()
else:
tracker = None
return tracker
2. 多目标跟踪Python代码示例
(1) 打开视频或摄像机,获取第一帧
pth = 'videoName.mp4'
cap = cv2.VideoCapture(pth)
success, frame = cap.read()
if not success:
print('opening video failed!')
sys.exit(1)
h, w = frame.shape[:2]
frame_resize = cv2.resize(frame, (int(w / 4), int(h / 4)))
(2) 在第一帧中选择目标ROI
boxs = []
for i in range(3):
boxs.append(cv2.selectROI('select ROI', frame_resize))
(3) 初始化多目标跟踪器
tracker = cv2.legacy.MultiTracker_create()
for box in boxs:
tracker.add(createTypeTracker('BOOSTING'), frame_resize, box)
(4) 按帧更新跟踪器
while success:
ret, frame = cap.read()
dsize_frame = cv2.resize(frame, (int(w / 4), int(h / 4)))
ok, boxs = tracker.update(dsize_frame)
if len(boxs) > 1:
for box in boxs:
pt1 = (int(box[0]), int(box[1]))
pt2 = (int(box[0] + box[2]), int(box[1] + box[3]))
cv2.rectangle(dsize_frame, pt1, pt2, (0, 0, 255), 2, 1)
else:
cv2.putText(dsize_frame, 'track failed!', (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0))
三、总结
以上是本文的全部内容,主要介绍了基于OpenCV-Python自带算法实现目标跟踪的方法。后续会更新基于卡曼滤波和深度学习等算法的目标跟踪代码示例。
|