当前位置:   article > 正文

Python图像处理——基于OpenCV的目标跟踪实现_python 单目标跟踪

python 单目标跟踪


一、单目标跟踪

1. 跟踪器建立

# 创建一个跟踪器,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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3. 初始化跟踪器

    # 初始化跟踪器
    ok = tracker.init(firstFrame, box)
    # 按帧读取视频
    while cap:
        ret, frame = cap.read()
        if not ret:
            print('read video error!')
            break
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4. 随视频更新

        # 计时器
        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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

二、多目标跟踪

1. 跟踪器建立

注意与单目标跟踪代码的区别!
这里我的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

  • 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

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]
    # print(frame.shape[:2])
    frame_resize = cv2.resize(frame, (int(w / 4), int(h / 4)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(2) 在第一帧中选择目标ROI

    # 在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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(3) 初始化多目标跟踪器

    tracker = cv2.legacy.MultiTracker_create()
    for box in boxs:
        tracker.add(createTypeTracker('BOOSTING'), frame_resize, box)  
  • 1
  • 2
  • 3

(4) 按帧更新跟踪器

    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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

三、总结

以上是本文的全部内容,主要介绍了基于OpenCV-Python自带算法实现目标跟踪的方法。后续会更新基于卡曼滤波和深度学习等算法的目标跟踪代码示例。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/78523
推荐阅读
相关标签
  

闽ICP备14008679号