徐高翔的个人网站

OpenCV中的物体跟踪

2018-11-04

软硬件环境

  • ubuntu 18.04 64bit
  • anaconda3 with python 3.6.4
  • opencv 3.4.2

跟踪算法

opencv中总共有8种目标跟踪算法,分别是BOOSTINGMILKCFTLDMEDIANFLOWGOTURNCSRTMOSSE。每种算法对opencv的版本各有要求,需要注意。

track_cv_version

目前使用的较多的跟踪算法是KCFCSRT,前者速度很快,但准确率稍低;后者准确率较高不过速度较慢,在实际应用中,需要自行根据场景进行选择。

实例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

# -*- coding: utf-8 -*-
# @Time : 18-12-19 下午8:08
# @Author : xugaoxiang
# @Email : djstava@gmail.com
# @Website : http://www.xugaoxiang.com
# @File : tracker.py
# @Software: PyCharm

import sys
import cv2
import click


@click.command()
@click.option('--video', help = 'input video')
@click.option('--algorithm', help = 'tracker algorithm, BOOSTING、MIL、KCF、TLD、MEDIANFLOW、GOTURN、CSRT、MOSSE')
def main(video, algorithm):
'''

:param video: 待处理的视频文件
:param algorithm: 指定OpenCV中的跟踪算法
:return:
'''

major_ver, minor_ver, subminor_ver = (cv2.__version__).split('.')

# 根据opencv的不同版本,创建跟踪器
if int(minor_ver) < 3:
tracker = cv2.Tracker_create(algorithm)
else:
if algorithm == 'BOOSTING':
tracker = cv2.TrackerBoosting_create()
if algorithm == 'MIL':
tracker = cv2.TrackerMIL_create()
if algorithm == 'KCF':
tracker = cv2.TrackerKCF_create()
if algorithm == 'TLD':
tracker = cv2.TrackerTLD_create()
if algorithm == 'MEDIANFLOW':
tracker = cv2.TrackerMedianFlow_create()
if algorithm == 'GOTURN':
tracker = cv2.TrackerGOTURN_create()
if algorithm == "CSRT":
tracker = cv2.TrackerCSRT_create()
if algorithm == 'MOSSE':
tracker = cv2.TrackerMOSSE_create()

# 读取视频文件
video_cap = cv2.VideoCapture(video)

# 检查视频文件是否被正确打开
if not video_cap.isOpened():
print("Open video failed.")
sys.exit()

# 读取第一帧数据
ok, frame = video_cap.read()
if not ok:
print('Read video file failed.')
sys.exit()

# 手动选择关注的区域
bbox = cv2.selectROI(frame, False)

#
ok = tracker.init(frame, bbox)

while True:
# 读取下一帧数据
ok, frame = video_cap.read()
if not ok:
break

# 开始计时器
timer = cv2.getTickCount()

# 更新跟踪器
ok, bbox = tracker.update(frame)

# 计算fps
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)

# 画出 bounding box
if ok:

p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
else:
# Tracking failure
cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

# 显示
cv2.putText(frame, algorithm + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)

cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)

cv2.imshow("Tracking", frame)

# 接收到q键,退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break

if __name__ == '__main__':
main()

然后执行脚步

1
python tracker.py --video liuxiang_22s.mp4 --algorithm CSRT

其它算法使用,可以通过python tracker.py --help来查看,最后运行的效果,我丢到了youtube上了,https://youtu.be/Xl4PUT5vRDY

推荐文章(由hexo文章推荐插件驱动)

使用支付宝打赏
使用微信打赏

请博主喝咖啡!