继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

opencv的目标跟踪算法

慕虎7371278
关注TA
已关注
手记 1300
粉丝 202
获赞 876

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

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


webp

trace.png

Adrian Rosebrock的OpenCV Object Tracking原文

webp

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 VideoStreamfrom imutils.video import FPSimport argparseimport imutilsimport timeimport 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 = Noneif 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 = Nonewhile 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"):        breakif not args.get("video", False):
    vs.stop()else:
    vs.release()

cv2.destroyAllWindows()

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


webp

opencv_object_tracking.gif

webp

opencv_object_tracking_selection.gif



作者:gaoshine
链接:https://www.jianshu.com/p/8830fe5b51a6


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP