美文网首页OpenCVOpenCvopencv
opencv的目标跟踪算法

opencv的目标跟踪算法

作者: gaoshine | 来源:发表于2018-08-04 16:32 被阅读142次

    opencv的目标跟踪算法(OpenCV Object Tracking)

    今日拜读Adrian Rosebrock的大作,看到了有关OpenCV Object Tracking的算法介绍,写了代码跑了一下,果然很神奇!,特别做了笔记学习一下,分享给大家.


    trace.png

    Adrian Rosebrock的OpenCV Object Tracking原文

    opencv_object_tracking.gif

    这篇文章所讲述的目标跟踪是采用opencv的算法实现的,并非当下流行的深度学习的object detect.
    经测试,效果还不错,效率也很高,在我的老款mac air上跑起来也非常流畅,令我印象深刻.

    其实目标追踪算法的确很多,目前最新的opencv(3.4.2)上就有8种object tracking算法.

    1. BOOSTING Tracker: Based on the same algorithm used to power the machine learning behind Haar cascades (AdaBoost), but like Haar cascades, is over a decade old. This tracker is slow and doesn’t work very well. Interesting only for legacy reasons and comparing other algorithms. (minimum OpenCV 3.0.0)
    2. MIL Tracker: Better accuracy than BOOSTING tracker but does a poor job of reporting failure. (minimum OpenCV 3.0.0)
    3. KCF Tracker: Kernelized Correlation Filters. Faster than BOOSTING and MIL. Similar to MIL and KCF, does not handle full occlusion well. (minimum OpenCV 3.1.0)
    4. CSRT Tracker: Discriminative Correlation Filter (with Channel and Spatial Reliability). Tends to be more accurate than KCF but slightly slower. (minimum OpenCV 3.4.2)
    5. MedianFlow Tracker: Does a nice job reporting failures; however, if there is too large of a jump in motion, such as fast moving objects, or objects that change quickly in their appearance, the model will fail. (minimum OpenCV 3.0.0)
    6. TLD Tracker: I’m not sure if there is a problem with the OpenCV implementation of the TLD tracker or the actual algorithm itself, but the TLD tracker was incredibly prone to false-positives. I do not recommend using this OpenCV object tracker. (minimum OpenCV 3.0.0)
    7. MOSSE Tracker: Very, very fast. Not as accurate as CSRT or KCF but a good choice if you need pure speed. (minimum OpenCV 3.4.1)
    8. GOTURN Tracker: The only deep learning-based object detector included in OpenCV. It requires additional model files to run (will not be covered in this post). My initial experiments showed it was a bit of a pain to use even though it reportedly handles viewing changes well (my initial experiments didn’t confirm this though). I’ll try to cover it in a future post, but in the meantime, take a look at Satya’s writeup. (minimum OpenCV 3.2.0)

    其中比较出色的就是 CSRT和KCF.(注意CSRT只有最新版才支持哦)

    文章建议如下:

    • 使用CSRT: 当需要更高的准确率的时候采用,这个更耗时一点.
    • 使用KCF: 这个更加高速(帧),单准确率差一点.
    • 使用MOSSE: 只最求高速的话,就选它吧.

    完整代码如下:

    from imutils.video import VideoStream
    from imutils.video import FPS
    import argparse
    import imutils
    import time
    import cv2
    
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", type=str,
                    help="path to input video file")
    ap.add_argument("-t", "--tracker", type=str, default="kcf",
                    help="OpenCV object tracker type")
    args = vars(ap.parse_args())
    
    (major, minor) = cv2.__version__.split(".")[:2]
    
    if int(major) == 3 and int(minor) < 3:
        tracker = cv2.Tracker_create(args["tracker"].upper())
    else:
        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
        }
    
    
        tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
    
    initBB = None
    
    if not args.get("video", False):
        print("[INFO] starting video stream...")
        vs = VideoStream(src=0).start()
        time.sleep(1.0)
    else:
        vs = cv2.VideoCapture(args["video"])
    
    fps = None
    
    while True:
        frame = vs.read()
        frame = frame[1] if args.get("video", False) else frame
        if frame is None:
            break
        frame = imutils.resize(frame, width=800)
        (H, W) = frame.shape[:2]
    
        if initBB is not None:
            (success, box) = tracker.update(frame)
            if success:
                (x, y, w, h) = [int(v) for v in box]
                cv2.rectangle(frame, (x, y), (x + w, y + h),
                              (0, 255, 0), 2)
            fps.update()
            fps.stop()
    
            info = [
                ("Tracker", args["tracker"]),
                ("Success", "Yes" if success else "No"),
                ("FPS", "{:.2f}".format(fps.fps())),
            ]
    
            for (i, (k, v)) in enumerate(info):
                text = "{}: {}".format(k, v)
                cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF
    
        if key == ord("s"):
    \        initBB = cv2.selectROI("Frame", frame, fromCenter=False,
                                   showCrosshair=True)
           tracker.init(frame, initBB)
            fps = FPS().start()
    
            elif key == ord("q"):
            break
    if not args.get("video", False):
        vs.stop()
    
    else:
        vs.release()
    
    cv2.destroyAllWindows()
    
    

    使用了例子提供的视频文件,效果如下:


    opencv_object_tracking.gif opencv_object_tracking_selection.gif

    简书上只能发布gif,其实有关赛马,短跑的视频跟踪算法做起来还是很精彩的,大家可以尝试一下.

    相关文章

      网友评论

      • 喏喏心要坚持:您好,我的报错如下:
        "csrt": cv2.TrackerCSRT_create,
        AttributeError: module 'cv2.cv2' has no attribute 'TrackerCSRT_create'
        您能帮我看看是什么原因吗?我的opencv-python3.4.2.17 版本,感觉不应该是cv版本的问题。先谢谢啦!
        a0f22f668146:@喏喏心要坚持 你好,我也是这个问题,请问怎么解决 的
        喏喏心要坚持:@gaoshine 已经解决了,谢谢。
        gaoshine:@喏喏心要坚持 你用KCF试试 先别使用CSRT 还报错吗?

      本文标题:opencv的目标跟踪算法

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