ROI tracking by using OpenCV

目录

  • source code:

source code:

import cv2

tracker = cv2.TrackerKCF_create()
video = cv2.VideoCapture(1)

while True:
    ret,frame = video.read()
    cv2.imshow("source frame",frame)
    k = cv2.waitKey(30)
    if k == 'q':
        break 

bbox = cv2.selectROI(frame, False)
ok = tracker.init(frame,bbox)
cv2.destroyWindow("ROI Selector")

while True:
    ret, frame = video.read()
    ret, bbox = tracker.update(frame)
    
    if ret:
        p1 = (int(bbox[0]),int(bbox[1]))
        p1 = (int(bbox[0]+bbox[2]),int(bbox[1]+bbox[3]))
        cv2.rectangle(frame, p1,p2,(0,0,255),2,2)
        
    cv2.imshow("Tracking", frame)
    k = cv2.waitkey(1)
    if k == 27:
        break

你可能感兴趣的:(SLAM,opencv,人工智能,计算机视觉)