Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
trackMeEllipse.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  * Tracking of an ellipse.
32  */
33 
45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpDebug.h>
47 
48 #include <iomanip>
49 #include <sstream>
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #if defined(VISP_HAVE_MODULE_ME) && \
54  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
55 
56 #include <visp3/core/vpColor.h>
57 #include <visp3/core/vpImage.h>
58 #include <visp3/core/vpImagePoint.h>
59 #include <visp3/core/vpIoTools.h>
60 #include <visp3/gui/vpDisplayGDI.h>
61 #include <visp3/gui/vpDisplayGTK.h>
62 #include <visp3/gui/vpDisplayOpenCV.h>
63 #include <visp3/gui/vpDisplayX.h>
64 #include <visp3/io/vpParseArgv.h>
65 #include <visp3/io/vpVideoReader.h>
66 #include <visp3/io/vpVideoWriter.h>
67 #include <visp3/me/vpMeEllipse.h>
68 
69 // List of allowed command line options
70 #define GETOPTARGS "Aabcdf:hi:l:p:r:s:S:t:vw:"
71 
72 #ifdef ENABLE_VISP_NAMESPACE
73 using namespace VISP_NAMESPACE_NAME;
74 #endif
75 
90 void usage(const char *name, const char *badparam, const std::string &video_in_ipath, const std::string &video_in_ppath,
91  unsigned video_in_first, int video_in_last, int video_in_step, int me_range, int me_sample_step, int me_threshold)
92 {
93 #if VISP_HAVE_DATASET_VERSION >= 0x030600
94  std::string ext("png");
95 #else
96  std::string ext("pgm");
97 #endif
98  fprintf(stdout, "\n\
99 Example of ellipse/circle or arc of ellipse/circle tracking using vpMeEllipse.\n\
100 \n\
101 SYNOPSIS\n\
102  %s [-i <visp dataset directory>] [-p <personal image path>]\n\
103  [-f <video first image>] [-l <video last image>] [-s <video step>]\n\
104  [-r <moving-edge range] [-t <moving-edge threshold] [-S <moving-edge sample step>]\n\
105  [-w <output images sequence name>] \n\
106  [-c] [-d] [-a] [-A] [-b] [-v] [-h]\n",
107  name);
108 
109  fprintf(stdout, "\n\
110 OPTIONS: Default\n\
111  -i <visp dataset directory> %s\n\
112  Set visp dataset directory location.\n\
113  From this directory read images \n\
114  \"ellipse-1/image.%%04d.%s\"\n\
115  Setting the VISP_INPUT_IMAGE_PATH environment\n\
116  variable produces the same behaviour than using\n\
117  this option.\n\
118  \n\
119  -p <personal image path> %s\n\
120  Specify a personal sequence containing images \n\
121  to process.\n\
122  The format is selected by analyzing \n\
123  the filename extension.\n\
124  Example : \"C:/Temp/ViSP-images/ellipse-1/image.%%04d.%s\"\n\
125  %%04d is for the image numbering.\n\
126  \n\
127  -f <video first image> %d\n\
128  First image number to process.\n\
129  Set -1 to process the first image of the sequence.\n\
130  \n\
131  -l <video last image> %d\n\
132  Last image number to process. \n\
133  Set -1 to process images until the last image\n\
134  of the sequence.\n\
135  \n\
136  -s <video step> %d\n\
137  Step between two images.\n\
138  \n\
139  -r <moving-edge range> %d\n\
140  Moving-edge range.\n\
141  Increase value to consider large displacement. \n\
142  When set to -1, use default value. \n\
143  \n\
144  -S <moving-edge sample step> %d\n\
145  Moving-edge sample step.\n\
146  Distance between two moving-edges samples in degrees. \n\
147  When set to -1, use default value. \n\
148  \n\
149  -t <moving-edge threshold> %d\n\
150  Moving-edge threshold corresponding to the minimum \n\
151  contrast to consider. Value in range [0 ; 255] \n\
152  When set to -1, use default value. \n\
153  \n\
154  -c\n\
155  Disable the mouse click. Useful to automate the \n\
156  execution of this program without human intervention.\n\
157  \n\
158  -d \n\
159  Turn off the display.\n\
160  \n\
161  -a \n\
162  Enable arc of ellipse tracking.\n\
163  \n\
164  -b \n\
165  Enable circle tracking.\n\
166  \n\
167  -w <output images sequence name> \n\
168  Save images with tracking results in overlay.\n\
169  Example: \"result/I%%04d.png\" \n\
170  \n\
171  -A \n\
172  When display is activated using -d option, enable\n\
173  windows auto scaling to fit the screen size. \n\
174  \n\
175  -v\n\
176  Enable verbosity.\n\
177  \n\
178  -h\n\
179  Print the help.\n",
180  video_in_ipath.c_str(), ext.c_str(), video_in_ppath.c_str(), ext.c_str(), video_in_first, video_in_last,
181  video_in_step, me_range, me_sample_step, me_threshold);
182 
183  if (badparam)
184  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
185 }
186 
212 bool getOptions(int argc, const char **argv, std::string &video_in_ipath, std::string &video_in_ppath,
213  int &video_in_first, int &video_in_last, int &video_in_step,
214  bool &click_allowed, bool &display, bool &display_scale_auto, bool &track_circle, bool &track_arc,
215  std::string &video_out_save, int &me_range, int &me_sample_step, int &me_threshold, bool &verbose)
216 {
217  const char *optarg_;
218  int c;
219  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
220 
221  switch (c) {
222  case 'A':
223  display_scale_auto = true;
224  break;
225  case 'a':
226  track_arc = true;
227  break;
228  case 'b':
229  track_circle = true;
230  break;
231  case 'c':
232  click_allowed = false;
233  break;
234  case 'd':
235  display = false;
236  break;
237  case 'f':
238  video_in_first = atoi(optarg_);
239  break;
240  case 'i':
241  video_in_ipath = std::string(optarg_);
242  break;
243  case 'l':
244  video_in_last = atoi(optarg_);
245  break;
246  case 'p':
247  video_in_ppath = std::string(optarg_);
248  break;
249  case 'r':
250  me_range = atoi(optarg_);
251  break;
252  case 's':
253  video_in_step = atoi(optarg_);
254  break;
255  case 'S':
256  me_sample_step = atoi(optarg_);
257  break;
258  case 't':
259  me_threshold = atoi(optarg_);
260  break;
261  case 'w':
262  video_out_save = std::string(optarg_);
263  break;
264  case 'v':
265  verbose = true;
266  break;
267  case 'h':
268  usage(argv[0], nullptr, video_in_ipath, video_in_ppath, video_in_first, video_in_last, video_in_step, me_range, me_sample_step, me_threshold);
269  return false;
270  break;
271 
272  default:
273  usage(argv[0], optarg_, video_in_ipath, video_in_ppath, video_in_first, video_in_last, video_in_step, me_range, me_sample_step, me_threshold);
274  return false;
275  break;
276  }
277  }
278 
279  if ((c == 1) || (c == -1)) {
280  // standalone param or error
281  usage(argv[0], nullptr, video_in_ipath, video_in_ppath, video_in_first, video_in_last, video_in_step, me_range, me_sample_step, me_threshold);
282  std::cerr << "ERROR: " << std::endl;
283  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
284  return false;
285  }
286 
287  return true;
288 }
289 
290 int main(int argc, const char **argv)
291 {
292 #if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
293  std::string env_ipath;
294  std::string opt_ipath;
295  std::string ipath;
296  std::string opt_ppath;
297  std::string videoname;
298  int opt_first = -1;
299  int opt_last = -1;
300  int opt_step = 1;
301  int opt_me_range = 30;
302  int opt_me_sample_step = 5;
303  int opt_me_threshold = 20; // Value in [0 ; 255]
304  bool opt_click_allowed = true;
305  bool opt_display = true;
306  bool opt_display_scale_auto = false;
307  bool opt_track_circle = false;
308  bool opt_track_arc = false;
309  bool opt_verbose = false;
310  std::string opt_save;
311  unsigned int thickness = 1;
312 
313  // Declare an image, this is a gray level image (unsigned char)
314  // it size is not defined yet, it will be defined when the image is
315  // read on the disk
317  vpDisplay *display = nullptr;
318  vpVideoReader g;
319 
320  try {
321  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
322  // environment variable value
323  env_ipath = vpIoTools::getViSPImagesDataPath();
324 
325  // Set the default input path
326  if (!env_ipath.empty())
327  ipath = env_ipath;
328 
329  // Read the command line options
330  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
331  opt_display, opt_display_scale_auto, opt_track_circle, opt_track_arc, opt_save,
332  opt_me_range, opt_me_sample_step, opt_me_threshold, opt_verbose) == false) {
333  return EXIT_FAILURE;
334  }
335 
336  // Get the option values
337  if (!opt_ipath.empty()) {
338  ipath = opt_ipath;
339  }
340 
341  // Compare ipath and env_ipath. If they differ, we take into account
342  // the input path coming from the command line option
343  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
344  if (ipath != env_ipath) {
345  std::cout << std::endl << "WARNING: " << std::endl;
346  std::cout << " Since -i <visp image path=" << ipath << "> "
347  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
348  << " we skip the environment variable." << std::endl;
349  }
350  }
351 
352  // Test if an input path is set
353  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
354  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step, opt_me_range, opt_me_sample_step, opt_me_threshold);
355  std::cerr << std::endl << "ERROR:" << std::endl;
356  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
357  << " environment variable to specify the location of the " << std::endl
358  << " image path where test images are located." << std::endl
359  << " Use -p <personal image path> option if you want to " << std::endl
360  << " use personal images." << std::endl
361  << std::endl;
362 
363  return EXIT_FAILURE;
364  }
365 
366  // Create output folder if needed
367  if (!opt_save.empty()) {
368  std::string parent = vpIoTools::getParent(opt_save);
369  if (!parent.empty()) {
370  std::cout << "Create output directory: " << parent << std::endl;
371  vpIoTools::makeDirectory(parent);
372  }
373  thickness += 1;
374  }
375 
376  if (opt_ppath.empty()) {
377  // Set the path location of the image sequence
378 #if VISP_HAVE_DATASET_VERSION >= 0x030600
379  videoname = vpIoTools::createFilePath(ipath, "ellipse-1/image.%04d.png");
380 #else
381  videoname = vpIoTools::createFilePath(ipath, "ellipse-1/image.%04d.pgm");
382 #endif
383  g.setFileName(videoname);
384  }
385  else {
386  g.setFileName(opt_ppath);
387  }
388 
389  if (opt_first > 0) {
390  g.setFirstFrameIndex(opt_first);
391  }
392  if (opt_last > 0) {
393  g.setLastFrameIndex(opt_last);
394  }
395  g.setFrameStep(opt_step);
396  g.open(I);
397 
398  if (opt_display) {
399  // We open a window using either X11, GTK, GDI or OpenCV
400 #if defined(VISP_HAVE_X11)
401  display = new vpDisplayX;
402 #elif defined(VISP_HAVE_GTK)
403  display = new vpDisplayGTK;
404 #elif defined(VISP_HAVE_GDI)
405  display = new vpDisplayGDI;
406 #elif defined(HAVE_OPENCV_HIGHGUI)
407  display = new vpDisplayOpenCV;
408 #endif
409  if (opt_display_scale_auto) {
410  display->setDownScalingFactor(vpDisplay::SCALE_AUTO);
411  }
412  // Display size is automatically defined by the image (I) size
413  display->init(I, 10, 10, "Current image");
414  // Display the image
415  // The image class has a member that specify a pointer toward
416  // the display that has been initialized in the display declaration
417  // therefore is is no longer necessary to make a reference to the
418  // display variable.
420  vpDisplay::flush(I);
421  }
422 
423  vpVideoWriter *writer = nullptr;
424  vpImage<vpRGBa> O;
425  if (!opt_save.empty()) {
426  writer = new vpVideoWriter();
427  writer->setFileName(opt_save);
428  writer->open(O);
429  }
430  vpMeEllipse me_ellipse;
431 
432  vpMe me;
433  if (opt_me_range > 0) {
434  me.setRange(opt_me_range);
435  }
436  if (opt_me_sample_step > 0) {
437  me.setSampleStep(opt_me_sample_step);
438  }
439  if (opt_me_threshold > 0) {
441  me.setThreshold(opt_me_threshold);
442  }
443 
444  me_ellipse.setMe(&me);
446 
447  std::cout << "Video settings" << std::endl;
448  std::cout << " Name : " << g.getFrameName() << std::endl;
449  std::cout << " First image: " << g.getFirstFrameIndex() << std::endl;
450  std::cout << " Last image : " << g.getLastFrameIndex() << std::endl;
451  std::cout << " Step : " << g.getFrameStep() << std::endl;
452  std::cout << " Image size : " << I.getWidth() << " x " << I.getHeight() << std::endl;
453 
454  std::cout << "Moving-edges settings" << std::endl;
455  std::cout << " Sample step : " << me_ellipse.getMe()->getSampleStep() << std::endl;
456  std::cout << " Range : " << me_ellipse.getMe()->getRange() << std::endl;
457  std::cout << " Threshold type: " << (me_ellipse.getMe()->getLikelihoodThresholdType() == vpMe::NORMALIZED_THRESHOLD ? "normalized" : "old threshold (to be avoided)") << std::endl;
458  std::cout << " Threshold : " << me_ellipse.getMe()->getThreshold() << std::endl;
459 
460  if (opt_click_allowed) {
461  me_ellipse.initTracking(I, opt_track_circle, opt_track_arc);
462  }
463  else {
464  // Create a list of clockwise points to automate the test
465  std::vector<vpImagePoint> ip;
466  ip.push_back(vpImagePoint(195, 329));
467  ip.push_back(vpImagePoint(243, 164));
468  ip.push_back(vpImagePoint(201, 36));
469  ip.push_back(vpImagePoint(83, 126));
470  ip.push_back(vpImagePoint(33, 276));
471 
472  me_ellipse.initTracking(I, ip, opt_track_circle, opt_track_arc);
473  }
474  if (opt_display) {
475  me_ellipse.display(I, vpColor::green, thickness);
476  }
477 
478  if (opt_display && opt_click_allowed) {
479  std::cout << "A click to continue..." << std::endl;
481  }
482  bool quit = false;
483 
484  while (!g.end() && !quit) {
485  // Read the image
486  g.acquire(I);
487  std::stringstream ss;
488  ss << "Process image " << g.getFrameIndex();
489  if (opt_verbose) {
490  std::cout << "-- " << ss.str() << std::endl;
491  }
492  if (opt_display) {
493  // Display the image
495  vpDisplay::displayText(I, 20, 10, ss.str(), vpColor::red);
496  if (opt_click_allowed) {
497  vpDisplay::displayText(I, 40, 10, "Click to exit...", vpColor::red);
498  }
499  }
500  me_ellipse.track(I);
501 
502  if (opt_display) {
503  me_ellipse.display(I, vpColor::green, thickness);
504  vpDisplay::flush(I);
505  }
506  if (!opt_save.empty()) {
507  vpDisplay::getImage(I, O);
508  writer->saveFrame(O);
509  }
510  if (opt_display && opt_click_allowed) {
511  if (vpDisplay::getClick(I, false)) {
512  quit = true;
513  }
514  }
515  }
516 
517  if (opt_display && opt_click_allowed && !quit) {
519  }
520 
521  if (writer) {
522  delete writer;
523  }
524  if (display) {
525  delete display;
526  }
527  return EXIT_SUCCESS;
528  }
529  catch (const vpException &e) {
530  std::cout << "Catch an exception: " << e << std::endl;
531  if (opt_display && opt_click_allowed) {
533  }
534  return EXIT_FAILURE;
535  }
536 #else
537  (void)argc;
538  (void)argv;
539  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
540 #endif
541 }
542 #else
543 #include <iostream>
544 
545 int main()
546 {
547  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
548  "functionalities are required..."
549  << std::endl;
550  return EXIT_SUCCESS;
551 }
552 
553 #endif
static const vpColor red
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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 getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:140
static void flush(const vpImage< unsigned char > &I)
@ SCALE_AUTO
Definition: vpDisplay.h:184
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
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
Class that tracks an ellipse using moving edges.
Definition: vpMeEllipse.h:96
void display(const vpImage< unsigned char > &I, const vpColor &col, unsigned int thickness=1)
void initTracking(const vpImage< unsigned char > &I, bool trackCircle=false, bool trackArc=false)
void track(const vpImage< unsigned char > &I)
@ RANGE_RESULT
Definition: vpMeSite.h:78
vpMe * getMe()
Definition: vpMeTracker.h:153
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:232
void setMe(vpMe *me)
Definition: vpMeTracker.h:260
Definition: vpMe.h:134
vpLikelihoodThresholdType getLikelihoodThresholdType() const
Definition: vpMe.h:327
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
double getThreshold() const
Definition: vpMe.h:291
double getSampleStep() const
Definition: vpMe.h:275
unsigned int getRange() const
Definition: vpMe.h:268
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void setLastFrameIndex(const long last_frame)
long getLastFrameIndex()
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
long getFirstFrameIndex()
void setFrameStep(const long frame_step)
long getFrameStep() const
std::string getFrameName() const
long getFrameIndex() const
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)