Visual Servoing Platform  version 3.4.0
testTrackDot.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test auto detection of dots.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <iomanip>
41 #include <sstream>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpDebug.h>
46 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
47 
48 #include <visp3/blob/vpDot2.h>
49 #include <visp3/core/vpCameraParameters.h>
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/gui/vpDisplayGTK.h>
54 #include <visp3/gui/vpDisplayX.h>
55 #include <visp3/io/vpImageIo.h>
56 #include <visp3/io/vpParseArgv.h>
57 #ifdef VISP_HAVE_MODULE_FEATURES
58 #include <visp3/visual_features/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeatureEllipse.h>
60 #endif
61 
68 // List of allowed command line options
69 #define GETOPTARGS "cdi:h"
70 
71 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
72 
73 void usage(const char *name, const char *badparam, std::string ipath);
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  \"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", ipath.c_str());
110 
111  if (badparam)
112  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
113 }
126 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
127 {
128  const char *optarg_;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
131 
132  switch (c) {
133  case 'c':
134  click_allowed = false;
135  break;
136  case 'd':
137  display = false;
138  break;
139  case 'i':
140  ipath = optarg_;
141  break;
142  case 'h':
143  usage(argv[0], NULL, ipath);
144  return false;
145  break;
146 
147  default:
148  usage(argv[0], optarg_, ipath);
149  return false;
150  break;
151  }
152  }
153 
154  if ((c == 1) || (c == -1)) {
155  // standalone param or error
156  usage(argv[0], NULL, ipath);
157  std::cerr << "ERROR: " << std::endl;
158  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
159  return false;
160  }
161 
162  return true;
163 }
164 
165 int main(int argc, const char **argv)
166 {
167  try {
168  std::string env_ipath;
169  std::string opt_ipath;
170  std::string ipath;
171  std::string dirname;
172  std::string filename;
173  bool opt_click_allowed = true;
174  bool opt_display = true;
175 
176  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
177  // environment variable value
178  env_ipath = vpIoTools::getViSPImagesDataPath();
179 
180  // Set the default input path
181  if (!env_ipath.empty())
182  ipath = env_ipath;
183 
184  // Read the command line options
185  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
186  exit(-1);
187  }
188 
189  // Get the option values
190  if (!opt_ipath.empty())
191  ipath = opt_ipath;
192 
193  // Compare ipath and env_ipath. If they differ, we take into account
194  // the input path comming from the command line option
195  if (!opt_ipath.empty() && !env_ipath.empty()) {
196  if (ipath != env_ipath) {
197  std::cout << std::endl << "WARNING: " << std::endl;
198  std::cout << " Since -i <visp image path=" << ipath << "> "
199  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
200  << " we skip the environment variable." << std::endl;
201  }
202  }
203 
204  // Test if an input path is set
205  if (opt_ipath.empty() && env_ipath.empty()) {
206  usage(argv[0], NULL, ipath);
207  std::cerr << std::endl << "ERROR:" << std::endl;
208  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
209  << " environment variable to specify the location of the " << std::endl
210  << " image path where test images are located." << std::endl
211  << std::endl;
212  exit(-1);
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 = vpIoTools::createFilePath(ipath, "ellipse");
222 
223  // Build the name of the image file
224  filename = vpIoTools::createFilePath(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  } catch (...) {
237  // an exception is throwned if an exception from readPGM has been
238  // catched here this will result in the end of the program Note that
239  // another error message has been printed from readPGM to give more
240  // information about the error
241  std::cerr << std::endl << "ERROR:" << std::endl;
242  std::cerr << " Cannot read " << filename << std::endl;
243  std::cerr << " Check your -i " << ipath << " option " << std::endl
244  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
245  exit(-1);
246  }
247 
248 // We open a window using either X11, GTK or GDI.
249 #if defined VISP_HAVE_X11
250  vpDisplayX display;
251 #elif defined VISP_HAVE_GTK
252  vpDisplayGTK display;
253 #elif defined VISP_HAVE_GDI
254  vpDisplayGDI display;
255 #endif
256 
257  if (opt_display) {
258  // Display size is automatically defined by the image (I) size
259  display.init(I, 100, 100, "Display...");
260  // Display the image
261  // The image class has a member that specify a pointer toward
262  // the display that has been initialized in the display declaration
263  // therefore is is no longuer necessary to make a reference to the
264  // display variable.
266  // Flush the display
267  vpDisplay::flush(I);
268  }
269 
270  vpDot2 dot;
271  std::cout << "debut 1\n";
272  // dot.setMaxDotSize(0.50); // dot max size = 50% of the image size
273  vpImagePoint ip;
274  ip.set_i(140);
275  ip.set_j(140);
276  dot.initTracking(I, ip);
277  if (opt_display) {
278  dot.setGraphics(true);
279  } else {
280  dot.setGraphics(false);
281  }
282  dot.setComputeMoments(true);
283  dot.track(I);
284 
285  vpCameraParameters cam;
286 
287 #ifdef VISP_HAVE_MODULE_FEATURES
289  vpFeatureBuilder::create(e, cam, dot);
290 #endif
291  if (opt_display) {
292 #ifdef VISP_HAVE_MODULE_FEATURES
293  e.display(cam, I, vpColor::red);
294 #endif
295  vpDisplay::flush(I);
296  if (opt_click_allowed) {
297  std::cout << "A click to exit..." << std::endl;
299  }
300  }
301  return 0;
302  } catch (const vpException &e) {
303  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
304  return 1;
305  }
306 }
307 #else
308 int main() { vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities..."); }
309 
310 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1202
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setGraphics(bool activate)
Definition: vpDot2.h:314
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:217
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1446
void set_i(double ii)
Definition: vpImagePoint.h:166
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:134
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
void set_j(double jj)
Definition: vpImagePoint.h:177
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:338
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:244
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void setComputeMoments(bool activate)
Definition: vpDot2.h:276
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
const char * getMessage() const
Definition: vpException.cpp:90