Object Detection and Tracking with OpenCV and C++

In this article, we are going to show how to develop, in a few lines of C++ code, a simple object detector and tracker, using the OpenCV library on a Windows system. So, are you ready? Let’s begin!

Configure your environment

First of all, you need to configure your environment. Be patient, because a long series of tedious setups are required! 😉 Here below, find the list of the software to install:

Once that are installed, download the source code of the following repositories:

All done? Great! Now extract both the archives in the same folder, and open Cmake, following the steps below:

1. Fill in the following input fields as below:

2. Click on the “Configure” button and fill in the following input fields as below:

3. Click on the “Finish” button. At the end of the configuration process, update the build configuration as below:

4. Uncheck BUILD_opencv_python3

5. Click on the “Add Entry” button and fill in the following input fields as below:

6. Click on the “OK“, “Configure” and “Generate” buttons, then wait for the generation process to finish.

7. From the build folder (C:/opencv-4.1.2/build), run one of the following commands by using a command prompt:

# Build in Debug mode
cmake.exe --build . --config Debug --target INSTALL

# Build in Release mode
cmake.exe --build . --config Release --target INSTALL

8. The build process will take a while. Once finished, add the OpenCV bin folder to your system PATH:

Great! As the final step, you have to download the project that we are going to analyse in this tutorial. So, check out this ready-to-use code repository, and open the Visual Studio solution inside. Then, by taking a look at the Visual C++ project properties (“Right-click on the project > Properties“), make sure to have the following configuration for the x64 plaform:

1. C/C++ – General

2. Linker – General

3. Linker – Input

The “Additional Dependencies” field, in addition to the existing libraries, must contain the names of all the .lib files present inside the following folder:


Tip. There are a lot of libraries inside the lib folder. To get the names of the libraries quickly, run the following command by using a command prompt:

dir > libs.txt

In this way, from the libs.txt file you can extrapolate the names of the libraries to put into the “Additional Dependencies” field. Use software like Notepad++ to simplify this operation.

Run the example

Bored yet? Don’t worry! Now we are going to have some fun! By running the example project with Visual Studio, a new window will be shown, where the frames captured from your webcam will be rendered. Put any object in front of the camera and move it into the coloured box, in the middle of the window. Now press “Space” on your keyboard and try to move the object around… magic!

Taking a look at the code behind

How does this magic work? Inside the Object-Tracking-OpenCV.cpp file, you can get more details. Firstly, we need to instantiate a new Tracker:

TrackerFactory trackerFactory(static_cast<TrackerType>(2));
auto tracker = trackerFactory.GetTracker();

The OpenCV tracking module makes different kind of trackers available. Below is the full list:

  • TrackerBoosting
  • TrackerMIL
  • TrackerKCF
  • TrackerTLD
  • TrackerMedianFlow
  • TrackerGOTURN
  • TrackerMOSSE
  • TrackerCSRT

In this example, we are going to use TrackerKCF, but you can get more information about trackers and their differences from this article. Once the tracker is instantiated, we can activate the camera:

VideoCapture camera(0);
if (!camera.isOpened()) return 1;

Now we can analyse each frame captured from the webcam inside a while-loop:

Mat frame;
Rect2d bbox(250, 100, 200, 225);

while (camera.read(frame))
    // Frame loop

At first, a coloured box is drawed in the middle of the captured frames, pointing out where to put the object to be tracked:

rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);

Once the “Enter” button is pressed in the keyboard, the next frame is used to initialise the tracker. The object captured inside the coloured box become the object to detect and track:

tracker->init(frame, bbox);

In the next frames, the tracker checks if an update is available, then draws the coloured box on the current frame to follow the new position of the object:

if (tracker->update(frame, bbox))
    rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);

Finally, we need to show the edited frame:

imshow("Tracking", frame);

And that’s it!
You have created your first simple object tracker by using just a few lines of code!

Happy coding!