数字图像处理——目标检测

© Fu Xianjun. All Rights Reserved


目标跟踪是计算机视觉领域的一个重要问题,目前广泛应用在体育赛事转播、安防监控和无人机、无人车、机器人等领域。

目标跟踪有哪些研究领域呢:

数字图像处理——目标检测_第1张图片

扩展包:

pip install opencv-contrib-python==3.4.3.18

目标追踪方法:

    "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

代码:

import cv2
import numpy as np

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
}

# 实例化OpenCV的tracker
trackers = cv2.MultiTracker_create()
vs = cv2.VideoCapture("1.mp4")
while True:
    # 取当前帧
    frame = vs.read()
    # (true, data)
    frame = frame[1]
    # 到头了就结束
    if frame is None:
        break

    # resize每一帧
    (h, w) = frame.shape[:2]
    width=600
    r = width / float(w)
    dim = (width, int(h * r))
    frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)

    # 追踪结果
    (success, boxes) = trackers.update(frame)

    # 绘制区域
    for box in boxes:
        (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) & 0xFF
    if key == ord("s"):
        # 选择一个区域,按s
        box = cv2.selectROI("Frame", frame, fromCenter=False,
            showCrosshair=True)

        # 创建一个新的追踪器
        tracker = OPENCV_OBJECT_TRACKERS["csrt"]()
        trackers.add(tracker, frame, box)

    # 退出
    elif key == 27:
        break
vs.release()
cv2.destroyAllWindows()    

效果:

数字图像处理——目标检测_第2张图片

你可能感兴趣的:(opencv,opencv,目标跟踪)