2017-04-20 24 views
0

我有一個計算成本高昂的方式來查找圖像中某個特徵的確切邊界框。在所有後續圖像上,該功能可能已移動。我希望避免在每一幀上執行這個計算量很大的過程。是否有一種技術可以使用諸如背景減法+輪廓檢測之類的功能追蹤一個特徵,並在其邊界框被識別一次之後?從鏈接知道圖像中某個特徵的確切位置後,在所有後續幀上跟蹤它。

+0

嘗試openCV 3.1的TLD跟蹤 – Micka

回答

1

Object Tracking using OpenCV (C++/Python)

的Python代碼示例:

import cv2 
import sys 

if __name__ == '__main__' : 

# Set up tracker. 
# Instead of MIL, you can also use 
# BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN 

tracker = cv2.Tracker_create("MIL") 

# Read video 
video = cv2.VideoCapture("videos/chaplin.mp4") 

# Exit if video not opened. 
if not video.isOpened(): 
    print "Could not open video" 
    sys.exit() 

# Read first frame. 
ok, frame = video.read() 
if not ok: 
    print 'Cannot read video file' 
    sys.exit() 

# Define an initial bounding box 
bbox = (287, 23, 86, 320) # x, y, width, height 

# Uncomment the line below to select a different bounding box 
# bbox = cv2.selectROI(frame, False) 

# Initialize tracker with first frame and bounding box 
ok = tracker.init(frame, bbox) 

while True: 
    # Read a new frame 
    ok, frame = video.read() 
    if not ok: 
     break 

    # Update tracker 
    ok, bbox = tracker.update(frame) 

    # Draw bounding box 
    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, (0,0,255)) 

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

    # Exit if ESC pressed 
    k = cv2.waitKey(1) & 0xff 
    if k == 27 : break 

希望這有助於!

+0

邊界框中點的順序是什麼?我得到[這個錯誤](http://stackoverflow.com/questions/43593444/opencv-tracker-the-model-is-not-initialized-in-function-init)當我嘗試這種方法 – Carpetfizz

+1

一個元組與4值通過以下命令:1.邊界框左上角的x座標 2.邊界框左上角的y座標 3.邊界框的寬度 4.邊界框的高度 – kai06046

+0

座標系的高度如何座標是1值? – Carpetfizz