OpenCV中的目标跟踪算法

软硬件环境

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

跟踪算法

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

[外链图片转存失败(img-3dCqPOd0-1567911581005)(https://code.xugaoxiang.com/xugaoxiang/blog/raw/master/images/ai/opencv/tracker_cv.jpeg)]

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

实例代码


# -*- coding: utf-8 -*-
# @Time    : 18-12-19 下午8:08
# @Author  : xugaoxiang
# @Email   : [email protected]
# @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()

然后执行脚步

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

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

你可能感兴趣的:(人工智能)