赞
踩
# 创建一个跟踪器,algorithm: KCF、CSRT、DaSiamRPN、GOTURM、MIL 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
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)
# 初始化跟踪器
ok = tracker.init(firstFrame, box)
# 按帧读取视频
while cap:
ret, frame = cap.read()
if not ret:
print('read video error!')
break
# 计时器
timer = cv2.getTickCount()
ok, box = tracker.update(frame)
# print(box)
# box=(x,y,h,w) 为一个四元素元组,前两个为矩形的左上角顶点坐标,后两个为矩形的尺寸
# 计算帧率
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))
注意与单目标跟踪代码的区别!
这里我的OpenCV版本是4.5.5,多目标跟踪模块在cv2.legacy中。
# 创建一个跟踪器,algorithm: KCF、CSRT、DaSiamRPN、GOTURM、MIL 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
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]
# print(frame.shape[:2])
frame_resize = cv2.resize(frame, (int(w / 4), int(h / 4)))
# 在C + + 版本中,selectROI允许您获取多个边界框,但在Python版本中,它只返回一个边界框。
boxs = []
for i in range(3):
boxs.append(cv2.selectROI('select ROI', frame_resize))
# cv2.rectangle(frame,p1,p2,(0,0,255),2)
# print(boxs)
tracker = cv2.legacy.MultiTracker_create()
for box in boxs:
tracker.add(createTypeTracker('BOOSTING'), frame_resize, box)
while success:
ret, frame = cap.read()
# 原图像为4K分辨率,这里降采样适应电脑屏幕,视情况而定
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自带算法实现目标跟踪的方法。后续会更新基于卡曼滤波和深度学习等算法的目标跟踪代码示例。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。