Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-klt-tracker.cpp
1 #include <visp3/core/vpConfig.h>
4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/io/vpVideoReader.h>
7 #include <visp3/klt/vpKltOpencv.h>
9 
10 int main(int argc, const char *argv[])
11 {
13 #if (defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO) || defined(VISP_HAVE_V4L2)) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
18 
19  try {
20  std::string opt_videoname = "video-postcard.mp4";
21  bool opt_init_by_click = false;
22  unsigned int opt_subsample = 1;
23  for (int i = 0; i < argc; i++) {
24  if (std::string(argv[i]) == "--videoname")
25  opt_videoname = std::string(argv[i + 1]);
26  else if (std::string(argv[i]) == "--init-by-click")
27  opt_init_by_click = true;
28  else if (std::string(argv[i]) == "--subsample")
29  opt_subsample = static_cast<unsigned int>(std::atoi(argv[i + 1]));
30  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
31  std::cout << "Usage: " << argv[0]
32  << " [--videoname <video name>] [--subsample <scale factor>] [--init-by-click]"
33  << " [--help] [-h]" << std::endl;
34  return EXIT_SUCCESS;
35  }
36  }
37 
39  vpVideoReader reader;
40  reader.setFileName(opt_videoname);
42 
44  vpImage<unsigned char> I, Iacq;
45  reader.acquire(Iacq);
46  Iacq.subsample(opt_subsample, opt_subsample, I);
48 
50  cv::Mat cvI;
53 
55  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
59 
61  vpKltOpencv tracker;
62  tracker.setMaxFeatures(200);
63  tracker.setWindowSize(10);
65  tracker.setQuality(0.01);
67  tracker.setMinDistance(15);
68  tracker.setHarrisFreeParameter(0.04);
69  tracker.setBlockSize(9);
70  tracker.setUseHarris(1);
71  tracker.setPyramidLevels(3);
73 
74  // Initialise the tracking
75  if (opt_init_by_click) {
77  std::vector<cv::Point2f> feature;
78  vpImagePoint ip;
79  do {
80  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
81  if (vpDisplay::getClick(I, ip, button, false)) {
82  if (button == vpMouseButton::button1) {
83  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
85  }
86  }
88  vpTime::wait(20);
89  } while (button != vpMouseButton::button3);
90  tracker.initTracking(cvI, feature);
91  }
92  else {
94  tracker.initTracking(cvI);
96  }
97 
99  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
101 
103  while (!reader.end()) {
104  double t = vpTime::measureTimeMs();
105  reader.acquire(Iacq);
106  Iacq.subsample(opt_subsample, opt_subsample, I);
108 
109  vpImageConvert::convert(I, cvI);
110 
111  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
113  std::vector<cv::Point2f> feature;
114  vpImagePoint ip;
115  do {
116  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
117  if (vpDisplay::getClick(I, ip, button, false)) {
118  if (button == vpMouseButton::button1) {
119  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
121  }
122  }
123  vpDisplay::flush(I);
124  vpTime::wait(20);
125  } while (button != vpMouseButton::button3);
126  tracker.initTracking(cvI, feature);
127  }
128 
129  tracker.track(cvI);
130 
131  tracker.display(I, vpColor::red);
132 
133  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
134  if (vpDisplay::getClick(I, false))
135  break;
136 
137  vpDisplay::flush(I);
138  if (!reader.isVideoFormat()) {
139  vpTime::wait(t, 40);
140  }
141  }
143 
147  }
148  catch (const vpException &e) {
149  std::cout << "Catch an exception: " << e << std::endl;
150  return EXIT_FAILURE;
151 }
152 #else
153  (void)argc;
154  (void)argv;
155 #endif
156  return EXIT_SUCCESS;
157 }
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
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:60
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:753
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
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
Definition: vpKltOpencv.cpp:94
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
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()