OpenCv 3.1.0中的KCF跟踪器 - 没有输出,没有报告错误

时间:2016-08-09 14:26:57

标签: python opencv tracking

我按照

中的建议

https://github.com/opencv/opencv_contrib/issues/640

修复错误,但仍然没有得到任何东西。

基本上KCF没有初始化,也没有报告错误。如果有人设法让KCF在opencv中运行,那么很高兴知道。所有其他跟踪器工作正常。

1 个答案:

答案 0 :(得分:1)

尝试以下代码。我编辑了https://www.learnopencv.com/object-tracking-using-opencv-cpp-python/上给出的示例代码。首先选择要跟踪的对象周围的矩形,然后按Enter键。

import cv2
import sys

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

if __name__ == '__main__' :

# Set up tracker.
# Instead of MIL, you can also use

tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']
tracker_type = tracker_types[2]

if int(minor_ver) < 3:
    tracker = cv2.Tracker_create(tracker_type)
else:
    if tracker_type == 'BOOSTING':
        tracker = cv2.TrackerBoosting_create()
    if tracker_type == 'MIL':
        tracker = cv2.TrackerMIL_create()
    if tracker_type == 'KCF':
        tracker = cv2.TrackerKCF_create()
    if tracker_type == 'TLD':
        tracker = cv2.TrackerTLD_create()
    if tracker_type == 'MEDIANFLOW':
        tracker = cv2.TrackerMedianFlow_create()
    if tracker_type == 'GOTURN':
        tracker = cv2.TrackerGOTURN_create()

cap = cv2.VideoCapture(0)
ok, frame = cap.read()

#Initializing a bounding box 
bbox = (287, 23, 86, 320)

bbox = cv2.selectROI(frame, False)

ok = tracker.init(frame, bbox)

while(1):
    ok, frame = cap.read()
    timer = cv2.getTickCount()
    ok, bbox = tracker.update(frame)
    fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
    if ok:
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
    else :
        # Tracking failure
        cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
    cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);

    # Display FPS on frame
    cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);

    # Display result
    cv2.imshow("Tracking", frame)

    K = cv2.waitKey(1)
    if (K==27):
        break
cap.release()
cv2.destroyAllWindows()