Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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 #ifdef VISP_HAVE_OPENCV
13  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 0;
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 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
46  IplImage *cvI = NULL;
47 #else
48  cv::Mat cvI;
49 #endif
52 
54  vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
58 
60  vpKltOpencv tracker;
61  tracker.setMaxFeatures(200);
62  tracker.setWindowSize(10);
64  tracker.setQuality(0.01);
66  tracker.setMinDistance(15);
67  tracker.setHarrisFreeParameter(0.04);
68  tracker.setBlockSize(9);
69  tracker.setUseHarris(1);
70  tracker.setPyramidLevels(3);
72 
73  // Initialise the tracking
74  if (opt_init_by_click) {
76 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
77  std::vector<CvPoint2D32f> feature;
78 #else
79  std::vector<cv::Point2f> feature;
80 #endif
81  vpImagePoint ip;
82  do {
83  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
84  if (vpDisplay::getClick(I, ip, button, false)) {
85  if (button == vpMouseButton::button1) {
86  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
88  }
89  }
91  vpTime::wait(20);
92  } while (button != vpMouseButton::button3);
93 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
94  tracker.initTracking(cvI, &feature[0], feature.size());
95 #else
96  tracker.initTracking(cvI, feature);
97 #endif
98  } else {
100  tracker.initTracking(cvI);
102  }
103 
105  std::cout << "Tracker initialized with " << tracker.getNbFeatures() << " features" << std::endl;
107 
109  while (!reader.end()) {
110  double t = vpTime::measureTimeMs();
111  reader.acquire(Iacq);
112  Iacq.subsample(opt_subsample, opt_subsample, I);
114 
115  vpImageConvert::convert(I, cvI);
116 
117  if (opt_init_by_click && reader.getFrameIndex() == reader.getFirstFrameIndex() + 20) {
119 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
120  std::vector<CvPoint2D32f> feature;
121 #else
122  std::vector<cv::Point2f> feature;
123 #endif
124  vpImagePoint ip;
125  do {
126  vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
127  if (vpDisplay::getClick(I, ip, button, false)) {
128  if (button == vpMouseButton::button1) {
129  feature.push_back(cv::Point2f((float)ip.get_u(), (float)ip.get_v()));
131  }
132  }
133  vpDisplay::flush(I);
134  vpTime::wait(20);
135  } while (button != vpMouseButton::button3);
136 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
137  tracker.initTracking(cvI, &feature[0], feature.size());
138 #else
139  tracker.initTracking(cvI, feature);
140 #endif
141  }
142 
143  tracker.track(cvI);
144 
145  tracker.display(I, vpColor::red);
146 
147  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
148  if (vpDisplay::getClick(I, false))
149  break;
150 
151  vpDisplay::flush(I);
152  if (! reader.isVideoFormat()) {
153  vpTime::wait(t, 40);
154  }
155  }
157 
161 
163 #if (VISP_HAVE_OPENCV_VERSION < 0x020408)
164  cvReleaseImage(&cvI);
165 #endif
166 
168  return 0;
169  } catch (const vpException &e) {
170  std::cout << "Catch an exception: " << e << std::endl;
171  }
172 #else
173  (void)argc;
174  (void)argv;
175 #endif
176 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
void setMaxFeatures(int maxCount)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setHarrisFreeParameter(double harris_k)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition: vpImage.h:1220
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
Definition: vpException.h:71
int getNbFeatures() const
Get the number of current features.
Definition: vpKltOpencv.h:120
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
long getFirstFrameIndex()
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:217
void display(const vpImage< unsigned char > &I, const vpColor &color=vpColor::red, unsigned int thickness=1)
void setQuality(double qualityLevel)
double get_u() const
Definition: vpImagePoint.h:262
bool isVideoFormat() const
void setPyramidLevels(int pyrMaxLevel)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void acquire(vpImage< vpRGBa > &I)
void setWindowSize(int winSize)
void initTracking(const cv::Mat &I, const cv::Mat &mask=cv::Mat())
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
long getFrameIndex() const
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:78
void setUseHarris(int useHarrisDetector)
void setFileName(const std::string &filename)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
void setBlockSize(int blockSize)
double get_v() const
Definition: vpImagePoint.h:273
void track(const cv::Mat &I)