Visual Servoing Platform  version 3.6.1 under development (2024-04-16)
tutorial-klt-tracker.cpp
1 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/io/vpVideoReader.h>
6 #include <visp3/klt/vpKltOpencv.h>
8 
9 int main(int argc, const char *argv[])
10 {
12 #if (defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO) || defined(VISP_HAVE_V4L2)) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
14  try {
15  std::string opt_videoname = "video-postcard.mp4";
16  bool opt_init_by_click = false;
17  unsigned int opt_subsample = 1;
18  for (int i = 0; i < argc; i++) {
19  if (std::string(argv[i]) == "--videoname")
20  opt_videoname = std::string(argv[i + 1]);
21  else if (std::string(argv[i]) == "--init-by-click")
22  opt_init_by_click = true;
23  else if (std::string(argv[i]) == "--subsample")
24  opt_subsample = static_cast<unsigned int>(std::atoi(argv[i + 1]));
25  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
26  std::cout << "Usage: " << argv[0]
27  << " [--videoname <video name>] [--subsample <scale factor>] [--init-by-click]"
28  << " [--help] [-h]" << std::endl;
29  return EXIT_SUCCESS;
30  }
31  }
32 
34  vpVideoReader reader;
35  reader.setFileName(opt_videoname);
37 
39  vpImage<unsigned char> I, Iacq;
40  reader.acquire(Iacq);
41  Iacq.subsample(opt_subsample, opt_subsample, I);
43 
45  cv::Mat cvI;
48 
50  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
54 
56  vpKltOpencv tracker;
57  tracker.setMaxFeatures(200);
58  tracker.setWindowSize(10);
60  tracker.setQuality(0.01);
62  tracker.setMinDistance(15);
63  tracker.setHarrisFreeParameter(0.04);
64  tracker.setBlockSize(9);
65  tracker.setUseHarris(1);
66  tracker.setPyramidLevels(3);
68 
69  // Initialise the tracking
70  if (opt_init_by_click) {
72  std::vector<cv::Point2f> feature;
73  vpImagePoint ip;
74  do {
75  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
76  if (vpDisplay::getClick(I, ip, button, false)) {
77  if (button == vpMouseButton::button1) {
78  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
80  }
81  }
83  vpTime::wait(20);
84  } while (button != vpMouseButton::button3);
85  tracker.initTracking(cvI, feature);
86  }
87  else {
89  tracker.initTracking(cvI);
91  }
92 
94  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
96 
98  while (!reader.end()) {
99  double t = vpTime::measureTimeMs();
100  reader.acquire(Iacq);
101  Iacq.subsample(opt_subsample, opt_subsample, I);
103 
104  vpImageConvert::convert(I, cvI);
105 
106  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
108  std::vector<cv::Point2f> feature;
109  vpImagePoint ip;
110  do {
111  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
112  if (vpDisplay::getClick(I, ip, button, false)) {
113  if (button == vpMouseButton::button1) {
114  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
116  }
117  }
118  vpDisplay::flush(I);
119  vpTime::wait(20);
120  } while (button != vpMouseButton::button3);
121  tracker.initTracking(cvI, feature);
122  }
123 
124  tracker.track(cvI);
125 
126  tracker.display(I, vpColor::red);
127 
128  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
129  if (vpDisplay::getClick(I, false))
130  break;
131 
132  vpDisplay::flush(I);
133  if (!reader.isVideoFormat()) {
134  vpTime::wait(t, 40);
135  }
136  }
138 
142  }
143  catch (const vpException &e) {
144  std::cout << "Catch an exception: " << e << std::endl;
145  return EXIT_FAILURE;
146  }
147 #else
148  (void)argc;
149  (void)argv;
150 #endif
151  return EXIT_SUCCESS;
152 }
static const vpColor red
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:214
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 displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition: vpImage.h:1638
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
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:274
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:314
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
Definition: vpKltOpencv.cpp:93
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
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
bool isVideoFormat() const
void acquire(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFirstFrameIndex()
long getFrameIndex() const
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()