Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-klt-tracker-with-reinit.cpp
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpImageConvert.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/klt/vpKltOpencv.h>
int main()
{
#if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && defined(HAVE_OPENCV_VIDEOIO)
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
try {
vpVideoReader reader;
reader.setFileName("video-postcard.mp4");
reader.acquire(I);
cv::Mat cvI;
// Display initialisation
vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
vpKltOpencv tracker;
// Set tracker parameters
tracker.setMaxFeatures(200);
tracker.setWindowSize(10);
tracker.setQuality(0.01);
tracker.setMinDistance(15);
tracker.setHarrisFreeParameter(0.04);
tracker.setBlockSize(9);
tracker.setUseHarris(1);
tracker.setPyramidLevels(3);
// Initialise the tracking
tracker.initTracking(cvI);
while (!reader.end()) {
reader.acquire(I);
std::cout << "Process image " << reader.getFrameIndex() << std::endl;
// Restart the initialization to detect new keypoints
if (reader.getFrameIndex() == 25) {
std::cout << "Re initialize the tracker" << std::endl;
// Save of previous features
std::vector<cv::Point2f> prev_features = tracker.getFeatures();
// Start a new feature detection
tracker.initTracking(cvI);
std::vector<cv::Point2f> new_features = tracker.getFeatures();
// Add previous features if they are not to close to detected one
double distance, minDistance_ = tracker.getMinDistance();
for (size_t i = 0; i < prev_features.size(); i++) {
// Test if a previous feature is not redundant with one of the newly
// detected
bool is_redundant = false;
for (size_t j = 0; j < new_features.size(); j++) {
distance = sqrt(vpMath::sqr(new_features[j].x - prev_features[i].x) +
vpMath::sqr(new_features[j].y - prev_features[i].y));
if (distance < minDistance_) {
is_redundant = true;
break;
}
}
if (is_redundant) {
continue;
}
// std::cout << "Add previous feature with index " << i <<
// std::endl;
tracker.addFeature(prev_features[i]);
}
}
// Track the features
tracker.track(cvI);
std::cout << "tracking of " << tracker.getNbFeatures() << " features" << std::endl;
tracker.display(I, vpColor::red);
}
}
catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}
static const vpColor red
Definition: vpColor.h:217
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:74
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1) const
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:267
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:356
void track(const cv::Mat &I)
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:198
std::vector< cv::Point2f > getFeatures() const
Get the list of current features.
Definition: vpKltOpencv.h:183
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void addFeature(const float &x, const float &y)
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
Definition: vpKltOpencv.cpp:94
double getMinDistance() const
Definition: vpKltOpencv.h:196
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:324
void setUseHarris(int useHarrisDetector)
Definition: vpKltOpencv.h:368
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:377
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:343
static double sqr(double x)
Definition: vpMath.h:203
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFrameIndex() const