ViSP  2.8.0
trackMeEllipse.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeEllipse.cpp 4323 2013-07-18 09:24:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Tracking of an ellipse.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
54 #include <visp/vpDebug.h>
55 #include <visp/vpConfig.h>
56 
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <sstream>
61 #include <iomanip>
62 
63 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
64 
65 #include <visp/vpImage.h>
66 #include <visp/vpImageIo.h>
67 #include <visp/vpImagePoint.h>
68 #include <visp/vpDisplayX.h>
69 #include <visp/vpDisplayGTK.h>
70 #include <visp/vpDisplayGDI.h>
71 #include <visp/vpColor.h>
72 
73 #include <visp/vpMeEllipse.h>
74 #include <visp/vpParseArgv.h>
75 #include <visp/vpIoTools.h>
76 
77 // List of allowed command line options
78 #define GETOPTARGS "cdi:h"
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  std::string env_ipath;
168  std::string opt_ipath;
169  std::string ipath;
170  std::string dirname;
171  std::string filename;
172  bool opt_click_allowed = true;
173  bool opt_display = true;
174 
175  // Get the VISP_IMAGE_PATH environment variable value
176  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
177  if (ptenv != NULL)
178  env_ipath = ptenv;
179 
180  // Set the default input path
181  if (! env_ipath.empty())
182  ipath = env_ipath;
183 
184 
185  // Read the command line options
186  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
187  exit (-1);
188  }
189 
190  // Get the option values
191  if (!opt_ipath.empty())
192  ipath = opt_ipath;
193 
194  // Compare ipath and env_ipath. If they differ, we take into account
195  // the input path comming from the command line option
196  if (!opt_ipath.empty() && !env_ipath.empty()) {
197  if (ipath != env_ipath) {
198  std::cout << std::endl
199  << "WARNING: " << std::endl;
200  std::cout << " Since -i <visp image path=" << ipath << "> "
201  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
202  << " we skip the environment variable." << std::endl;
203  }
204  }
205 
206  // Test if an input path is set
207  if (opt_ipath.empty() && env_ipath.empty()){
208  usage(argv[0], NULL, ipath);
209  std::cerr << std::endl
210  << "ERROR:" << std::endl;
211  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
212  << std::endl
213  << " environment variable to specify the location of the " << std::endl
214  << " image path where test images are located." << std::endl << std::endl;
215  exit(-1);
216  }
217 
218 
219  // Declare an image, this is a gray level image (unsigned char)
220  // it size is not defined yet, it will be defined when the image is
221  // read on the disk
223 
224  // Set the path location of the image sequence
225  dirname = ipath + vpIoTools::path("/ViSP-images/ellipse-1/");
226 
227  // Build the name of the image file
228  unsigned iter = 1; // Image number
229  std::ostringstream s;
230  s.setf(std::ios::right, std::ios::adjustfield);
231  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
232  filename = dirname + s.str();
233 
234  // Read the PGM image named "filename" on the disk, and put the
235  // bitmap into the image structure I. I is initialized to the
236  // correct size
237  //
238  // exception readPGM may throw various exception if, for example,
239  // the file does not exist, or if the memory cannot be allocated
240  try{
241  vpCTRACE << "Load: " << filename << std::endl;
242 
243  vpImageIo::read(I, filename) ;
244  }
245  catch(...)
246  {
247  // an exception is thrown if an exception from readPGM has been caught
248  // here this will result in the end of the program
249  // Note that another error message has been printed from readPGM
250  // to give more information about the error
251  std::cerr << std::endl
252  << "ERROR:" << std::endl;
253  std::cerr << " Cannot read " << filename << std::endl;
254  std::cerr << " Check your -i " << ipath << " option " << std::endl
255  << " or VISP_INPUT_IMAGE_PATH environment variable."
256  << 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 #endif
268 
269  if (opt_display) {
270  try{
271  // Display size is automatically defined by the image (I) size
272  display.init(I, 100, 100,"Display...") ;
273  // Display the image
274  // The image class has a member that specify a pointer toward
275  // the display that has been initialized in the display declaration
276  // therefore is is no longuer necessary to make a reference to the
277  // display variable.
278  vpDisplay::display(I) ;
279  vpDisplay::flush(I) ;
280  }
281  catch(...)
282  {
283  vpERROR_TRACE("Error while displaying the image") ;
284  exit(-1);
285  }
286  }
287 
288  vpMeEllipse E1 ;
289 
290  vpMe me ;
291  me.setRange(20) ;
292  me.setSampleStep(2) ;
293  me.setPointsToTrack(60) ;
294  me.setThreshold(15000) ;
295 
296  E1.setMe(&me) ;
298  if (opt_click_allowed)
299  E1.initTracking(I) ;
300  else {
301  // Create a list of points to automate the test
302  unsigned int n=5 ;
303  vpImagePoint *ip = new vpImagePoint [n];
304  ip[0].set_i( 33 ); ip[0].set_j( 276 );
305  ip[1].set_i( 83 ); ip[1].set_j( 126 );
306  ip[2].set_i( 201 ); ip[2].set_j( 36 );
307  ip[3].set_i( 243 ); ip[3].set_j( 164 );
308  ip[4].set_i( 195 ); ip[4].set_j( 329 );
309  E1.initTracking(I, n, ip) ;
310  delete [] ip ;
311  }
312  if (opt_display) {
313  E1.display(I, vpColor::green) ;
314  }
315 
316  vpERROR_TRACE("sample step %f ",E1.getMe()->getSampleStep()) ;
317  E1.track(I) ;
318  if (opt_display && opt_click_allowed) {
319  std::cout << "A click to continue..." << std::endl;
321  }
322  std::cout <<"------------------------------------------------------------"<<std::endl;
323 
324 
325  for (int 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 = 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
336  vpDisplay::display(I) ;
337  }
338  try
339  {
340  E1.track(I) ;
341  }
342  catch(...)
343  {
344  vpERROR_TRACE("Error in tracking vpMeLine ") ;
345  exit(1) ;
346  }
347 
348  if (opt_display) {
349  E1.display(I,vpColor::green) ;
350  vpDisplay::flush(I) ;
351  }
352  }
353  if (opt_display && opt_click_allowed) {
354  std::cout << "A click to exit..." << std::endl;
356  }
357 }
358 #else
359 int
360 main()
361 {
362  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
363 }
364 
365 #endif
void set_j(const double j)
Definition: vpImagePoint.h:156
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:379
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
void setSampleStep(const double &s)
Definition: vpMe.h:271
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void set_i(const double i)
Definition: vpImagePoint.h:145
Class that tracks an ellipse moving edges.
Definition: vpMeEllipse.h:142
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#define vpCTRACE
Definition: vpDebug.h:327
void track(const vpImage< unsigned char > &Im)
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
void display(const vpImage< unsigned char > &I, vpColor col)
vpMe * getMe()
Definition: vpMeTracker.h:147
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setThreshold(const double &t)
Definition: vpMe.h:299
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
void setRange(const unsigned int &r)
Definition: vpMe.h:229
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:277
double getSampleStep() const
Definition: vpMe.h:278
void setMe(vpMe *me)
Definition: vpMeTracker.h:140