OpenCV  4.2.0
Open Source Computer Vision
Introduction to OpenCV Tracker

.2.0+dfsg_contrib_modules_tracking_tutorials_tutorial_introduction_to_tracker

Goal

In this tutorial you will learn how to

  • Create a tracker object.
  • Use the roiSelector function to select a ROI from a given image.
  • Track a specific region in a given image.

Source Code

#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
int main( int argc, char** argv ){
// show help
if(argc<2){
cout<<
" Usage: tracker <video_name>\n"
" examples:\n"
" example_tracking_kcf Bolt/img/%04d.jpg\n"
" example_tracking_kcf faceocc2.webm\n"
<< endl;
return 0;
}
// declares all required variables
Rect2d roi;
Mat frame;
// create a tracker object
Ptr<Tracker> tracker = TrackerKCF::create();
// set input video
std::string video = argv[1];
VideoCapture cap(video);
// get bounding box
cap >> frame;
roi=selectROI("tracker",frame);
//quit if ROI was not selected
if(roi.width==0 || roi.height==0)
return 0;
// initialize the tracker
tracker->init(frame,roi);
// perform the tracking process
printf("Start the tracking process, press ESC to quit.\n");
for ( ;; ){
// get frame from the video
cap >> frame;
// stop the program if no more images
if(frame.rows==0 || frame.cols==0)
break;
// update the tracking result
tracker->update(frame,roi);
// draw the tracked object
rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
// show image with the tracked object
imshow("tracker",frame);
//quit on ESC button
if(waitKey(1)==27)break;
}
return 0;
}

Explanation

  1. Set up the input video

    if(argc<2){
    cout<<
    " Usage: tracker <video_name>\n"
    " examples:\n"
    " example_tracking_kcf Bolt/img/%04d.jpg\n"
    " example_tracking_kcf faceocc2.webm\n"
    << endl;
    return 0;
    }

    In this tutorial, you can choose between video or list of images for the program input. As written in the help, you should specify the input video as parameter of the program. If you want to use image list as input, the image list should have formatted numbering as shown in help. In the help, it means that the image files are numbered with 4 digits (e.g. the file naming will be 0001.jpg, 0002.jpg, and so on).

    You can find video samples in opencv_extra/testdata/cv/tracking https://github.com/opencv/opencv_extra/tree/master/testdata/cv/tracking

  2. Declares the required variables

    You need roi to record the bounding box of the tracked object. The value stored in this variable will be updated using the tracker object.

    Rect2d roi;
    Mat frame;

    The frame variable is used to hold the image data from each frame of the input video or images list.

  3. Creating a tracker object

    Ptr<Tracker> tracker = TrackerKCF::create();

    There are at least 7 types of tracker algorithms that can be used:

    • MIL
    • BOOSTING
    • MEDIANFLOW
    • TLD
    • KCF
    • GOTURN
    • MOSSE

    Each tracker algorithm has their own advantages and disadvantages, please refer the documentation of cv::Tracker for more detailed information.

  4. Select the tracked object

    roi=selectROI("tracker",frame);

    Using this function, you can select the bounding box of the tracked object using a GUI. With default parameters, the selection is started from the center of the box and a middle cross will be shown.

  5. Initializing the tracker object

    tracker->init(frame,roi);

    Any tracker algorithm should be initialized with the provided image data, and an initial bounding box of the tracked object. Make sure that the bounding box is valid (size more than zero) to avoid failure of the initialization process.

  6. Update

    tracker->update(frame,roi);

    This update function will perform the tracking process and pass the result to the roi variable.

cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:609
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
highgui.hpp
cv::rectangle
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
cv::Tracker::init
bool init(InputArray image, const Rect2d &boundingBox)
Initialize the tracker with a known bounding box that surrounded the target.
cv::selectROI
Rect selectROI(const String &windowName, InputArray img, bool showCrosshair=true, bool fromCenter=false)
Selects ROI on the given image. Function creates a window and allows user to select a ROI using mouse...
cv::Ptr
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
cv::Rect_
Template class for 2D rectangles.
Definition: types.hpp:421
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Rect2d
Rect_< double > Rect2d
Definition: types.hpp:461
cv::Rect_::width
_Tp width
width of the rectangle
Definition: types.hpp:455
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
tracking.hpp
cv::Rect_::height
_Tp height
height of the rectangle
Definition: types.hpp:456
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:792
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
utility.hpp
cv::Tracker::update
bool update(InputArray image, Rect2d &boundingBox)
Update the tracker, find the new most likely bounding box for the target.
videoio.hpp