ViSP
trackMeEllipse.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeEllipse.cpp 5108 2015-01-05 07:48:58Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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) || defined(VISP_HAVE_OPENCV))
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/vpDisplayOpenCV.h>
72 #include <visp/vpColor.h>
73 
74 #include <visp/vpMeEllipse.h>
75 #include <visp/vpParseArgv.h>
76 #include <visp/vpIoTools.h>
77 
78 // List of allowed command line options
79 #define GETOPTARGS "cdi:h"
80 
81 void usage(const char *name, const char *badparam, std::string ipath);
82 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
83 
93 void usage(const char *name, const char *badparam, std::string ipath)
94 {
95  fprintf(stdout, "\n\
96 Test of ellipse tracking using vpMeEllipse.\n\
97 \n\
98 SYNOPSIS\n\
99  %s [-i <input image path>] [-c] [-h]\n", name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  -i <input image path> %s\n\
104  Set image input path.\n\
105  From this path read images \n\
106  \"ViSP-images/ellipse-1/image.%%04d.pgm\"\n\
107  Setting the VISP_INPUT_IMAGE_PATH environment\n\
108  variable produces the same behaviour than using\n\
109  this option.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -h\n\
119  Print the help.\n",
120  ipath.c_str());
121 
122  if (badparam)
123  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
124 }
138 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
139 {
140  const char *optarg_;
141  int c;
142  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
143 
144  switch (c) {
145  case 'c': click_allowed = false; break;
146  case 'd': display = false; break;
147  case 'i': ipath = optarg_; break;
148  case 'h': usage(argv[0], NULL, ipath); return false; break;
149 
150  default:
151  usage(argv[0], optarg_, ipath);
152  return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL, ipath);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 
168 int
169 main(int argc, const char ** argv)
170 {
171  try {
172  std::string env_ipath;
173  std::string opt_ipath;
174  std::string ipath;
175  std::string dirname;
176  std::string filename;
177  bool opt_click_allowed = true;
178  bool opt_display = true;
179 
180  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
181  env_ipath = vpIoTools::getViSPImagesDataPath();
182 
183  // Set the default input path
184  if (! env_ipath.empty())
185  ipath = env_ipath;
186 
187  // Read the command line options
188  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
189  exit (-1);
190  }
191 
192  // Get the option values
193  if (!opt_ipath.empty())
194  ipath = opt_ipath;
195 
196  // Compare ipath and env_ipath. If they differ, we take into account
197  // the input path comming from the command line option
198  if (!opt_ipath.empty() && !env_ipath.empty()) {
199  if (ipath != env_ipath) {
200  std::cout << std::endl
201  << "WARNING: " << std::endl;
202  std::cout << " Since -i <visp image path=" << ipath << "> "
203  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
204  << " we skip the environment variable." << std::endl;
205  }
206  }
207 
208  // Test if an input path is set
209  if (opt_ipath.empty() && env_ipath.empty()){
210  usage(argv[0], NULL, ipath);
211  std::cerr << std::endl
212  << "ERROR:" << std::endl;
213  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
214  << std::endl
215  << " environment variable to specify the location of the " << std::endl
216  << " image path where test images are located." << std::endl << std::endl;
217  exit(-1);
218  }
219 
220 
221  // Declare an image, this is a gray level image (unsigned char)
222  // it size is not defined yet, it will be defined when the image is
223  // read on the disk
225 
226  // Set the path location of the image sequence
227  dirname = vpIoTools::createFilePath(ipath, "ViSP-images/ellipse-1");
228 
229  // Build the name of the image file
230  unsigned int iter = 1; // Image number
231  std::ostringstream s;
232  s.setf(std::ios::right, std::ios::adjustfield);
233  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
234  filename = vpIoTools::createFilePath(dirname, s.str());
235 
236  // Read the PGM image named "filename" on the disk, and put the
237  // bitmap into the image structure I. I is initialized to the
238  // correct size
239  //
240  // exception readPGM may throw various exception if, for example,
241  // the file does not exist, or if the memory cannot be allocated
242  try{
243  vpCTRACE << "Load: " << filename << std::endl;
244 
245  vpImageIo::read(I, filename) ;
246  }
247  catch(...)
248  {
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
254  << "ERROR:" << std::endl;
255  std::cerr << " Cannot read " << filename << std::endl;
256  std::cerr << " Check your -i " << ipath << " option " << std::endl
257  << " or VISP_INPUT_IMAGE_PATH environment variable."
258  << std::endl;
259  exit(-1);
260  }
261 
262  // We open a window using either X11, GTK or GDI.
263 #if defined VISP_HAVE_X11
264  vpDisplayX display;
265 #elif defined VISP_HAVE_GTK
266  vpDisplayGTK display;
267 #elif defined VISP_HAVE_GDI
268  vpDisplayGDI display;
269 #elif defined VISP_HAVE_OPENCV
270  vpDisplayOpenCV display;
271 #endif
272 
273  if (opt_display) {
274  // Display size is automatically defined by the image (I) size
275  display.init(I, 100, 100,"Display...") ;
276  // Display the image
277  // The image class has a member that specify a pointer toward
278  // the display that has been initialized in the display declaration
279  // therefore is is no longuer necessary to make a reference to the
280  // display variable.
281  vpDisplay::display(I) ;
282  vpDisplay::flush(I) ;
283  }
284 
285  vpMeEllipse E1 ;
286 
287  vpMe me ;
288  me.setRange(20) ;
289  me.setSampleStep(2) ;
290  me.setPointsToTrack(60) ;
291  me.setThreshold(15000) ;
292 
293  E1.setMe(&me) ;
295  if (opt_click_allowed)
296  E1.initTracking(I) ;
297  else {
298  // Create a list of points to automate the test
299  unsigned int n=5 ;
300  vpImagePoint *ip = new vpImagePoint [n];
301  ip[0].set_i( 33 ); ip[0].set_j( 276 );
302  ip[1].set_i( 83 ); ip[1].set_j( 126 );
303  ip[2].set_i( 201 ); ip[2].set_j( 36 );
304  ip[3].set_i( 243 ); ip[3].set_j( 164 );
305  ip[4].set_i( 195 ); ip[4].set_j( 329 );
306  E1.initTracking(I, n, ip) ;
307  delete [] ip ;
308  }
309  if (opt_display) {
310  E1.display(I, vpColor::green) ;
311  }
312 
313  vpERROR_TRACE("sample step %f ",E1.getMe()->getSampleStep()) ;
314  E1.track(I) ;
315  if (opt_display && opt_click_allowed) {
316  std::cout << "A click to continue..." << std::endl;
318  }
319  std::cout <<"------------------------------------------------------------"<<std::endl;
320 
321 
322  for (iter = 1 ; iter < 51 ; iter++) // initially : iter < 1500
323  {
324  // set the new image name
325  s.str("");
326  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
327  filename = vpIoTools::createFilePath(dirname, s.str());
328  std::cout << "Tracking on image: " << filename << std::endl;
329  // read the image
330  vpImageIo::read(I, filename);
331  if (opt_display) {
332  // Display the image
333  vpDisplay::display(I) ;
334  }
335 
336  E1.track(I) ;
337 
338  if (opt_display) {
339  E1.display(I,vpColor::green) ;
340  vpDisplay::flush(I) ;
341  }
342  }
343  if (opt_display && opt_click_allowed) {
344  std::cout << "A click to exit..." << std::endl;
346  }
347  return 0;
348  }
349  catch(vpException e) {
350  std::cout << "Catch an exception: " << e << std::endl;
351  return 1;
352  }
353 }
354 #else
355 int
356 main()
357 {
358  vpERROR_TRACE("You do not have X11, GTK, GDI or OpenCV display functionalities...");
359 }
360 
361 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:395
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setSampleStep(const double &s)
Definition: vpMe.h:271
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
Class that tracks an ellipse moving edges.
Definition: vpMeEllipse.h:142
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:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
#define vpCTRACE
Definition: vpDebug.h:341
void set_i(const double ii)
Definition: vpImagePoint.h:159
void track(const vpImage< unsigned char > &Im)
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
void display(const vpImage< unsigned char > &I, vpColor col)
vpMe * getMe()
Definition: vpMeTracker.h:149
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void set_j(const double jj)
Definition: vpImagePoint.h:170
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:93
void setRange(const unsigned int &r)
Definition: vpMe.h:229
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:142
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
double getSampleStep() const
Definition: vpMe.h:278