Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-klt-tracker-with-reinit.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/io/vpVideoReader.h>
6 #include <visp3/klt/vpKltOpencv.h>
7 
8 int main()
9 {
10 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && defined(HAVE_OPENCV_VIDEOIO)
11 #ifdef ENABLE_VISP_NAMESPACE
12  using namespace VISP_NAMESPACE_NAME;
13 #endif
14 
15  try {
16  vpVideoReader reader;
17  reader.setFileName("video-postcard.mp4");
18 
20  reader.acquire(I);
21 
22  cv::Mat cvI;
23 
25 
26  // Display initialisation
27  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
30 
31  vpKltOpencv tracker;
32  // Set tracker parameters
33  tracker.setMaxFeatures(200);
34  tracker.setWindowSize(10);
35  tracker.setQuality(0.01);
36  tracker.setMinDistance(15);
37  tracker.setHarrisFreeParameter(0.04);
38  tracker.setBlockSize(9);
39  tracker.setUseHarris(1);
40  tracker.setPyramidLevels(3);
41 
42  // Initialise the tracking
43  tracker.initTracking(cvI);
44 
45  while (!reader.end()) {
46  reader.acquire(I);
47  std::cout << "Process image " << reader.getFrameIndex() << std::endl;
49 
51 
53  // Restart the initialization to detect new keypoints
54  if (reader.getFrameIndex() == 25) {
55  std::cout << "Re initialize the tracker" << std::endl;
56 
57  // Save of previous features
58  std::vector<cv::Point2f> prev_features = tracker.getFeatures();
59 
60  // Start a new feature detection
61  tracker.initTracking(cvI);
62  std::vector<cv::Point2f> new_features = tracker.getFeatures();
63 
64  // Add previous features if they are not to close to detected one
65  double distance, minDistance_ = tracker.getMinDistance();
66  for (size_t i = 0; i < prev_features.size(); i++) {
67  // Test if a previous feature is not redundant with one of the newly
68  // detected
69  bool is_redundant = false;
70  for (size_t j = 0; j < new_features.size(); j++) {
71  distance = sqrt(vpMath::sqr(new_features[j].x - prev_features[i].x) +
72  vpMath::sqr(new_features[j].y - prev_features[i].y));
73  if (distance < minDistance_) {
74  is_redundant = true;
75  break;
76  }
77  }
78  if (is_redundant) {
79  continue;
80  }
81  // std::cout << "Add previous feature with index " << i <<
82  // std::endl;
83  tracker.addFeature(prev_features[i]);
84  }
85  }
86  // Track the features
87  tracker.track(cvI);
89 
90  std::cout << "tracking of " << tracker.getNbFeatures() << " features" << std::endl;
91 
92  tracker.display(I, vpColor::red);
94  }
95 
97 
98  }
99  catch (const vpException &e) {
100  std::cout << "Catch an exception: " << e << std::endl;
101  return EXIT_FAILURE;
102  }
103 #endif
104  return EXIT_SUCCESS;
105 }
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