Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trackMeEllipse.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpConfig.h>
52 
53 
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <sstream>
57 #include <iomanip>
58 
59 #if defined(VISP_HAVE_MODULE_ME) && (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
60 
61 #include <visp3/core/vpImage.h>
62 #include <visp3/io/vpImageIo.h>
63 #include <visp3/core/vpImagePoint.h>
64 #include <visp3/gui/vpDisplayX.h>
65 #include <visp3/gui/vpDisplayGTK.h>
66 #include <visp3/gui/vpDisplayGDI.h>
67 #include <visp3/gui/vpDisplayOpenCV.h>
68 #include <visp3/core/vpColor.h>
69 
70 #include <visp3/me/vpMeEllipse.h>
71 #include <visp3/io/vpParseArgv.h>
72 #include <visp3/core/vpIoTools.h>
73 
74 // List of allowed command line options
75 #define GETOPTARGS "cdi:h"
76 
77 void usage(const char *name, const char *badparam, std::string ipath);
78 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
79 
89 void usage(const char *name, const char *badparam, std::string ipath)
90 {
91  fprintf(stdout, "\n\
92 Test of ellipse tracking using vpMeEllipse.\n\
93 \n\
94 SYNOPSIS\n\
95  %s [-i <input image path>] [-c] [-h]\n", name);
96 
97  fprintf(stdout, "\n\
98 OPTIONS: Default\n\
99  -i <input image path> %s\n\
100  Set image input path.\n\
101  From this path read images \n\
102  \"ViSP-images/ellipse-1/image.%%04d.pgm\"\n\
103  Setting the VISP_INPUT_IMAGE_PATH environment\n\
104  variable produces the same behaviour than using\n\
105  this option.\n\
106 \n\
107  -c\n\
108  Disable the mouse click. Useful to automaze the \n\
109  execution of this program without humain intervention.\n\
110 \n\
111  -d \n\
112  Turn off the display.\n\
113 \n\
114  -h\n\
115  Print the help.\n",
116  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': click_allowed = false; break;
142  case 'd': display = false; break;
143  case 'i': ipath = optarg_; break;
144  case 'h': usage(argv[0], NULL, ipath); return false; break;
145 
146  default:
147  usage(argv[0], optarg_, ipath);
148  return false; break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL, ipath);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 
164 int
165 main(int argc, const char ** argv)
166 {
167  try {
168  std::string env_ipath;
169  std::string opt_ipath;
170  std::string ipath;
171  std::string dirname;
172  std::string filename;
173  bool opt_click_allowed = true;
174  bool opt_display = true;
175 
176  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
177  env_ipath = vpIoTools::getViSPImagesDataPath();
178 
179  // Set the default input path
180  if (! env_ipath.empty())
181  ipath = env_ipath;
182 
183  // Read the command line options
184  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
185  exit (-1);
186  }
187 
188  // Get the option values
189  if (!opt_ipath.empty())
190  ipath = opt_ipath;
191 
192  // Compare ipath and env_ipath. If they differ, we take into account
193  // the input path comming from the command line option
194  if (!opt_ipath.empty() && !env_ipath.empty()) {
195  if (ipath != env_ipath) {
196  std::cout << std::endl
197  << "WARNING: " << std::endl;
198  std::cout << " Since -i <visp image path=" << ipath << "> "
199  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
200  << " we skip the environment variable." << std::endl;
201  }
202  }
203 
204  // Test if an input path is set
205  if (opt_ipath.empty() && env_ipath.empty()){
206  usage(argv[0], NULL, ipath);
207  std::cerr << std::endl
208  << "ERROR:" << std::endl;
209  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
210  << std::endl
211  << " environment variable to specify the location of the " << std::endl
212  << " image path where test images are located." << std::endl << std::endl;
213  exit(-1);
214  }
215 
216 
217  // Declare an image, this is a gray level image (unsigned char)
218  // it size is not defined yet, it will be defined when the image is
219  // read on the disk
221 
222  // Set the path location of the image sequence
223  dirname = vpIoTools::createFilePath(ipath, "ViSP-images/ellipse-1");
224 
225  // Build the name of the image file
226  unsigned int iter = 1; // Image number
227  std::ostringstream s;
228  s.setf(std::ios::right, std::ios::adjustfield);
229  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
230  filename = vpIoTools::createFilePath(dirname, s.str());
231 
232  // Read the PGM image named "filename" on the disk, and put the
233  // bitmap into the image structure I. I is initialized to the
234  // correct size
235  //
236  // exception readPGM may throw various exception if, for example,
237  // the file does not exist, or if the memory cannot be allocated
238  try{
239  vpCTRACE << "Load: " << filename << std::endl;
240 
241  vpImageIo::read(I, filename) ;
242  }
243  catch(...)
244  {
245  // an exception is thrown if an exception from readPGM has been caught
246  // here this will result in the end of the program
247  // Note that another error message has been printed from readPGM
248  // to give more information about the error
249  std::cerr << std::endl
250  << "ERROR:" << std::endl;
251  std::cerr << " Cannot read " << filename << std::endl;
252  std::cerr << " Check your -i " << ipath << " option " << std::endl
253  << " or VISP_INPUT_IMAGE_PATH environment variable."
254  << std::endl;
255  exit(-1);
256  }
257 
258  // We open a window using either X11, GTK or GDI.
259 #if defined VISP_HAVE_X11
260  vpDisplayX display;
261 #elif defined VISP_HAVE_GTK
262  vpDisplayGTK display;
263 #elif defined VISP_HAVE_GDI
264  vpDisplayGDI display;
265 #elif defined VISP_HAVE_OPENCV
266  vpDisplayOpenCV display;
267 #endif
268 
269  if (opt_display) {
270  // Display size is automatically defined by the image (I) size
271  display.init(I, 100, 100,"Display...") ;
272  // Display the image
273  // The image class has a member that specify a pointer toward
274  // the display that has been initialized in the display declaration
275  // therefore is is no longuer necessary to make a reference to the
276  // display variable.
277  vpDisplay::display(I) ;
278  vpDisplay::flush(I) ;
279  }
280 
281  vpMeEllipse E1 ;
282 
283  vpMe me ;
284  me.setRange(20) ;
285  me.setSampleStep(2) ;
286  me.setPointsToTrack(60) ;
287  me.setThreshold(15000) ;
288 
289  E1.setMe(&me) ;
291  if (opt_click_allowed)
292  E1.initTracking(I) ;
293  else {
294  // Create a list of points to automate the test
295  std::vector<vpImagePoint> ip;
296  ip.push_back(vpImagePoint(33, 276));
297  ip.push_back(vpImagePoint(83, 126));
298  ip.push_back(vpImagePoint(201, 36));
299  ip.push_back(vpImagePoint(243, 164));
300  ip.push_back(vpImagePoint(195, 329));
301 
302  E1.initTracking(I, ip) ;
303  }
304  if (opt_display) {
305  E1.display(I, vpColor::green) ;
306  }
307 
308  vpERROR_TRACE("sample step %f ",E1.getMe()->getSampleStep()) ;
309  E1.track(I) ;
310  if (opt_display && opt_click_allowed) {
311  std::cout << "A click to continue..." << std::endl;
313  }
314  std::cout <<"------------------------------------------------------------"<<std::endl;
315 
316 
317  for (iter = 1 ; iter < 51 ; iter++) // initially : iter < 1500
318  {
319  // set the new image name
320  s.str("");
321  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
322  filename = vpIoTools::createFilePath(dirname, s.str());
323  std::cout << "Tracking on image: " << filename << std::endl;
324  // read the image
325  vpImageIo::read(I, filename);
326  if (opt_display) {
327  // Display the image
328  vpDisplay::display(I) ;
329  }
330 
331  E1.track(I) ;
332 
333  if (opt_display) {
334  E1.display(I,vpColor::green) ;
335  vpDisplay::flush(I) ;
336  }
337  }
338  if (opt_display && opt_click_allowed) {
339  std::cout << "A click to exit..." << std::endl;
341  }
342  return 0;
343  }
344  catch(vpException &e) {
345  std::cout << "Catch an exception: " << e << std::endl;
346  return 1;
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 functionalities are required..." << std::endl;
355 }
356 
357 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:249
#define vpERROR_TRACE
Definition: vpDebug.h:391
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:263
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
error that can be emited by ViSP classes.
Definition: vpException.h:73
Class that tracks an ellipse moving edges.
Definition: vpMeEllipse.h:95
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpMe.h:59
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void track(const vpImage< unsigned char > &Im)
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:101
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
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:142
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:337
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
void setThreshold(const double &t)
Definition: vpMe.h:284
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:256
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:135
double getSampleStep() const
Definition: vpMe.h:270