The Tracker class is an abstract base class that defines the interface for all tracking algorithms in this library. To use a specific tracking algorithm, you must call init to set the initial target location and then call update in a loop to follow the target in subsequent frames.
Key methods:
init(const cv::Rect &roi, cv::Mat image): Initializes the tracker with the initial bounding box (roi) and the first frame (image).update(cv::Mat image): Processes a new frame and returns the updated bounding box (cv::Rect) of the target. If the target is not found, the behavior depends on the specific implementation (typically returning an empty rect or the last known position).
// Example usage pattern for a Tracker implementation
Tracker* tracker = /* get instance of a specific tracker, e.g., KCFTracker */;
// 1. Initialize with the first frame and the target ROI
cv::Rect initial_roi(100, 100, 50, 50);
cv::Mat first_frame = cv::imread("frame0.jpg");
tracker->init(initial_roi, first_frame);
// 2. Update in a loop with subsequent frames
while (true) {
cv::Mat frame = /* get next frame from video stream or file */;
if (frame.empty()) break;
cv::Rect updated_roi = tracker->update(frame);
if (updated_roi.width > 0 && updated_roi.height > 0) {
// Draw the bounding box on the frame
cv::rectangle(frame, updated_roi, cv::Scalar(0, 255, 0), 2);
}
cv::imshow("Tracking", frame);
if (cv::waitKey(30) == 27) break;
}
delete tracker;