ViSP  2.9.0
trackDot2.cpp
1 /****************************************************************************
2  *
3  * $Id: trackDot2.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  * Example of dot tracking.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
48 #include <visp/vpDebug.h>
49 #include <visp/vpConfig.h>
50 
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <sstream>
54 #include <iomanip>
55 
56 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
57 
58 #include <visp/vpImage.h>
59 #include <visp/vpImageIo.h>
60 #include <visp/vpImagePoint.h>
61 #include <visp/vpDisplayX.h>
62 #include <visp/vpDisplayGTK.h>
63 #include <visp/vpDisplayGDI.h>
64 #include <visp/vpDot2.h>
65 #include <visp/vpParseArgv.h>
66 #include <visp/vpIoTools.h>
67 
68 // List of allowed command line options
69 #define GETOPTARGS "cdf:i:n:p:s:h"
70 
71 //int gsl_warnings_off;
72 
73 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath,
74  unsigned first, unsigned nimages, unsigned step);
75 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first,
76  unsigned &nimages, unsigned &step, bool &click_allowed, bool &display);
96 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath,
97  unsigned first, unsigned nimages, unsigned step)
98 {
99  fprintf(stdout, "\n\
100 Test dot tracking using vpDot2 class.\n\
101 \n\
102 SYNOPSIS\n\
103  %s [-i <test image path>] [-p <personal image path>]\n\
104  [-f <first image>] [-n <number of images>] [-s <step>]\n\
105  [-c] [-d] [-h]\n", name);
106 
107  fprintf(stdout, "\n\
108 OPTIONS: Default\n\
109  -i <input image path> %s\n\
110  Set image input path.\n\
111  From this path read images \n\
112  \"ViSP-images/mire-2/image.%%04d.pgm\". These \n\
113  images come from ViSP-images-x.y.z.tar.gz available \n\
114  on the ViSP website.\n\
115  Setting the VISP_INPUT_IMAGE_PATH environment\n\
116  variable produces the same behaviour than using\n\
117  this option.\n\
118  \n\
119  -p <personal image path> %s\n\
120  Specify a personal sequence containing images \n\
121  to process.\n\
122  By image sequence, we mean one file per image.\n\
123  The following image file formats PNM (PGM P5, PPM P6)\n\
124  are supported. The format is selected by analysing \n\
125  the filename extension.\n\
126  Example : \"C:/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
127  %%04d is for the image numbering.\n\
128  \n\
129  -f <first image> %u\n\
130  First image number of the sequence.\n\
131  \n\
132  -n <number of images> %u\n\
133  Number of images to load from the sequence.\n\
134  \n\
135  -s <step> %u\n\
136  Step between two images.\n\
137 \n\
138  -c\n\
139  Disable the mouse click. Useful to automaze the \n\
140  execution of this program without humain intervention.\n\
141 \n\
142  -d \n\
143  Turn off the display.\n\
144 \n\
145  -h\n\
146  Print the help.\n",
147  ipath.c_str(), ppath.c_str(), first, nimages, step);
148 
149  if (badparam)
150  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
151 }
169 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first,
170  unsigned &nimages, unsigned &step, bool &click_allowed, bool &display)
171 {
172  const char *optarg_;
173  int c;
174  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
175 
176  switch (c) {
177  case 'c': click_allowed = false; break;
178  case 'd': display = false; break;
179  case 'i': ipath = optarg_; break;
180  case 'p': ppath = optarg_; break;
181  case 'f': first = (unsigned) atoi(optarg_); break;
182  case 'n': nimages = (unsigned) atoi(optarg_); break;
183  case 's': step = (unsigned) atoi(optarg_); break;
184  case 'h': usage(argv[0], NULL, ipath, ppath, first, nimages, step);
185  return false; break;
186 
187  default:
188  usage(argv[0], optarg_, ipath, ppath, first, nimages, step);
189  return false; break;
190  }
191  }
192 
193  if ((c == 1) || (c == -1)) {
194  // standalone param or error
195  usage(argv[0], NULL, ipath, ppath, first, nimages, step);
196  std::cerr << "ERROR: " << std::endl;
197  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
198  return false;
199  }
200 
201  return true;
202 }
203 
204 int
205 main(int argc, const char ** argv)
206 {
207  try {
208  std::string env_ipath;
209  std::string opt_ipath;
210  std::string ipath;
211  std::string opt_ppath;
212  std::string dirname;
213  std::string filename;
214  unsigned opt_first = 1;
215  unsigned opt_nimages = 500;
216  unsigned opt_step = 1;
217  bool opt_click_allowed = true;
218  bool opt_display = true;
219 
220  // Get the VISP_IMAGE_PATH environment variable value
221  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
222  if (ptenv != NULL)
223  env_ipath = ptenv;
224 
225  // Set the default input path
226  if (! env_ipath.empty())
227  ipath = env_ipath;
228 
229 
230  // Read the command line options
231  if (getOptions(argc, argv, opt_ipath, opt_ppath,opt_first, opt_nimages,
232  opt_step, opt_click_allowed, opt_display) == false) {
233  exit (-1);
234  }
235 
236  // Get the option values
237  if (!opt_ipath.empty())
238  ipath = opt_ipath;
239 
240  // Compare ipath and env_ipath. If they differ, we take into account
241  // the input path comming from the command line option
242  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
243  if (ipath != env_ipath) {
244  std::cout << std::endl
245  << "WARNING: " << std::endl;
246  std::cout << " Since -i <visp image path=" << ipath << "> "
247  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
248  << " we skip the environment variable." << std::endl;
249  }
250  }
251 
252  // Test if an input path is set
253  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty() ){
254  usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_nimages, opt_step);
255  std::cerr << std::endl
256  << "ERROR:" << std::endl;
257  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
258  << std::endl
259  << " environment variable to specify the location of the " << std::endl
260  << " image path where test images are located." << std::endl
261  << " Use -p <personal image path> option if you want to "<<std::endl
262  << " use personal images." << std::endl
263  << std::endl;
264 
265  exit(-1);
266  }
267 
268  // Declare an image, this is a gray level image (unsigned char)
269  // it size is not defined yet, it will be defined when the image will
270  // read on the disk
272 
273  unsigned iter = opt_first;
274  std::ostringstream s;
275  char cfilename[FILENAME_MAX];
276 
277  if (opt_ppath.empty()){
278 
279 
280  // Warning :
281  // the image sequence is not provided with the ViSP package
282  // therefore the program will return you an error :
283  // !! vpImageIoPnm.cpp: readPGM(#210) :couldn't read file
284  // ViSP-images/mire-2/image.0001.pgm
285  // !! vpDotExample.cpp: main(#95) :Error while reading the image
286  // terminate called after throwing an instance of 'vpImageException'
287  //
288  // The sequence is available on the visp www site
289  // http://www.irisa.fr/lagadic/visp/visp.html
290  // in the download section. It is named "ViSP-images.tar.gz"
291 
292  // Set the path location of the image sequence
293  dirname = ipath + vpIoTools::path("/ViSP-images/mire-2/");
294 
295  // Build the name of the image file
296 
297  s.setf(std::ios::right, std::ios::adjustfield);
298  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
299  filename = dirname + s.str();
300  }
301  else {
302 
303  sprintf(cfilename,opt_ppath.c_str(), iter) ;
304  filename = cfilename;
305  }
306 
307  // Read the PGM image named "filename" on the disk, and put the
308  // bitmap into the image structure I. I is initialized to the
309  // correct size
310  //
311  // exception readPGM may throw various exception if, for example,
312  // the file does not exist, or if the memory cannot be allocated
313  try{
314  vpCTRACE << "Load: " << filename << std::endl;
315 
316  vpImageIo::read(I, filename) ;
317  }
318  catch(...)
319  {
320  // an exception is throwned if an exception from readPGM has been catched
321  // here this will result in the end of the program
322  // Note that another error message has been printed from readPGM
323  // to give more information about the error
324  std::cerr << std::endl
325  << "ERROR:" << std::endl;
326  std::cerr << " Cannot read " << filename << std::endl;
327  std::cerr << " Check your -i " << ipath << " option " << std::endl
328  << " or VISP_INPUT_IMAGE_PATH environment variable."
329  << std::endl;
330  exit(-1);
331  }
332 
333  // We open a window using either X11, GTK or GDI.
334 #if defined VISP_HAVE_X11
335  vpDisplayX display;
336 #elif defined VISP_HAVE_GTK
337  vpDisplayGTK display;
338 #elif defined VISP_HAVE_GDI
339  vpDisplayGDI display;
340 #endif
341 
342  if (opt_display) {
343  // Display size is automatically defined by the image (I) size
344  display.init(I, 100, 100,"Display...") ;
345  // Display the image
346  // The image class has a member that specify a pointer toward
347  // the display that has been initialized in the display declaration
348  // therefore is is no longuer necessary to make a reference to the
349  // display variable.
350  vpDisplay::display(I) ;
351  vpDisplay::flush(I) ;
352  }
353  // define the vpDot structure.
354 
355  // vpDot and vpDot2 correspond to two different algorithms designed to track
356  // a dot. vpDot is based on recurse connex componants (all the pixels of the
357  // dot are parsed), while vpDot2 is based on freeman chain code (only the
358  // contour of the dot is parsed)
359 
360  vpDot2 d ;
361  vpImagePoint cog;
362 
363  if (opt_display) {
364  // by using setGraphics, we request to see the all the pixel of the dot
365  // in green on the screen.
366  // It uses the overlay image plane.
367  // The default of this setting is that it is time consumming
368 
369  d.setGraphics(true) ;
370  }
371  else {
372 
373  d.setGraphics(false) ;
374  }
375  // We want to track an ellipsoid shape. If you want to track a non ellipsoid
376  // object, use d.setEllipsoidShape(0);
377  // we also request to compute the dot moment m00, m10, m01, m11, m20, m02
378  d.setComputeMoments(true);
379  d.setGrayLevelPrecision(0.90);
380 
381  // tracking is initalized if no other parameters are given to the
382  // iniTracking(..) method a right mouse click on the dot is expected
383  // dot location can also be specified explicitely in the
384  // initTracking method : d.initTracking(I,ip) where ip is the image
385  // point from which the dot is searched
386 
387  if (opt_display && opt_click_allowed) {
388  std::cout << "Click on a dot to track it."<< std::endl;
389  d.initTracking(I) ;
390  }
391  else {
392  vpImagePoint ip;
393  ip.set_u( 160 );
394  ip.set_v( 212 );
395  d.initTracking(I, ip) ;
396  }
397  if (1) {
398  std::cout << "COG: " << std::endl;
399  cog = d.getCog();
400  std::cout << " u: " << cog.get_u() << " v: " << cog.get_v()
401  << " - "
402  << d.m10 / d.m00 << " " << d.m01 / d.m00 << std::endl;
403  std::cout << "Size:" << std::endl;
404  std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
405  std::cout << "Area: " << d.getArea() << std::endl;
406  std::cout << "Moments:" << std::endl;
407  std::cout << " m00: " << d.m00 << std::endl;
408  std::cout << " m10: " << d.m10 << std::endl;
409  std::cout << " m01: " << d.m01 << std::endl;
410  std::cout << " m11: " << d.m11 << std::endl;
411  std::cout << " m02: " << d.m02 << std::endl;
412  std::cout << " m20: " << d.m20 << std::endl;
413  std::cout << "Centered moments:" << std::endl;
414  std::cout << " mu11: " << d.mu11 << std::endl;
415  std::cout << " mu02: " << d.mu02 << std::endl;
416  std::cout << " mu20: " << d.mu20 << std::endl;
417  std::cout << "Settings:" << std::endl;
418  std::cout << " gray level min: " << d.getGrayLevelMin() << std::endl;
419  std::cout << " gray level max: " << d.getGrayLevelMax() << std::endl;
420  std::cout << " size precision: " << d.getSizePrecision() << std::endl;
421  std::cout << " gray level precision: " << d.getGrayLevelPrecision() << std::endl;
422  }
423 
424  while (iter < opt_first + opt_nimages*opt_step) {
425  // set the new image name
426  if (opt_ppath.empty()){
427  s.str("");
428  s << "image." << std::setw(4) << std::setfill('0') << iter << ".pgm";
429  filename = dirname + s.str();
430  }
431  else {
432  sprintf(cfilename, opt_ppath.c_str(), iter) ;
433  filename = cfilename;
434  }
435  // read the image
436  std::cout << "read : " << filename << std::endl;
437  vpImageIo::read(I, filename);
438 
439  // track the dot and returns its coordinates in the image
440  // results are given in float since many many are usually considered
441  //
442  // an expcetion is thrown by the track method if
443  // - dot is lost
444 
445  if (opt_display) {
446  // Display the image
447  vpDisplay::display(I) ;
448  }
449 
450  std::cout << "Tracking on image: " << filename << std::endl;
451  double time = vpTime::measureTimeMs();
452  d.track(I) ;
453 
454  std::cout << "COG (" << vpTime::measureTimeMs() - time << " ms): "
455  << std::endl;
456  cog = d.getCog();
457  std::cout << " u: " << cog.get_u() << " v: " << cog.get_v()
458  << " - "
459  << d.m10 / d.m00 << " " << d.m01 / d.m00 << std::endl;
460  std::cout << "Size:" << std::endl;
461  std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
462  std::cout << "Area: " << d.getArea() << std::endl;
463  std::cout << "Moments:" << std::endl;
464  std::cout << " m00: " << d.m00 << std::endl;
465  std::cout << " m10: " << d.m10 << std::endl;
466  std::cout << " m01: " << d.m01 << std::endl;
467  std::cout << " m11: " << d.m11 << std::endl;
468  std::cout << " m02: " << d.m02 << std::endl;
469  std::cout << " m20: " << d.m20 << std::endl;
470  std::cout << "Centered moments:" << std::endl;
471  std::cout << " mu11: " << d.mu11 << std::endl;
472  std::cout << " mu02: " << d.mu02 << std::endl;
473  std::cout << " mu20: " << d.mu20 << std::endl;
474  std::cout << "Settings:" << std::endl;
475  std::cout << " gray level min: " << d.getGrayLevelMin() << std::endl;
476  std::cout << " gray level max: " << d.getGrayLevelMax() << std::endl;
477  std::cout << " size precision: " << d.getSizePrecision() << std::endl;
478  std::cout << " gray level precision: " << d.getGrayLevelPrecision()
479  << std::endl;
480 
481  if (opt_display) {
482  if (0) {
483  std::list<vpImagePoint> edges;
484  d.getEdges(edges);
485  std::list<vpImagePoint>::const_iterator it;
486  for(it = edges.begin(); it != edges.end(); ++it) {
488  }
489  }
490 
491  // display a green cross (size 10) in the image at the dot center
492  // of gravity location
494  // flush the X11 buffer
495 
496  vpDisplay::flush(I) ;
497  }
498  iter ++;
499  }
500 
501  if (opt_display && opt_click_allowed) {
502  std::cout << "\nA click to exit..." << std::endl;
503  // Wait for a blocking mouse click
505  }
506  return 0;
507  }
508  catch(vpException e) {
509  std::cout << "Catch an exception: " << e << std::endl;
510  return 1;
511  }
512 }
513 #else
514 int
515 main()
516 {
517  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
518 }
519 
520 #endif
double getWidth() const
Definition: vpDot2.cpp:648
double m02
Definition: vpDot2.h:414
double get_v() const
Definition: vpImagePoint.h:263
double mu02
Definition: vpDot2.h:433
#define vpERROR_TRACE
Definition: vpDebug.h:395
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
double getGrayLevelPrecision() const
Definition: vpDot2.cpp:678
double get_u() const
Definition: vpImagePoint.h:252
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
double m11
Definition: vpDot2.h:398
double getHeight() const
Definition: vpDot2.cpp:658
static double measureTimeMs()
Definition: vpTime.cpp:86
double getArea() const
Definition: vpDot2.cpp:668
static const vpColor green
Definition: vpColor.h:170
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:127
double m01
Definition: vpDot2.h:390
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:465
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
double getSizePrecision() const
Definition: vpDot2.cpp:688
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#define vpCTRACE
Definition: vpDebug.h:341
vpImagePoint getCog() const
Definition: vpDot2.h:163
void setGrayLevelPrecision(const double &grayLevelPrecision)
Definition: vpDot2.cpp:788
double mu11
Definition: vpDot2.h:423
void set_u(const double u)
Definition: vpImagePoint.h:216
double m20
Definition: vpDot2.h:405
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void set_v(const double v)
Definition: vpImagePoint.h:227
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
unsigned int getGrayLevelMin() const
Definition: vpDot2.h:212
void setComputeMoments(const bool activate)
Definition: vpDot2.h:274
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:266
double m10
Definition: vpDot2.h:382
double mu20
Definition: vpDot2.h:428
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
double m00
Definition: vpDot2.h:374
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
unsigned int getGrayLevelMax() const
Definition: vpDot2.h:220
void setGraphics(const bool activate)
Definition: vpDot2.h:312
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
void getEdges(std::list< vpImagePoint > &edges_list) const
Definition: vpDot2.h:177
static const vpColor blue
Definition: vpColor.h:173