How to Track Object in Video Using OpenCV in Computer Vision
To track an object in video using
OpenCV, you first select the object region with cv2.selectROI, then create a tracker like cv2.TrackerCSRT_create(), and update the tracker frame-by-frame with tracker.update(). This process allows you to follow the object's position across video frames efficiently.Syntax
Object tracking in OpenCV involves these main steps:
cv2.selectROI: Manually select the object region in the first frame.cv2.TrackerCSRT_create(): Create a tracker instance (CSRT is accurate and robust).tracker.init(frame, bbox): Initialize the tracker with the first frame and bounding box.tracker.update(frame): Update the tracker with each new frame to get the new bounding box.
python
import cv2 # Create tracker tracker = cv2.TrackerCSRT_create() # Select ROI on first frame bbox = cv2.selectROI('Frame', frame, False) # Initialize tracker tracker.init(frame, bbox) # In loop for each frame: (success, bbox) = tracker.update(frame)
Example
This example shows how to track a selected object in a video file or webcam stream using OpenCV's CSRT tracker.
python
import cv2 # Open video or webcam video = cv2.VideoCapture(0) # Use 0 for webcam or replace with video file path # Read first frame ret, frame = video.read() if not ret: print('Failed to read video') exit() # Select ROI bbox = cv2.selectROI('Tracking', frame, False) cv2.destroyWindow('Tracking') # Create tracker tracker = cv2.TrackerCSRT_create() tracker.init(frame, bbox) while True: ret, frame = video.read() if not ret: break # Update tracker success, bbox = tracker.update(frame) if success: # Draw bounding box x, y, w, h = map(int, bbox) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(frame, 'Tracking', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) else: cv2.putText(frame, 'Lost', (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) cv2.imshow('Tracking', frame) if cv2.waitKey(1) & 0xFF == 27: # ESC key to exit break video.release() cv2.destroyAllWindows()
Output
A window opens showing the video stream with a green rectangle tracking the selected object; if tracking fails, 'Lost' appears in red text.
Common Pitfalls
Common mistakes when tracking objects with OpenCV include:
- Not selecting the ROI correctly or forgetting to initialize the tracker.
- Using an incompatible tracker for the object or video conditions (CSRT is good for accuracy, KCF is faster but less robust).
- Not handling tracker failure when the object is lost or occluded.
- Forgetting to release video capture and destroy windows, causing resource leaks.
Always check the success flag from tracker.update() to handle lost tracking gracefully.
python
import cv2 # Wrong: Not initializing tracker before update tracker = cv2.TrackerCSRT_create() # This will fail because tracker.init() is missing ret, frame = video.read() (success, bbox) = tracker.update(frame) # Wrong usage # Correct usage: # tracker.init(frame, bbox) before calling update
Quick Reference
| Step | Function | Description |
|---|---|---|
| 1 | cv2.selectROI(window, frame, False) | Manually select object bounding box |
| 2 | cv2.TrackerCSRT_create() | Create a CSRT tracker instance |
| 3 | tracker.init(frame, bbox) | Initialize tracker with first frame and bbox |
| 4 | success, bbox = tracker.update(frame) | Update tracker for each new frame |
| 5 | cv2.rectangle(frame, (x,y), (x+w,y+h), color, thickness) | Draw bounding box on frame |
Key Takeaways
Use cv2.selectROI to manually select the object region in the first frame.
Create and initialize a tracker like cv2.TrackerCSRT_create() before updating it.
Call tracker.update(frame) on each new frame to get the object's new position.
Check the success flag from tracker.update() to handle tracking failures.
Release video capture and destroy OpenCV windows to free resources.