Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
trackMeCircle.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  * Tracking of an ellipse.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
51 #include <visp3/core/vpConfig.h>
52 
53 #include <iomanip>
54 #include <sstream>
55 #include <stdio.h>
56 #include <stdlib.h>
57 
58 #if defined(VISP_HAVE_MODULE_ME) && \
59  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
60 
61 #include <visp3/core/vpColor.h>
62 #include <visp3/core/vpImage.h>
63 #include <visp3/gui/vpDisplayGDI.h>
64 #include <visp3/gui/vpDisplayGTK.h>
65 #include <visp3/gui/vpDisplayOpenCV.h>
66 #include <visp3/gui/vpDisplayX.h>
67 #include <visp3/io/vpImageIo.h>
68 
69 #include <visp3/core/vpIoTools.h>
70 #include <visp3/io/vpParseArgv.h>
71 #include <visp3/me/vpMeEllipse.h>
72 
73 // List of allowed command line options
74 #define GETOPTARGS "cdi:h"
75 
76 void usage(const char *name, const char *badparam, std::string ipath);
77 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
78 
88 void usage(const char *name, const char *badparam, std::string ipath)
89 {
90  fprintf(stdout, "\n\
91 Test auto detection of dots using vpDot2.\n\
92 \n\
93 SYNOPSIS\n\
94  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  -i <input image path> %s\n\
99  Set image input path.\n\
100  From this path read \"circle/circle.pgm\"\n\
101  image. \n\
102  Setting the VISP_INPUT_IMAGE_PATH environment\n\
103  variable produces the same behaviour than using\n\
104  this option.\n\
105 \n\
106  -c\n\
107  Disable the mouse click. Useful to automaze the \n\
108  execution of this program without humain intervention.\n\
109 \n\
110  -d \n\
111  Turn off the display.\n\
112 \n\
113  -h\n\
114  Print the help.\n", ipath.c_str());
115 
116  if (badparam)
117  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
118 }
132 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
133 {
134  const char *optarg_;
135  int c;
136  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
137 
138  switch (c) {
139  case 'c':
140  click_allowed = false;
141  break;
142  case 'd':
143  display = false;
144  break;
145  case 'i':
146  ipath = optarg_;
147  break;
148  case 'h':
149  usage(argv[0], NULL, ipath);
150  return false;
151  break;
152 
153  default:
154  usage(argv[0], optarg_, ipath);
155  return false;
156  break;
157  }
158  }
159 
160  if ((c == 1) || (c == -1)) {
161  // standalone param or error
162  usage(argv[0], NULL, ipath);
163  std::cerr << "ERROR: " << std::endl;
164  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
165  return false;
166  }
167 
168  return true;
169 }
170 
171 int main(int argc, const char **argv)
172 {
173  try {
174  std::string env_ipath;
175  std::string opt_ipath;
176  std::string ipath;
177  std::string dirname;
178  std::string filename;
179  bool opt_click_allowed = true;
180  bool opt_display = true;
181 
182  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
183  // environment variable value
184  env_ipath = vpIoTools::getViSPImagesDataPath();
185 
186  // Set the default input path
187  if (!env_ipath.empty())
188  ipath = env_ipath;
189 
190  // Read the command line options
191  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
192  exit(-1);
193  }
194 
195  // Get the option values
196  if (!opt_ipath.empty())
197  ipath = opt_ipath;
198 
199  // Compare ipath and env_ipath. If they differ, we take into account
200  // the input path comming from the command line option
201  if (!opt_ipath.empty() && !env_ipath.empty()) {
202  if (ipath != env_ipath) {
203  std::cout << std::endl << "WARNING: " << std::endl;
204  std::cout << " Since -i <visp image path=" << ipath << "> "
205  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
206  << " we skip the environment variable." << std::endl;
207  }
208  }
209 
210  // Test if an input path is set
211  if (opt_ipath.empty() && env_ipath.empty()) {
212  usage(argv[0], NULL, ipath);
213  std::cerr << std::endl << "ERROR:" << std::endl;
214  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
215  << " environment variable to specify the location of the " << std::endl
216  << " image path where test images are located." << std::endl
217  << std::endl;
218  exit(-1);
219  }
220 
221  // Declare an image, this is a gray level image (unsigned char)
222  // it size is not defined yet, it will be defined when the image will
223  // read on the disk
225 
226  // Set the path location of the image sequence
227  dirname = vpIoTools::createFilePath(ipath, "circle");
228 
229  // Build the name of the image file
230  filename = vpIoTools::createFilePath(dirname, "circle.pgm");
231 
232  // Read the PGM image named "filename" on the disk, and put the
233  // bitmap into the image structure I. I is initialized to the
234  // correct size
235  //
236  // exception readPGM may throw various exception if, for example,
237  // the file does not exist, or if the memory cannot be allocated
238  try {
239  vpCTRACE << "Load: " << filename << std::endl;
240 
241  vpImageIo::read(I, filename);
242  } catch (...) {
243  // an exception is throwned if an exception from readPGM has been
244  // catched here this will result in the end of the program Note that
245  // another error message has been printed from readPGM to give more
246  // information about the error
247  std::cerr << std::endl << "ERROR:" << std::endl;
248  std::cerr << " Cannot read " << filename << std::endl;
249  std::cerr << " Check your -i " << ipath << " option " << std::endl
250  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
251  exit(-1);
252  }
253 
254 // We open a window using either X11, GTK or GDI.
255 #if defined VISP_HAVE_X11
256  vpDisplayX display;
257 #elif defined VISP_HAVE_GTK
258  vpDisplayGTK display;
259 #elif defined VISP_HAVE_GDI
260  vpDisplayGDI display;
261 #elif defined VISP_HAVE_OPENCV
262  vpDisplayOpenCV display;
263 #endif
264 
265  if (opt_display) {
266  // Display size is automatically defined by the image (I) size
267  display.init(I, 100, 100, "Display...");
268  // Display the image
269  // The image class has a member that specify a pointer toward
270  // the display that has been initialized in the display declaration
271  // therefore is is no longuer necessary to make a reference to the
272  // display variable.
274  vpDisplay::flush(I);
275  }
276 
277  vpMeEllipse E1;
278 
279  vpMe me;
280  me.setRange(20);
281  me.setSampleStep(2);
282  me.setPointsToTrack(60);
283  me.setThreshold(15000);
284 
285  E1.setMe(&me);
287  // If click is allowed, wait for a mouse click to select the points
288  // on the ellipse
289  if (opt_display && opt_click_allowed) {
290  E1.initTracking(I);
291  } else {
292  // Create a list of points to automate the test
293  std::vector<vpImagePoint> ip;
294  ip.push_back(vpImagePoint(39, 136));
295  ip.push_back(vpImagePoint(42, 83));
296  ip.push_back(vpImagePoint(86, 55));
297  ip.push_back(vpImagePoint(132, 72));
298  ip.push_back(vpImagePoint(145, 134));
299 
300  E1.initTracking(I, ip);
301  }
302 
303  if (opt_display) {
304  E1.display(I, vpColor::green);
305  vpDisplay::flush(I);
306  }
307 
308  vpTRACE("sample step %f ", E1.getMe()->getSampleStep());
309  std::cout << "Tracking on image: " << filename << std::endl;
310  E1.track(I);
311  if (opt_display) {
312  vpDisplay::flush(I);
313  }
314 
315  if (opt_display && opt_click_allowed) {
316  std::cout << "A click to exit..." << std::endl;
318  }
319  std::cout << "------------------------------------------------------------" << std::endl;
320  return EXIT_SUCCESS;
321  } catch (const vpException &e) {
322  std::cout << "Catch an exception: " << e << std::endl;
323  return EXIT_FAILURE;
324  }
325 }
326 #else
327 #include <iostream>
328 
329 int main()
330 {
331  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
332  "functionalities are required..."
333  << std::endl;
334  return EXIT_SUCCESS;
335 }
336 
337 #endif
void track(const vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1316
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:264
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
void setSampleStep(const double &s)
Definition: vpMe.h:278
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that tracks an ellipse moving edges.
Definition: vpMeEllipse.h:106
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpMe.h:60
static const vpColor green
Definition: vpColor.h:183
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 std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
#define vpTRACE
Definition: vpDebug.h:416
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:104
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void display(const vpImage< unsigned char > &I, vpColor col)
vpMe * getMe()
Definition: vpMeTracker.h:151
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
#define vpCTRACE
Definition: vpDebug.h:338
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
void setThreshold(const double &t)
Definition: vpMe.h:300
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:144
double getSampleStep() const
Definition: vpMe.h:285