ViSP  2.9.0
trackMeCircle.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeCircle.cpp 4658 2014-02-09 09:50:14Z 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  *****************************************************************************/
54 #include <visp/vpDebug.h>
55 #include <visp/vpConfig.h>
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <sstream>
60 #include <iomanip>
61 
62 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
63 
64 #include <visp/vpImage.h>
65 #include <visp/vpImageIo.h>
66 #include <visp/vpDisplayX.h>
67 #include <visp/vpDisplayGTK.h>
68 #include <visp/vpDisplayGDI.h>
69 #include <visp/vpColor.h>
70 
71 #include <visp/vpMeEllipse.h>
72 #include <visp/vpParseArgv.h>
73 #include <visp/vpIoTools.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 auto detection of dots using vpDot2.\n\
94 \n\
95 SYNOPSIS\n\
96  %s [-i <input image path>] [-c] [-d] [-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 \"ViSP-images/circle/circle.pgm\"\n\
103  image. \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",
117  ipath.c_str());
118 
119  if (badparam)
120  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
121 }
135 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
136 {
137  const char *optarg_;
138  int c;
139  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
140 
141  switch (c) {
142  case 'c': click_allowed = false; break;
143  case 'd': display = false; break;
144  case 'i': ipath = optarg_; break;
145  case 'h': usage(argv[0], NULL, ipath); return false; break;
146 
147  default:
148  usage(argv[0], optarg_, ipath);
149  return false; break;
150  }
151  }
152 
153  if ((c == 1) || (c == -1)) {
154  // standalone param or error
155  usage(argv[0], NULL, ipath);
156  std::cerr << "ERROR: " << std::endl;
157  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
158  return false;
159  }
160 
161  return true;
162 }
163 
164 
165 int
166 main(int argc, const char ** argv)
167 {
168  try {
169  std::string env_ipath;
170  std::string opt_ipath;
171  std::string ipath;
172  std::string dirname;
173  std::string filename;
174  bool opt_click_allowed = true;
175  bool opt_display = true;
176 
177  // Get the VISP_IMAGE_PATH environment variable value
178  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
179  if (ptenv != NULL)
180  env_ipath = ptenv;
181 
182  // Set the default input path
183  if (! env_ipath.empty())
184  ipath = env_ipath;
185 
186 
187  // Read the command line options
188  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
189  opt_display) == false) {
190  exit (-1);
191  }
192 
193  // Get the option values
194  if (!opt_ipath.empty())
195  ipath = opt_ipath;
196 
197  // Compare ipath and env_ipath. If they differ, we take into account
198  // the input path comming from the command line option
199  if (!opt_ipath.empty() && !env_ipath.empty()) {
200  if (ipath != env_ipath) {
201  std::cout << std::endl
202  << "WARNING: " << std::endl;
203  std::cout << " Since -i <visp image path=" << ipath << "> "
204  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
205  << " we skip the environment variable." << std::endl;
206  }
207  }
208 
209  // Test if an input path is set
210  if (opt_ipath.empty() && env_ipath.empty()){
211  usage(argv[0], NULL, ipath);
212  std::cerr << std::endl
213  << "ERROR:" << std::endl;
214  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
215  << std::endl
216  << " environment variable to specify the location of the " << std::endl
217  << " image path where test images are located." << std::endl << std::endl;
218  exit(-1);
219  }
220 
221 
222  // Declare an image, this is a gray level image (unsigned char)
223  // it size is not defined yet, it will be defined when the image will
224  // read on the disk
226 
227  // Set the path location of the image sequence
228  dirname = ipath + vpIoTools::path("/ViSP-images/circle/");
229 
230  // Build the name of the image file
231  filename = dirname + "circle.pgm";
232 
233  // Read the PGM image named "filename" on the disk, and put the
234  // bitmap into the image structure I. I is initialized to the
235  // correct size
236  //
237  // exception readPGM may throw various exception if, for example,
238  // the file does not exist, or if the memory cannot be allocated
239  try{
240  vpCTRACE << "Load: " << filename << std::endl;
241 
242  vpImageIo::read(I, filename) ;
243  }
244  catch(...)
245  {
246  // an exception is throwned if an exception from readPGM has been catched
247  // here this will result in the end of the program
248  // Note that another error message has been printed from readPGM
249  // to give more information about the error
250  std::cerr << std::endl
251  << "ERROR:" << std::endl;
252  std::cerr << " Cannot read " << filename << std::endl;
253  std::cerr << " Check your -i " << ipath << " option " << std::endl
254  << " or VISP_INPUT_IMAGE_PATH environment variable."
255  << std::endl;
256  exit(-1);
257  }
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  // 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 
290  E1.setCircle(true) ;
291  E1.setMe(&me) ;
293  // If click is allowed, wait for a mouse click to select the points
294  // on the ellipse
295  if (opt_display && opt_click_allowed) {
296  E1.initTracking(I) ;
297  }
298  else {
299  // Create a list of points to automate the test
300  unsigned int n=5 ;
301  vpImagePoint *ip = new vpImagePoint [n];
302  ip[0].set_i( 39 ); ip[0].set_j( 136 );
303  ip[1].set_i( 42 ); ip[1].set_j( 83 );
304  ip[2].set_i( 86 ); ip[2].set_j( 55 );
305  ip[3].set_i( 132 ); ip[3].set_j( 72 );
306  ip[4].set_i( 145 ); ip[4].set_j( 134 );
307  E1.initTracking(I, n, ip) ;
308  delete [] ip ;
309  }
310 
311  if (opt_display) {
312  E1.display(I, vpColor::green) ;
313  vpDisplay::flush(I);
314  }
315 
316  vpTRACE("sample step %f ",E1.getMe()->getSampleStep()) ;
317  std::cout << "Tracking on image: " << filename << std::endl;
318  E1.track(I) ;
319  if (opt_display) {
320  vpDisplay::flush(I);
321  }
322 
323  if (opt_display && opt_click_allowed) {
324  std::cout << "A click to exit..." << std::endl;
326  }
327  std::cout <<"------------------------------------------------------------"<<std::endl;
328  return 0;
329  }
330  catch(vpException e) {
331  std::cout << "Catch an exception: " << e << std::endl;
332  return 1;
333  }
334 }
335 #else
336 int
337 main()
338 {
339  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
340 }
341 
342 #endif
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
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
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:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#define vpCTRACE
Definition: vpDebug.h:341
void set_i(const double ii)
Definition: vpImagePoint.h:158
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:206
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 set_j(const double jj)
Definition: vpImagePoint.h:169
void setThreshold(const double &t)
Definition: vpMe.h:299
void setCircle(bool is_circle)
Definition: vpMeEllipse.h:180
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
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:140
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
double getSampleStep() const
Definition: vpMe.h:278