Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
trackMeEllipse.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Tracking of an ellipse.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpDebug.h>
53 
54 #include <iomanip>
55 #include <sstream>
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 #if defined(VISP_HAVE_MODULE_ME) && \
60  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
61 
62 #include <visp3/core/vpColor.h>
63 #include <visp3/core/vpImage.h>
64 #include <visp3/core/vpImagePoint.h>
65 #include <visp3/gui/vpDisplayGDI.h>
66 #include <visp3/gui/vpDisplayGTK.h>
67 #include <visp3/gui/vpDisplayOpenCV.h>
68 #include <visp3/gui/vpDisplayX.h>
69 #include <visp3/io/vpImageIo.h>
70 
71 #include <visp3/core/vpIoTools.h>
72 #include <visp3/io/vpParseArgv.h>
73 #include <visp3/me/vpMeEllipse.h>
74 
75 // List of allowed command line options
76 #define GETOPTARGS "acdi:h"
77 
78 void usage(const char *name, const char *badparam, std::string ipath);
79 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
80 
90 void usage(const char *name, const char *badparam, std::string ipath)
91 {
92  fprintf(stdout, "\n\
93 Example of ellipse or arc of ellipse tracking using vpMeEllipse.\n\
94 \n\
95 SYNOPSIS\n\
96  %s [-i <input image path>] [-c] [-a] [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  -i <input image path> %s\n\
101  Set image input path.\n\
102  From this path read images \n\
103  \"ellipse-1/image.%%04d.pgm\"\n\
104  Setting the VISP_INPUT_IMAGE_PATH environment\n\
105  variable produces the same behaviour than using\n\
106  this option.\n\
107 \n\
108  -c\n\
109  Disable the mouse click. Useful to automaze the \n\
110  execution of this program without humain intervention.\n\
111 \n\
112  -d \n\
113  Turn off the display.\n\
114 \n\
115  -a \n\
116  Enable arc of ellipse tracking.\n\
117 \n\
118  -h\n\
119  Print the help.\n", ipath.c_str());
120 
121  if (badparam)
122  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123 }
138 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display, bool &trackArc)
139 {
140  const char *optarg_;
141  int c;
142  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
143 
144  switch (c) {
145  case 'a':
146  trackArc = true;
147  break;
148  case 'c':
149  click_allowed = false;
150  break;
151  case 'd':
152  display = false;
153  break;
154  case 'i':
155  ipath = optarg_;
156  break;
157  case 'h':
158  usage(argv[0], NULL, ipath);
159  return false;
160  break;
161 
162  default:
163  usage(argv[0], optarg_, ipath);
164  return false;
165  break;
166  }
167  }
168 
169  if ((c == 1) || (c == -1)) {
170  // standalone param or error
171  usage(argv[0], NULL, ipath);
172  std::cerr << "ERROR: " << std::endl;
173  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
174  return false;
175  }
176 
177  return true;
178 }
179 
180 int main(int argc, const char **argv)
181 {
182 #if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
183  try {
184  std::string env_ipath;
185  std::string opt_ipath;
186  std::string ipath;
187  std::string dirname;
188  std::string filename;
189  bool opt_click_allowed = true;
190  bool opt_display = true;
191  bool opt_track_arc = false;
192 
193  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
194  // environment variable value
195  env_ipath = vpIoTools::getViSPImagesDataPath();
196 
197  // Set the default input path
198  if (!env_ipath.empty())
199  ipath = env_ipath;
200 
201  // Read the command line options
202  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display, opt_track_arc) == false) {
203  exit(-1);
204  }
205 
206  // Get the option values
207  if (!opt_ipath.empty())
208  ipath = opt_ipath;
209 
210  // Compare ipath and env_ipath. If they differ, we take into account
211  // the input path comming from the command line option
212  if (!opt_ipath.empty() && !env_ipath.empty()) {
213  if (ipath != env_ipath) {
214  std::cout << std::endl << "WARNING: " << std::endl;
215  std::cout << " Since -i <visp image path=" << ipath << "> "
216  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
217  << " we skip the environment variable." << std::endl;
218  }
219  }
220 
221  // Test if an input path is set
222  if (opt_ipath.empty() && env_ipath.empty()) {
223  usage(argv[0], NULL, ipath);
224  std::cerr << std::endl << "ERROR:" << std::endl;
225  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
226  << " environment variable to specify the location of the " << std::endl
227  << " image path where test images are located." << std::endl
228  << std::endl;
229  exit(-1);
230  }
231 
232  // Declare an image, this is a gray level image (unsigned char)
233  // it size is not defined yet, it will be defined when the image is
234  // read on the disk
236 
237  // Set the path location of the image sequence
238  dirname = vpIoTools::createFilePath(ipath, "ellipse-1");
239 
240  // Build the name of the image file
241  unsigned int iter = 1; // Image number
242  std::ostringstream s;
243  s.setf(std::ios::right, std::ios::adjustfield);
244  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
245  filename = vpIoTools::createFilePath(dirname, s.str());
246 
247  // Read the PGM image named "filename" on the disk, and put the
248  // bitmap into the image structure I. I is initialized to the
249  // correct size
250  //
251  // exception readPGM may throw various exception if, for example,
252  // the file does not exist, or if the memory cannot be allocated
253  try {
254  vpCTRACE << "Load: " << filename << std::endl;
255 
256  vpImageIo::read(I, filename);
257  } catch (...) {
258  // an exception is thrown if an exception from readPGM has been caught
259  // here this will result in the end of the program
260  // Note that another error message has been printed from readPGM
261  // to give more information about the error
262  std::cerr << std::endl << "ERROR:" << std::endl;
263  std::cerr << " Cannot read " << filename << std::endl;
264  std::cerr << " Check your -i " << ipath << " option " << std::endl
265  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
266  exit(-1);
267  }
268 
269 // We open a window using either X11, GTK or GDI.
270 #if defined VISP_HAVE_X11
271  vpDisplayX display;
272 #elif defined VISP_HAVE_GTK
273  vpDisplayGTK display;
274 #elif defined VISP_HAVE_GDI
275  vpDisplayGDI display;
276 #elif defined VISP_HAVE_OPENCV
277  vpDisplayOpenCV display;
278 #endif
279 
280  if (opt_display) {
281  // Display size is automatically defined by the image (I) size
282  display.init(I, 100, 100, "Display...");
283  // Display the image
284  // The image class has a member that specify a pointer toward
285  // the display that has been initialized in the display declaration
286  // therefore is is no longuer necessary to make a reference to the
287  // display variable.
289  vpDisplay::flush(I);
290  }
291 
292  vpMeEllipse me_ellipse;
293 
294  vpMe me;
295  me.setRange(20);
296  me.setSampleStep(10);
297  me.setThreshold(15000);
298 
299  me_ellipse.setMe(&me);
301  if (opt_click_allowed)
302  me_ellipse.initTracking(I, opt_track_arc);
303  else {
304  // Create a list of clockwise points to automate the test
305  std::vector<vpImagePoint> ip;
306  ip.push_back(vpImagePoint(195, 329));
307  ip.push_back(vpImagePoint(243, 164));
308  ip.push_back(vpImagePoint(201, 36));
309  ip.push_back(vpImagePoint(83, 126));
310  ip.push_back(vpImagePoint(33, 276));
311 
312  me_ellipse.initTracking(I, ip, opt_track_arc);
313  }
314  if (opt_display) {
315  me_ellipse.display(I, vpColor::green);
316  }
317 
318  vpERROR_TRACE("sample step %f ", me_ellipse.getMe()->getSampleStep());
319  if (opt_display && opt_click_allowed) {
320  std::cout << "A click to continue..." << std::endl;
322  }
323  std::cout << "------------------------------------------------------------" << std::endl;
324 
325  for (iter = 1; iter < 51; iter++) // initially : iter < 1500
326  {
327  // set the new image name
328  s.str("");
329  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
330  filename = vpIoTools::createFilePath(dirname, s.str());
331  std::cout << "Tracking on image: " << filename << std::endl;
332  // read the image
333  vpImageIo::read(I, filename);
334  if (opt_display) {
335  // Display the image
337  }
338 
339  me_ellipse.track(I);
340 
341  if (opt_display) {
342  me_ellipse.display(I, vpColor::green);
343  vpDisplay::flush(I);
344  }
345  }
346  if (opt_display && opt_click_allowed) {
347  std::cout << "A click to exit..." << std::endl;
349  }
350  return EXIT_SUCCESS;
351  } catch (const vpException &e) {
352  std::cout << "Catch an exception: " << e << std::endl;
353  return EXIT_FAILURE;
354  }
355 #else
356  (void)argc;
357  (void)argv;
358  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
359 #endif
360 }
361 #else
362 #include <iostream>
363 
364 int main()
365 {
366  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
367  "functionalities are required..."
368  << std::endl;
369  return EXIT_SUCCESS;
370 }
371 
372 #endif
void track(const vpImage< unsigned char > &I)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:149
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:278
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:134
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that tracks an ellipse using moving edges.
Definition: vpMeEllipse.h:97
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpMe.h:60
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void initTracking(const vpImage< unsigned char > &I, bool trackArc=false)
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670
double getSampleStep() const
Definition: vpMe.h:285
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:152
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 display(const vpImage< unsigned char > &I, vpColor col)
vpMe * getMe()
Definition: vpMeTracker.h:120
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
#define vpCTRACE
Definition: vpDebug.h:338
void setThreshold(const double &t)
Definition: vpMe.h:300
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:173