美文网首页
图像处理实战-目标追踪

图像处理实战-目标追踪

作者: YvanYan | 来源:发表于2020-08-24 11:58 被阅读0次

完整代码:https://github.com/YvanYan/image_processing/tree/master/object_tracking


流程:
1.读取视频,调整视频尺寸
2.更新追踪器,绘制目标区域
3.根据需要创建新的追踪目标

目前OpenCV已经实现了的追踪算法:

OPENCV_OBJECT_TRACKERS = {
    "csrt": cv2.TrackerCSRT_create,
    "kcf": cv2.TrackerKCF_create,
    "boosting": cv2.TrackerBoosting_create,
    "mil": cv2.TrackerMIL_create,
    "tld": cv2.TrackerTLD_create,
    "medianflow": cv2.TrackerMedianFlow_create,
    "mosse": cv2.TrackerMOSSE_create
}

1.读取视频,调整视频尺寸

cap = cv2.VideoCapture('videos/soccer_01.mp4')
trackers = cv2.MultiTracker_create()

while (True):
    frame = cap.read()[1]  
    if frame is None:
        break

    (h, w) = frame.shape[0:2]
    width = 600
    ratio = width / float(w)
    height = int(h * ratio)
    frame = cv2.resize(frame, (height, width), interpolation=cv2.INTER_AREA)

通过MultiTracker_create初始化追踪器集合。

2.更新追踪器,绘制目标区域

    (success, boxs) = trackers.update(frame)

    for box in boxs:
        (x, y, w, h) = [int(v) for v in box]
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow('frame', frame)
    key = cv2.waitKey(100)

根据trackers.update返回的区域集,绘制不同的目标区域。

3.根据需要创建新的追踪目标

 box = cv2.selectROI("frame", frame, fromCenter=False,
                            showCrosshair=True)
        tracker = OPENCV_OBJECT_TRACKERS['kcf']()
        trackers.add(tracker, frame, box)

使用selectROI创造矩形,然后创建kcf追踪器,将新的追踪器添加到追踪器集合中。

相关文章

网友评论

      本文标题:图像处理实战-目标追踪

      本文链接:https://www.haomeiwen.com/subject/zjtnjktx.html