Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 "cdi: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 Test of ellipse tracking using vpMeEllipse.\n\
94 \n\
95 SYNOPSIS\n\
96  %s [-i <input image path>] [-c] [-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  -h\n\
116  Print the help.\n", ipath.c_str());
117 
118  if (badparam)
119  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
120 }
134 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
135 {
136  const char *optarg_;
137  int c;
138  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
139 
140  switch (c) {
141  case 'c':
142  click_allowed = false;
143  break;
144  case 'd':
145  display = false;
146  break;
147  case 'i':
148  ipath = optarg_;
149  break;
150  case 'h':
151  usage(argv[0], NULL, ipath);
152  return false;
153  break;
154 
155  default:
156  usage(argv[0], optarg_, ipath);
157  return false;
158  break;
159  }
160  }
161 
162  if ((c == 1) || (c == -1)) {
163  // standalone param or error
164  usage(argv[0], NULL, ipath);
165  std::cerr << "ERROR: " << std::endl;
166  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
167  return false;
168  }
169 
170  return true;
171 }
172 
173 int main(int argc, const char **argv)
174 {
175  try {
176  std::string env_ipath;
177  std::string opt_ipath;
178  std::string ipath;
179  std::string dirname;
180  std::string filename;
181  bool opt_click_allowed = true;
182  bool opt_display = true;
183 
184  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
185  // environment variable value
186  env_ipath = vpIoTools::getViSPImagesDataPath();
187 
188  // Set the default input path
189  if (!env_ipath.empty())
190  ipath = env_ipath;
191 
192  // Read the command line options
193  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
194  exit(-1);
195  }
196 
197  // Get the option values
198  if (!opt_ipath.empty())
199  ipath = opt_ipath;
200 
201  // Compare ipath and env_ipath. If they differ, we take into account
202  // the input path comming from the command line option
203  if (!opt_ipath.empty() && !env_ipath.empty()) {
204  if (ipath != env_ipath) {
205  std::cout << std::endl << "WARNING: " << std::endl;
206  std::cout << " Since -i <visp image path=" << ipath << "> "
207  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
208  << " we skip the environment variable." << std::endl;
209  }
210  }
211 
212  // Test if an input path is set
213  if (opt_ipath.empty() && env_ipath.empty()) {
214  usage(argv[0], NULL, ipath);
215  std::cerr << std::endl << "ERROR:" << std::endl;
216  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
217  << " environment variable to specify the location of the " << std::endl
218  << " image path where test images are located." << std::endl
219  << std::endl;
220  exit(-1);
221  }
222 
223  // Declare an image, this is a gray level image (unsigned char)
224  // it size is not defined yet, it will be defined when the image is
225  // read on the disk
227 
228  // Set the path location of the image sequence
229  dirname = vpIoTools::createFilePath(ipath, "ellipse-1");
230 
231  // Build the name of the image file
232  unsigned int iter = 1; // Image number
233  std::ostringstream s;
234  s.setf(std::ios::right, std::ios::adjustfield);
235  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
236  filename = vpIoTools::createFilePath(dirname, s.str());
237 
238  // Read the PGM image named "filename" on the disk, and put the
239  // bitmap into the image structure I. I is initialized to the
240  // correct size
241  //
242  // exception readPGM may throw various exception if, for example,
243  // the file does not exist, or if the memory cannot be allocated
244  try {
245  vpCTRACE << "Load: " << filename << std::endl;
246 
247  vpImageIo::read(I, filename);
248  } catch (...) {
249  // an exception is thrown if an exception from readPGM has been caught
250  // here this will result in the end of the program
251  // Note that another error message has been printed from readPGM
252  // to give more information about the error
253  std::cerr << std::endl << "ERROR:" << std::endl;
254  std::cerr << " Cannot read " << filename << std::endl;
255  std::cerr << " Check your -i " << ipath << " option " << std::endl
256  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
257  exit(-1);
258  }
259 
260 // We open a window using either X11, GTK or GDI.
261 #if defined VISP_HAVE_X11
262  vpDisplayX display;
263 #elif defined VISP_HAVE_GTK
264  vpDisplayGTK display;
265 #elif defined VISP_HAVE_GDI
266  vpDisplayGDI display;
267 #elif defined VISP_HAVE_OPENCV
268  vpDisplayOpenCV display;
269 #endif
270 
271  if (opt_display) {
272  // Display size is automatically defined by the image (I) size
273  display.init(I, 100, 100, "Display...");
274  // Display the image
275  // The image class has a member that specify a pointer toward
276  // the display that has been initialized in the display declaration
277  // therefore is is no longuer necessary to make a reference to the
278  // display variable.
280  vpDisplay::flush(I);
281  }
282 
283  vpMeEllipse E1;
284 
285  vpMe me;
286  me.setRange(20);
287  me.setSampleStep(2);
288  me.setPointsToTrack(60);
289  me.setThreshold(15000);
290 
291  E1.setMe(&me);
293  if (opt_click_allowed)
294  E1.initTracking(I);
295  else {
296  // Create a list of points to automate the test
297  std::vector<vpImagePoint> ip;
298  ip.push_back(vpImagePoint(33, 276));
299  ip.push_back(vpImagePoint(83, 126));
300  ip.push_back(vpImagePoint(201, 36));
301  ip.push_back(vpImagePoint(243, 164));
302  ip.push_back(vpImagePoint(195, 329));
303 
304  E1.initTracking(I, ip);
305  }
306  if (opt_display) {
307  E1.display(I, vpColor::green);
308  }
309 
310  vpERROR_TRACE("sample step %f ", E1.getMe()->getSampleStep());
311  E1.track(I);
312  if (opt_display && opt_click_allowed) {
313  std::cout << "A click to continue..." << std::endl;
315  }
316  std::cout << "------------------------------------------------------------" << std::endl;
317 
318  for (iter = 1; iter < 51; iter++) // initially : iter < 1500
319  {
320  // set the new image name
321  s.str("");
322  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
323  filename = vpIoTools::createFilePath(dirname, s.str());
324  std::cout << "Tracking on image: " << filename << std::endl;
325  // read the image
326  vpImageIo::read(I, filename);
327  if (opt_display) {
328  // Display the image
330  }
331 
332  E1.track(I);
333 
334  if (opt_display) {
335  E1.display(I, vpColor::green);
336  vpDisplay::flush(I);
337  }
338  }
339  if (opt_display && opt_click_allowed) {
340  std::cout << "A click to exit..." << std::endl;
342  }
343  return EXIT_SUCCESS;
344  } catch (const vpException &e) {
345  std::cout << "Catch an exception: " << e << std::endl;
346  return EXIT_FAILURE;
347  }
348 }
349 #else
350 #include <iostream>
351 
352 int main()
353 {
354  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
355  "functionalities are required..."
356  << std::endl;
357  return EXIT_SUCCESS;
358 }
359 
360 #endif
void track(const vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1316
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:264
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that tracks an ellipse moving edges.
Definition: vpMeEllipse.h:106
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:183
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:104
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:151
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
#define vpCTRACE
Definition: vpDebug.h:338
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
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:88
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:144
double getSampleStep() const
Definition: vpMe.h:285