Visual Servoing Platform
version 3.0.0
|
With ViSP it is possible to track keypoints using OpenCV KLT tracker, an implementation of the Kanade-Lucas-Tomasi feature tracker. The following example code available in tutorial-klt-tracker.cpp shows how to use ViSP vpKltOpencv class to this end. This class is a wrapper over the OpenCV KLT tracker implementation.
The video shows the result of the tracking:
The previous example can be run without command line options. In that case, keypoints are automatically detected before tracking.
It can also be run with [–init-by-click] option. In that case, the user can select a set of keypoints to track with a left mouse click. A right mouse click stops the keypoints selection and allows to start the tracking.
Here is the line by line explanation of the source :
We include here the headers that define the corresponding classes. vpImageConvert class will be used to convert ViSP images implemented in vpImage class into OpenCV IplImage or cv::Mat structures used as an entry by the KLT tracker. Then we include the header of vpKltOpencv class which is the wrapper over OpenCV KLT tracker implementation.
We need also to include a device to display the images. We retain vpDisplayOpenCV that works on Unix and Windows since OpenCV is mandatory by the tracker. Finally we include vpVideoReader header that will be used to read an mpeg input stream.
At the beginning of the main() function, we use the following macro to ensure that OpenCV requested by the tracker is available. Note that OpenCV will also be used to render the images and read the input video stream.
The program starts by the creation of a vpVideoReader instance able to extract all the images of the video file video-postcard.mpeg
. Here, the video should be in the same folder than the binary.
Then we extract the first image of the video in the gray level ViSP image container I
.
This image I
is then converted into cvI
, an OpenCV image format that will be used by the tracker.
We also create a window associated to I
, at position (0,0) in the screen, with "Klt tracking" as title, and display image I
.
From now we have to create an instance of the tracker and set the parameters of the Harris keypoint detector.
The tracker is then initialized on cvI
image.
With the next line the user can know how many keypoints were detected automatically or selected by the user during initialization.
To detect more keypoints, you may decrease the quality parameter set with the following line:
Until the end of the video, we get I
the next image in ViSP format, display and convert it in OpenCV format. Then we track the Harris keypoints using KLT tracker before displaying the keypoints that are tracked with a red cross.
We are waiting for a mouse click event on image I
to end the program.
With the following line, we release the memory allocated for the OpenCV IplImage cvI
before ending the program. This has to be done only if OpenCV version is less than 2.8.0.
Once initialized, the number of tracked features decreases over the time. Depending on a criteria, it may sense to detect and track new features online. A possible criteria is for example to compare the number of currently tracked features to the initial number of detected features. If less than a given percentage of features are tracked, you can start a new detection.
To get the number of detected or tracked features just call:
Then the idea is to add the previously tracked features to the list of features that are detected.
The example tutorial-klt-tracker-with-reinit.cpp shows how to do that. In that example we start a new detection on frame 25. Compared to the previous code available in tutorial-klt-tracker.cpp we add the following lines:
In this code we do the following:
You are now ready to see the next Tutorial: Moving-edges tracking.