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