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