Visual Servoing Platform  version 3.6.1 under development (2025-03-07)
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
tutorial-klt-tracker.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpConfig.h>
5 
7 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && defined(HAVE_OPENCV_VIDEOIO)
9 
11 #include <visp3/core/vpImageConvert.h>
12 #include <visp3/gui/vpDisplayOpenCV.h>
13 #include <visp3/io/vpVideoReader.h>
14 #include <visp3/klt/vpKltOpencv.h>
16 
17 int main(int argc, const char *argv[])
18 {
19 #ifdef ENABLE_VISP_NAMESPACE
20  using namespace VISP_NAMESPACE_NAME;
21 #endif
22 
23  try {
24  std::string opt_videoname = "video-postcard.mp4";
25  bool opt_init_by_click = false;
26  unsigned int opt_subsample = 1;
27  for (int i = 1; i < argc; i++) {
28  if (std::string(argv[i]) == "--videoname") {
29  opt_videoname = std::string(argv[++i]);
30  }
31  else if (std::string(argv[i]) == "--init-by-click") {
32  opt_init_by_click = true;
33  }
34  else if (std::string(argv[i]) == "--subsample") {
35  opt_subsample = static_cast<unsigned int>(std::atoi(argv[++i]));
36  }
37  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
38  std::cout << "Usage: " << argv[0]
39  << " [--videoname <video name>] [--subsample <scale factor>] [--init-by-click]"
40  << " [--help] [-h]" << std::endl;
41  return EXIT_SUCCESS;
42  }
43  }
44 
46  vpVideoReader reader;
47  reader.setFileName(opt_videoname);
49 
51  vpImage<unsigned char> I, Iacq;
52  reader.acquire(Iacq);
53  Iacq.subsample(opt_subsample, opt_subsample, I);
55 
57  cv::Mat cvI;
60 
62  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
66 
68  vpKltOpencv tracker;
69  tracker.setMaxFeatures(200);
70  tracker.setWindowSize(10);
72  tracker.setQuality(0.01);
74  tracker.setMinDistance(15);
75  tracker.setHarrisFreeParameter(0.04);
76  tracker.setBlockSize(9);
77  tracker.setUseHarris(1);
78  tracker.setPyramidLevels(3);
80 
81  // Initialise the tracking
82  if (opt_init_by_click) {
84  std::vector<cv::Point2f> feature;
85  vpImagePoint ip;
86  do {
87  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
88  if (vpDisplay::getClick(I, ip, button, false)) {
89  if (button == vpMouseButton::button1) {
90  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
92  }
93  }
95  vpTime::wait(20);
96  } while (button != vpMouseButton::button3);
97  tracker.initTracking(cvI, feature);
98  }
99  else {
101  tracker.initTracking(cvI);
103  }
104 
106  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
108 
110  while (!reader.end()) {
111  double t = vpTime::measureTimeMs();
112  reader.acquire(Iacq);
113  Iacq.subsample(opt_subsample, opt_subsample, I);
115 
116  vpImageConvert::convert(I, cvI);
117 
118  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
120  std::vector<cv::Point2f> feature;
121  vpImagePoint ip;
122  do {
123  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
124  if (vpDisplay::getClick(I, ip, button, false)) {
125  if (button == vpMouseButton::button1) {
126  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
128  }
129  }
130  vpDisplay::flush(I);
131  vpTime::wait(20);
132  } while (button != vpMouseButton::button3);
133  tracker.initTracking(cvI, feature);
134  }
135 
136  tracker.track(cvI);
137 
138  tracker.display(I, vpColor::red);
139 
140  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
141  if (vpDisplay::getClick(I, false))
142  break;
143 
144  vpDisplay::flush(I);
145  if (!reader.isVideoFormat()) {
146  vpTime::wait(t, 40);
147  }
148  }
150 
154  }
155  catch (const vpException &e) {
156  std::cout << "Catch an exception: " << e << std::endl;
157  return EXIT_FAILURE;
158  }
159  return EXIT_SUCCESS;
160 }
161 
162 #else
163 
164 int main()
165 {
166 #if !defined(HAVE_OPENCV_HIGHGUI)
167  std::cout << "This tutorial needs OpenCV highgui module that is missing." << std::endl;
168 #endif
169 #if !defined(HAVE_OPENCV_IMGPROC)
170  std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
171 #endif
172 #if !defined(HAVE_OPENCV_VIDEO)
173  std::cout << "This tutorial needs OpenCV video module that is missing." << std::endl;
174 #endif
175 #if !defined(HAVE_OPENCV_VIDEOIO)
176  std::cout << "This tutorial needs OpenCV videoio module that is missing." << std::endl;
177 #endif
178 }
179 
180 #endif
static const vpColor red
Definition: vpColor.h:198
static const vpColor green
Definition: vpColor.h:201
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:755
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:79
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1) const
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:272
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:361
void track(const cv::Mat &I)
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:203
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:280
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:320
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
Definition: vpKltOpencv.cpp:94
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:329
void setUseHarris(int useHarrisDetector)
Definition: vpKltOpencv.h:373
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:382
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:348
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()