Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testTrackDot.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test auto detection of dots.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
40 
41 #include <visp3/core/vpDebug.h>
42 #include <visp3/core/vpConfig.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <sstream>
46 #include <iomanip>
47 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
48 
49 #include <visp3/core/vpCameraParameters.h>
50 #include <visp3/gui/vpDisplayX.h>
51 #include <visp3/gui/vpDisplayGTK.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/core/vpIoTools.h>
54 #include <visp3/core/vpImage.h>
55 #include <visp3/io/vpImageIo.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/blob/vpDot2.h>
58 #ifdef VISP_HAVE_MODULE_FEATURES
59 # include <visp3/visual_features/vpFeatureEllipse.h>
60 # include <visp3/visual_features/vpFeatureBuilder.h>
61 #endif
62 
69 // List of allowed command line options
70 #define GETOPTARGS "cdi:h"
71 
72 bool getOptions(int argc, const char **argv, std::string &ipath,
73  bool &click_allowed, bool &display);
74 
75 void usage(const char *name, const char *badparam, std::string ipath);
85 void usage(const char *name, const char *badparam, std::string ipath)
86 {
87  fprintf(stdout, "\n\
88 Test dot tracking.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  -i <input image path> %s\n\
96  Set image input path.\n\
97  From this path read image \n\
98  \"ViSP-images/ellipse/ellipse.pgm\"\n\
99  Setting the VISP_INPUT_IMAGE_PATH environment\n\
100  variable produces the same behaviour than using\n\
101  this option.\n\
102 \n\
103  -c\n\
104  Disable the mouse click. Useful to automaze the \n\
105  execution of this program without humain intervention.\n\
106 \n\
107  -d \n\
108  Turn off the display.\n\
109 \n\
110  -h\n\
111  Print the help.\n",
112  ipath.c_str());
113 
114  if (badparam)
115  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
116 
117 }
130 bool getOptions(int argc, const char **argv, std::string &ipath,
131  bool &click_allowed, bool &display)
132 {
133  const char *optarg_;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136 
137  switch (c) {
138  case 'c': click_allowed = false; break;
139  case 'd': display = false; break;
140  case 'i': ipath = optarg_; break;
141  case 'h': usage(argv[0], NULL, ipath); return false; break;
142 
143  default:
144  usage(argv[0], optarg_, ipath);
145  return false; break;
146  }
147  }
148 
149  if ((c == 1) || (c == -1)) {
150  // standalone param or error
151  usage(argv[0], NULL, ipath);
152  std::cerr << "ERROR: " << std::endl;
153  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
154  return false;
155  }
156 
157  return true;
158 }
159 
160 
161 int
162 main(int argc, const char ** argv)
163 {
164  try {
165  std::string env_ipath;
166  std::string opt_ipath;
167  std::string ipath;
168  std::string dirname;
169  std::string filename;
170  bool opt_click_allowed = true;
171  bool opt_display = true;
172 
173  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
174  env_ipath = vpIoTools::getViSPImagesDataPath();
175 
176  // Set the default input path
177  if (! env_ipath.empty())
178  ipath = env_ipath;
179 
180 
181  // Read the command line options
182  if (getOptions(argc, argv, opt_ipath,
183  opt_click_allowed, opt_display) == false) {
184  exit (-1);
185  }
186 
187  // Get the option values
188  if (!opt_ipath.empty())
189  ipath = opt_ipath;
190 
191  // Compare ipath and env_ipath. If they differ, we take into account
192  // the input path comming from the command line option
193  if (!opt_ipath.empty() && !env_ipath.empty()) {
194  if (ipath != env_ipath) {
195  std::cout << std::endl
196  << "WARNING: " << std::endl;
197  std::cout << " Since -i <visp image path=" << ipath << "> "
198  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
199  << " we skip the environment variable." << std::endl;
200  }
201  }
202 
203  // Test if an input path is set
204  if (opt_ipath.empty() && env_ipath.empty()){
205  usage(argv[0], NULL, ipath);
206  std::cerr << std::endl
207  << "ERROR:" << std::endl;
208  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
209  << std::endl
210  << " environment variable to specify the location of the " << std::endl
211  << " image path where test images are located." << std::endl << std::endl;
212  exit(-1);
213  }
214 
215 
216  // Declare an image, this is a gray level image (unsigned char)
217  // it size is not defined yet, it will be defined when the image will
218  // read on the disk
220 
221  // Set the path location of the image sequence
222  dirname = vpIoTools::createFilePath(ipath, "ViSP-images/ellipse");
223 
224  // Build the name of the image file
225  filename = vpIoTools::createFilePath(dirname, "ellipse.pgm");
226 
227  // Read the PGM image named "filename" on the disk, and put the
228  // bitmap into the image structure I. I is initialized to the
229  // correct size
230  //
231  // exception readPGM may throw various exception if, for example,
232  // the file does not exist, or if the memory cannot be allocated
233  try{
234  vpCTRACE << "Load: " << filename << std::endl;
235 
236  vpImageIo::read(I, filename) ;
237  }
238  catch(...)
239  {
240  // an exception is throwned if an exception from readPGM has been catched
241  // here this will result in the end of the program
242  // Note that another error message has been printed from readPGM
243  // to give more information about the error
244  std::cerr << std::endl
245  << "ERROR:" << std::endl;
246  std::cerr << " Cannot read " << filename << std::endl;
247  std::cerr << " Check your -i " << ipath << " option " << std::endl
248  << " or VISP_INPUT_IMAGE_PATH environment variable."
249  << std::endl;
250  exit(-1);
251  }
252 
253  // We open a window using either X11, GTK or GDI.
254 #if defined VISP_HAVE_X11
255  vpDisplayX display;
256 #elif defined VISP_HAVE_GTK
257  vpDisplayGTK display;
258 #elif defined VISP_HAVE_GDI
259  vpDisplayGDI display;
260 #endif
261 
262  if (opt_display) {
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 
275  vpDot2 dot ;
276  std::cout << "debut 1\n";
277  //dot.setMaxDotSize(0.50); // dot max size = 50% of the image size
278  vpImagePoint ip;
279  ip.set_i( 140 );
280  ip.set_j( 140 );
281  dot.initTracking(I, ip);
282  if (opt_display) {
283  dot.setGraphics(true) ;
284  }
285  else {
286  dot.setGraphics(false) ;
287  }
288  dot.setComputeMoments(true);
289  dot.track(I) ;
290 
291  vpCameraParameters cam;
292 
293 #ifdef VISP_HAVE_MODULE_FEATURES
295  vpFeatureBuilder::create(e,cam,dot);
296 #endif
297  if (opt_display) {
298 #ifdef VISP_HAVE_MODULE_FEATURES
299  e.display(cam, I, vpColor::red) ;
300 #endif
301  vpDisplay::flush(I);
302  if (opt_click_allowed) {
303  std::cout << "A click to exit..." << std::endl;
305  }
306  }
307  return 0;
308  }
309  catch(vpException &e) {
310  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
311  return 1;
312  }
313 }
314 #else
315 int
316 main()
317 {
318  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
319 }
320 
321 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
#define vpERROR_TRACE
Definition: vpDebug.h:391
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
error that can be emited by ViSP classes.
Definition: vpException.h:73
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static const vpColor red
Definition: vpColor.h:163
void set_i(const double ii)
Definition: vpImagePoint.h:163
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
const char * getMessage(void) const
Definition: vpException.cpp:97
void setComputeMoments(const bool activate)
Definition: vpDot2.h:278
void set_j(const double jj)
Definition: vpImagePoint.h:174
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
#define vpCTRACE
Definition: vpDebug.h:337
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:262
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setGraphics(const bool activate)
Definition: vpDot2.h:316