Visual Servoing Platform  version 3.6.1 under development (2023-11-30)
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 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
73  std::vector<CvPoint2D32f> feature;
74 #else
75  std::vector<cv::Point2f> feature;
76 #endif
77  vpImagePoint ip;
78  do {
79  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
80  if (vpDisplay::getClick(I, ip, button, false)) {
81  if (button == vpMouseButton::button1) {
82  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
84  }
85  }
87  vpTime::wait(20);
88  } while (button != vpMouseButton::button3);
89 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
90  tracker.initTracking(cvI, &feature[0], feature.size());
91 #else
92  tracker.initTracking(cvI, feature);
93 #endif
94  } else {
96  tracker.initTracking(cvI);
98  }
99 
101  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
103 
105  while (!reader.end()) {
106  double t = vpTime::measureTimeMs();
107  reader.acquire(Iacq);
108  Iacq.subsample(opt_subsample, opt_subsample, I);
110 
111  vpImageConvert::convert(I, cvI);
112 
113  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
115 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
116  std::vector<CvPoint2D32f> feature;
117 #else
118  std::vector<cv::Point2f> feature;
119 #endif
120  vpImagePoint ip;
121  do {
122  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
123  if (vpDisplay::getClick(I, ip, button, false)) {
124  if (button == vpMouseButton::button1) {
125  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
127  }
128  }
129  vpDisplay::flush(I);
130  vpTime::wait(20);
131  } while (button != vpMouseButton::button3);
132 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
133  tracker.initTracking(cvI, &feature[0], feature.size());
134 #else
135  tracker.initTracking(cvI, feature);
136 #endif
137  }
138 
139  tracker.track(cvI);
140 
141  tracker.display(I, vpColor::red);
142 
143  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
144  if (vpDisplay::getClick(I, false))
145  break;
146 
147  vpDisplay::flush(I);
148  if (!reader.isVideoFormat()) {
149  vpTime::wait(t, 40);
150  }
151  }
153 
157  } catch (const vpException &e) {
158  std::cout << "Catch an exception: " << e << std::endl;
159  return EXIT_FAILURE;
160  }
161 #else
162  (void)argc;
163  (void)argv;
164 #endif
165  return EXIT_SUCCESS;
166 }
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:1462
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()