ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testTrackDot.cpp
1 /****************************************************************************
2  *
3  * $Id: testTrackDot.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  * Test auto detection of dots.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
44 
45 #include <visp/vpDebug.h>
46 #include <visp/vpConfig.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <sstream>
50 #include <iomanip>
51 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
52 
53 #include <visp/vpImage.h>
54 #include <visp/vpImageIo.h>
55 #include <visp/vpDisplayX.h>
56 #include <visp/vpDisplayGTK.h>
57 #include <visp/vpDisplayGDI.h>
58 #include <visp/vpDot.h>
59 #include <visp/vpFeatureEllipse.h>
60 #include <visp/vpCameraParameters.h>
61 #include <visp/vpFeatureBuilder.h>
62 #include <visp/vpParseArgv.h>
63 #include <visp/vpIoTools.h>
64 
71 // List of allowed command line options
72 #define GETOPTARGS "cdi:h"
73 
83 void usage(const char *name, const char *badparam, std::string ipath)
84 {
85  fprintf(stdout, "\n\
86 Test dot tracking.\n\
87 \n\
88 SYNOPSIS\n\
89  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  -i <input image path> %s\n\
94  Set image input path.\n\
95  From this path read image \n\
96  \"ViSP-images/ellipse/ellipse.pgm\"\n\
97  Setting the VISP_INPUT_IMAGE_PATH environment\n\
98  variable produces the same behaviour than using\n\
99  this option.\n\
100 \n\
101  -c\n\
102  Disable the mouse click. Useful to automaze the \n\
103  execution of this program without humain intervention.\n\
104 \n\
105  -d \n\
106  Turn off the display.\n\
107 \n\
108  -h\n\
109  Print the help.\n",
110  ipath.c_str());
111 
112  if (badparam)
113  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
114 
115 }
128 bool getOptions(int argc, const char **argv, std::string &ipath,
129  bool &click_allowed, bool &display)
130 {
131  const char *optarg;
132  int c;
133  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
134 
135  switch (c) {
136  case 'c': click_allowed = false; break;
137  case 'd': display = false; break;
138  case 'i': ipath = optarg; break;
139  case 'h': usage(argv[0], NULL, ipath); return false; break;
140 
141  default:
142  usage(argv[0], optarg, ipath);
143  return false; break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL, ipath);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 
159 int
160 main(int argc, const char ** argv)
161 {
162  std::string env_ipath;
163  std::string opt_ipath;
164  std::string ipath;
165  std::string dirname;
166  std::string filename;
167  bool opt_click_allowed = true;
168  bool opt_display = true;
169 
170  // Get the VISP_IMAGE_PATH environment variable value
171  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
172  if (ptenv != NULL)
173  env_ipath = ptenv;
174 
175  // Set the default input path
176  if (! env_ipath.empty())
177  ipath = env_ipath;
178 
179 
180  // Read the command line options
181  if (getOptions(argc, argv, opt_ipath,
182  opt_click_allowed, opt_display) == false) {
183  exit (-1);
184  }
185 
186  // Get the option values
187  if (!opt_ipath.empty())
188  ipath = opt_ipath;
189 
190  // Compare ipath and env_ipath. If they differ, we take into account
191  // the input path comming from the command line option
192  if (!opt_ipath.empty() && !env_ipath.empty()) {
193  if (ipath != env_ipath) {
194  std::cout << std::endl
195  << "WARNING: " << std::endl;
196  std::cout << " Since -i <visp image path=" << ipath << "> "
197  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
198  << " we skip the environment variable." << std::endl;
199  }
200  }
201 
202  // Test if an input path is set
203  if (opt_ipath.empty() && env_ipath.empty()){
204  usage(argv[0], NULL, ipath);
205  std::cerr << std::endl
206  << "ERROR:" << std::endl;
207  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
208  << std::endl
209  << " environment variable to specify the location of the " << std::endl
210  << " image path where test images are located." << std::endl << std::endl;
211  exit(-1);
212  }
213 
214 
215  // Declare an image, this is a gray level image (unsigned char)
216  // it size is not defined yet, it will be defined when the image will
217  // read on the disk
219 
220  // Set the path location of the image sequence
221  dirname = ipath + vpIoTools::path("/ViSP-images/ellipse/");
222 
223  // Build the name of the image file
224  filename = dirname + "ellipse.pgm";
225 
226  // Read the PGM image named "filename" on the disk, and put the
227  // bitmap into the image structure I. I is initialized to the
228  // correct size
229  //
230  // exception readPGM may throw various exception if, for example,
231  // the file does not exist, or if the memory cannot be allocated
232  try{
233  vpCTRACE << "Load: " << filename << std::endl;
234 
235  vpImageIo::read(I, filename) ;
236  }
237  catch(...)
238  {
239  // an exception is throwned if an exception from readPGM has been catched
240  // here this will result in the end of the program
241  // Note that another error message has been printed from readPGM
242  // to give more information about the error
243  std::cerr << std::endl
244  << "ERROR:" << std::endl;
245  std::cerr << " Cannot read " << filename << std::endl;
246  std::cerr << " Check your -i " << ipath << " option " << std::endl
247  << " or VISP_INPUT_IMAGE_PATH environment variable."
248  << std::endl;
249  exit(-1);
250  }
251 
252  // We open a window using either X11, GTK or GDI.
253 #if defined VISP_HAVE_X11
254  vpDisplayX display;
255 #elif defined VISP_HAVE_GTK
256  vpDisplayGTK display;
257 #elif defined VISP_HAVE_GDI
258  vpDisplayGDI display;
259 #endif
260 
261  if (opt_display) {
262  try{
263  // Display size is automatically defined by the image (I) size
264  display.init(I, 100, 100,"Display...") ;
265  // Display the image
266  // The image class has a member that specify a pointer toward
267  // the display that has been initialized in the display declaration
268  // therefore is is no longuer necessary to make a reference to the
269  // display variable.
270  vpDisplay::display(I) ;
271  //Flush the display
272  vpDisplay::flush(I) ;
273  }
274  catch(...)
275  {
276  vpERROR_TRACE("Error while displaying the image") ;
277  exit(-1);
278  }
279  }
280 
281  vpDot dot ;
282  dot.setMaxDotSize(0.50); // dot max size = 50% of the image size
283  vpImagePoint ip;
284  ip.set_i( 140 );
285  ip.set_j( 140 );
286  dot.initTracking(I, ip) ;
287  if (opt_display) {
288  dot.setGraphics(true) ;
289  }
290  else {
291  dot.setGraphics(false) ;
292  }
293  dot.setComputeMoments(true) ;
294  dot.track(I) ;
295 
296  vpFeatureEllipse e ;
297 
298  vpCameraParameters cam ;
299  vpFeatureBuilder::create(e,cam,dot) ;
300  if (opt_display) {
301  e.display(cam, I, vpColor::red) ;
302  if (opt_click_allowed) {
303  std::cout << "A click to exit..." << std::endl;
305  }
306  }
307 }
308 #else
309 int
310 main()
311 {
312  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
313 }
314 
315 #endif
void set_j(const double j)
Definition: vpImagePoint.h:156
void setMaxDotSize(double percentage)
Definition: vpDot.cpp:605
#define vpERROR_TRACE
Definition: vpDebug.h:379
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void set_i(const double i)
Definition: vpImagePoint.h:145
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:791
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
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
static const vpColor red
Definition: vpColor.h:167
#define vpCTRACE
Definition: vpDebug.h:327
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
Generic class defining intrinsic camera parameters.
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 display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void setGraphics(const bool activate)
Definition: vpDot.h:361
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:118
void setComputeMoments(const bool activate)
Definition: vpDot.h:333
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
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:277
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:642