Object tracking jitter

I’m working on an application that crops out relevant bits of a video frames. The cropped photos are then superimposed and further processed, but the problem is that cv2 trackers seem to have a lot of jitter which throws off the other functions because they’re not superimposed properly.

Here’s some code to demonstrate what’s going on.

cv2 Object tracking example

import cv2

tracker = cv2.TrackerCSRT_create()
video = cv2.VideoCapture('project.mp4')
ok,frame=video.read()
bbox = cv2.selectROI(frame)
ok = tracker.init(frame,bbox)
while True:
    ok,frame=video.read()
    if not ok:
        break
    ok,bbox=tracker.update(frame)
    if ok:
        (x,y,w,h)=[int(v) for v in bbox]
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2,1)
    else:
        cv2.putText(frame,'Error',(100,0),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)
    cv2.imshow('Tracking',frame)
    if cv2.waitKey(1) & 0XFF==27:
        break
cv2.destroyAllWindows()

To help explain this, I created a video using the same image repeated 100 times or so. There is no movement in the video as the frame change, so the object box ideally wouldn’t be changing.

link to download test video

But you can see if you run the code with that video that the tracker ROI box jitters back and forth every frame despite there being no movement.

Any ideas?

undeclared crosspost:

I made the post in both forums. Do I need to declare this?

it would be the polite thing to do. crossposting (without crosslinking), on the internet, has always been frowned upon for various reasons.

Cross-posting has the potential to waste people’s time both in reading and in responding.