Visual Servoing Platform  version 3.6.1 under development (2025-03-12)
trackKltOpencv.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example of dot tracking.
32  */
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_DISPLAY)
44 
45 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
46 
47 #include <vector>
48 
49 #include <visp3/core/vpImage.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/gui/vpDisplayFactory.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/klt/vpKltOpencv.h>
55 
56 // List of allowed command line options
57 #define GETOPTARGS "cdf:i:l:p:s:h"
58 
59 #ifdef ENABLE_VISP_NAMESPACE
60 using namespace VISP_NAMESPACE_NAME;
61 #endif
62 
63 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
64  unsigned last, unsigned step);
65 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
66  unsigned &step, bool &click_allowed, bool &display);
88 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
89  unsigned last, unsigned step)
90 {
91 #if defined(VISP_HAVE_DATASET)
92 #if VISP_HAVE_DATASET_VERSION >= 0x030600
93  std::string ext("png");
94 #else
95  std::string ext("pgm");
96 #endif
97 #else
98  // We suppose that the user will download a recent dataset
99  std::string ext("png");
100 #endif
101  fprintf(stdout, "\n\
102 Example of KLT tracking using OpenCV library.\n\
103 \n\
104 SYNOPSIS\n\
105  %s [-i <test image path>] [-p <personal image path>]\n\
106  [-f <first image>] [-l <last image>] [-s <step>]\n\
107  [-c] [-d] [-h]\n",
108  name);
109 
110  fprintf(stdout, "\n\
111 OPTIONS: Default\n\
112  -i <input image path> %s\n\
113  Set image input path.\n\
114  From this path read images \n\
115  \"mire-2/image.%%04d.%s\". These \n\
116  images come from visp-images-x.y.z.tar.gz available \n\
117  on the ViSP website.\n\
118  Setting the VISP_INPUT_IMAGE_PATH environment\n\
119  variable produces the same behaviour than using\n\
120  this option.\n\
121  \n\
122  -p <personal image path> %s\n\
123  Specify a personal sequence containing images \n\
124  to process.\n\
125  By image sequence, we mean one file per image.\n\
126  Example : \"/Temp/visp-images/mire-2/image.%%04d.%s\"\n\
127  %%04d is for the image numbering.\n\
128  \n\
129  -f <first image> %u\n\
130  First image number of the sequence.\n\
131  \n\
132  -l <last image> %u\n\
133  Last image number of the sequence.\n\
134  \n\
135  -s <step> %u\n\
136  Step between two images.\n\
137 \n\
138  -c\n\
139  Disable the mouse click. Useful to automate the \n\
140  execution of this program without human intervention.\n\
141 \n\
142  -d \n\
143  Turn off the display.\n\
144 \n\
145  -h\n\
146  Print the help.\n",
147  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
148 
149  if (badparam)
150  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
151 }
152 
170 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
171  unsigned &step, bool &click_allowed, bool &display)
172 {
173  const char *optarg_;
174  int c;
175  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
176 
177  switch (c) {
178  case 'c':
179  click_allowed = false;
180  break;
181  case 'd':
182  display = false;
183  break;
184  case 'i':
185  ipath = optarg_;
186  break;
187  case 'p':
188  ppath = optarg_;
189  break;
190  case 'f':
191  first = (unsigned)atoi(optarg_);
192  break;
193  case 'l':
194  last = (unsigned)atoi(optarg_);
195  break;
196  case 's':
197  step = (unsigned)atoi(optarg_);
198  break;
199  case 'h':
200  usage(argv[0], nullptr, ipath, ppath, first, last, step);
201  return false;
202  break;
203 
204  default:
205  usage(argv[0], optarg_, ipath, ppath, first, last, step);
206  return false;
207  break;
208  }
209  }
210 
211  if ((c == 1) || (c == -1)) {
212  // standalone param or error
213  usage(argv[0], nullptr, ipath, ppath, first, last, step);
214  std::cerr << "ERROR: " << std::endl;
215  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
216  return false;
217  }
218 
219  return true;
220 }
221 
222 int main(int argc, const char **argv)
223 {
224  try {
225  std::string env_ipath;
226  std::string opt_ipath;
227  std::string ipath;
228  std::string opt_ppath;
229  std::string dirname;
230  std::string filename;
231  unsigned opt_first = 1;
232  unsigned opt_last = 500;
233  unsigned opt_step = 1;
234  bool opt_click_allowed = true;
235  bool opt_display = true;
236 
237 #if defined(VISP_HAVE_DATASET)
238 #if VISP_HAVE_DATASET_VERSION >= 0x030600
239  std::string ext("png");
240 #else
241  std::string ext("pgm");
242 #endif
243 #else
244  // We suppose that the user will download a recent dataset
245  std::string ext("png");
246 #endif
247 
248  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
249  // environment variable value
250  env_ipath = vpIoTools::getViSPImagesDataPath();
251 
252  // Set the default input path
253  if (!env_ipath.empty())
254  ipath = env_ipath;
255 
256  // Read the command line options
257  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
258  opt_display) == false) {
259  return EXIT_FAILURE;
260  }
261 
262  // Get the option values
263  if (!opt_ipath.empty())
264  ipath = opt_ipath;
265 
266  // Compare ipath and env_ipath. If they differ, we take into account
267  // the input path coming from the command line option
268  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
269  if (ipath != env_ipath) {
270  std::cout << std::endl << "WARNING: " << std::endl;
271  std::cout << " Since -i <visp image path=" << ipath << "> "
272  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
273  << " we skip the environment variable." << std::endl;
274  }
275  }
276 
277  // Test if an input path is set
278  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
279  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
280  std::cerr << std::endl << "ERROR:" << std::endl;
281  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
282  << " environment variable to specify the location of the " << std::endl
283  << " image path where test images are located." << std::endl
284  << " Use -p <personal image path> option if you want to " << std::endl
285  << " use personal images." << std::endl
286  << std::endl;
287 
288  return EXIT_FAILURE;
289  }
290 
291  // Declare an image, this is a gray level image (unsigned char)
292  // it size is not defined yet, it will be defined when the image will
293  // read on the disk
294  vpImage<unsigned char> vpI; // This is a ViSP image used for display only
295  cv::Mat cvI;
296  vpDisplay *display = nullptr;
297 
298  unsigned iter = opt_first;
299  std::ostringstream s;
300  char cfilename[FILENAME_MAX];
301 
302  if (opt_ppath.empty()) {
303 
304  // Warning :
305  // The image sequence is not provided with the ViSP package
306  // therefore the program will return an error :
307  // !! couldn't read file visp-images/mire-2/image.0001.png
308  //
309  // ViSP dataset is available on the visp www site
310  // https://visp.inria.fr/download/.
311 
312  // Set the path location of the image sequence
313  dirname = vpIoTools::createFilePath(ipath, "mire-2");
314 
315  // Build the name of the image file
316 
317  s.setf(std::ios::right, std::ios::adjustfield);
318  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
319  filename = vpIoTools::createFilePath(dirname, s.str());
320  }
321  else {
322  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
323  filename = cfilename;
324  }
325 
326  // Read the image named "filename", and put the bitmap into the image structure I.
327  // I is initialized to the correct size
328  //
329  // vpImageIo::read() may throw various exception if, for example,
330  // the file does not exist, or if the memory cannot be allocated
331  try {
332  std::cout << "Load: " << filename << std::endl;
333 
334  // Load a ViSP image used for the display
335  vpImageIo::read(vpI, filename);
336  vpImageConvert::convert(vpI, cvI);
337  }
338  catch (...) {
339  // If an exception is thrown by vpImageIo::read() it will result in the end of the program.
340  std::cerr << std::endl << "ERROR:" << std::endl;
341  std::cerr << " Cannot read " << filename << std::endl;
342  if (opt_ppath.empty()) {
343  std::cerr << " Check your -i " << ipath << " option " << std::endl
344  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
345  }
346  else {
347  std::cerr << " Check your -p " << opt_ppath << " option " << std::endl;
348  }
349  return EXIT_FAILURE;
350  }
351 
352 
353  if (opt_display) {
354  // We open a window using either X11, GTK, OpenCV or GDI.
356  // Display size is automatically defined by the image (I) size
357  display->init(vpI, 100, 100, "Display...");
358  // Display the image
359  // The image class has a member that specify a pointer toward
360  // the display that has been initialized in the display declaration
361  // therefore is is no longer necessary to make a reference to the
362  // display variable.
363  vpDisplay::display(vpI);
364  vpDisplay::flush(vpI);
365  }
366 
367  // KLT tracker
368  vpKltOpencv tracker;
369 
370  // Event manager
371  // tracker.setOnNewFeature(&newFeature);
372  // tracker.setOnFeatureLost(&lostFeature);
373  // tracker.setIsFeatureValid(&isValid);
374 
375  // Tracker parameters
376  tracker.setTrackerId(1);
377  // tracker.setOnMeasureFeature(&modifyFeature);
378  tracker.setMaxFeatures(200);
379  tracker.setWindowSize(10);
380  tracker.setQuality(0.01);
381  tracker.setMinDistance(15);
382  tracker.setHarrisFreeParameter(0.04);
383  tracker.setBlockSize(9);
384  tracker.setUseHarris(1);
385  tracker.setPyramidLevels(3);
386 
387  // Point detection using Harris. In input we have an OpenCV IPL image
388  tracker.initTracking(cvI);
389 
390  if (opt_display) {
391  // Plot the Harris points on ViSP image
392  tracker.display(vpI, vpColor::red);
393  }
394 
395  bool quit = false;
396 
397  // Tracking is now initialized. We can start the tracker.
398  while ((iter < opt_last) && (!quit)) {
399  // set the new image name
400  if (opt_ppath.empty()) {
401  s.str("");
402  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
403  filename = vpIoTools::createFilePath(dirname, s.str());
404  }
405  else {
406  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
407  filename = cfilename;
408  }
409  // read the image
410  vpImageIo::read(vpI, filename);
411  vpImageConvert::convert(vpI, cvI);
412 
413  // track the dot and returns its coordinates in the image
414  // results are given in float since many many are usually considered
415  //
416  // an exception is thrown by the track method if
417  // - dot is lost
418 
419  if (opt_display) {
420  // Display the image
421  vpDisplay::display(vpI);
422  }
423 
424  std::cout << "Tracking on image: " << filename << std::endl;
425  double time = vpTime::measureTimeMs();
426  // Tracking of the detected points
427  tracker.track(cvI);
428  std::cout << "Tracking performed in " << vpTime::measureTimeMs() - time << " ms" << std::endl;
429 
430  if (opt_display) {
431  // Display the tracked points
432  tracker.display(vpI, vpColor::red);
433  vpDisplay::displayText(vpI, 20, 20, "Click to quit...", vpColor::red);
434  if (vpDisplay::getClick(vpI, false)) {
435  quit = true;
436  }
437  vpDisplay::flush(vpI);
438  }
439  iter += opt_step;
440  }
441  if (opt_display && opt_click_allowed && !quit) {
442  std::cout << "\nA click to exit..." << std::endl;
443  // Wait for a blocking mouse click
444  vpDisplay::getClick(vpI);
445  }
446  if (display) {
447  delete display;
448  }
449  return EXIT_SUCCESS;
450  }
451  catch (const vpException &e) {
452  std::cout << "Catch an exception: " << e << std::endl;
453  return EXIT_FAILURE;
454  }
455 }
456 #else
457 int main()
458 {
459  std::cout << "You do not have OpenCV functionalities to display images..." << std::endl;
460  std::cout << "Tip:" << std::endl;
461  std::cout << "- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
462  return EXIT_SUCCESS;
463 }
464 #endif
465 #else
466 #include <iostream>
467 
468 int main()
469 {
470  std::cout << "visp_klt module or X11, GTK, GDI or OpenCV display functionalities are required..." << std::endl;
471 }
472 
473 #endif
static const vpColor red
Definition: vpColor.h:198
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1439
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)
void setTrackerId(int tid)
Definition: vpKltOpencv.h:365
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
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()