Visual Servoing Platform  version 3.4.0
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 #if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
174  try {
175  std::string env_ipath;
176  std::string opt_ipath;
177  std::string ipath;
178  std::string dirname;
179  std::string filename;
180  bool opt_click_allowed = true;
181  bool opt_display = true;
182 
183  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
184  // environment variable value
185  env_ipath = vpIoTools::getViSPImagesDataPath();
186 
187  // Set the default input path
188  if (!env_ipath.empty())
189  ipath = env_ipath;
190 
191  // Read the command line options
192  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
193  exit(-1);
194  }
195 
196  // Get the option values
197  if (!opt_ipath.empty())
198  ipath = opt_ipath;
199 
200  // Compare ipath and env_ipath. If they differ, we take into account
201  // the input path comming from the command line option
202  if (!opt_ipath.empty() && !env_ipath.empty()) {
203  if (ipath != env_ipath) {
204  std::cout << std::endl << "WARNING: " << std::endl;
205  std::cout << " Since -i <visp image path=" << ipath << "> "
206  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
207  << " we skip the environment variable." << std::endl;
208  }
209  }
210 
211  // Test if an input path is set
212  if (opt_ipath.empty() && env_ipath.empty()) {
213  usage(argv[0], NULL, ipath);
214  std::cerr << std::endl << "ERROR:" << std::endl;
215  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
216  << " environment variable to specify the location of the " << std::endl
217  << " image path where test images are located." << std::endl
218  << std::endl;
219  exit(-1);
220  }
221 
222  // Declare an image, this is a gray level image (unsigned char)
223  // it size is not defined yet, it will be defined when the image will
224  // read on the disk
226 
227  // Set the path location of the image sequence
228  dirname = vpIoTools::createFilePath(ipath, "circle");
229 
230  // Build the name of the image file
231  filename = vpIoTools::createFilePath(dirname, "circle.pgm");
232 
233  // Read the PGM image named "filename" on the disk, and put the
234  // bitmap into the image structure I. I is initialized to the
235  // correct size
236  //
237  // exception readPGM may throw various exception if, for example,
238  // the file does not exist, or if the memory cannot be allocated
239  try {
240  vpCTRACE << "Load: " << filename << std::endl;
241 
242  vpImageIo::read(I, filename);
243  } catch (...) {
244  // an exception is throwned if an exception from readPGM has been
245  // catched here this will result in the end of the program Note that
246  // another error message has been printed from readPGM to give more
247  // information about the error
248  std::cerr << std::endl << "ERROR:" << std::endl;
249  std::cerr << " Cannot read " << filename << std::endl;
250  std::cerr << " Check your -i " << ipath << " option " << std::endl
251  << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
252  exit(-1);
253  }
254 
255 // We open a window using either X11, GTK or GDI.
256 #if defined VISP_HAVE_X11
257  vpDisplayX display;
258 #elif defined VISP_HAVE_GTK
259  vpDisplayGTK display;
260 #elif defined VISP_HAVE_GDI
261  vpDisplayGDI display;
262 #elif defined VISP_HAVE_OPENCV
263  vpDisplayOpenCV display;
264 #endif
265 
266  if (opt_display) {
267  // Display size is automatically defined by the image (I) size
268  display.init(I, 100, 100, "Display...");
269  // Display the image
270  // The image class has a member that specify a pointer toward
271  // the display that has been initialized in the display declaration
272  // therefore is is no longuer necessary to make a reference to the
273  // display variable.
275  vpDisplay::flush(I);
276  }
277 
278  vpMeEllipse E1;
279 
280  vpMe me;
281  me.setRange(20);
282  me.setSampleStep(2);
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(78, 203));
295  ip.push_back(vpImagePoint(62, 125));
296  ip.push_back(vpImagePoint(128, 101));
297  ip.push_back(vpImagePoint(167, 147));
298  ip.push_back(vpImagePoint(147, 200));
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 #else
326  (void)argc;
327  (void)argv;
328  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
329 #endif
330 }
331 #else
332 #include <iostream>
333 
334 int main()
335 {
336  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
337  "functionalities are required..."
338  << std::endl;
339  return EXIT_SUCCESS;
340 }
341 
342 #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:1202
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
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:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
Class that tracks an ellipse using moving edges.
Definition: vpMeEllipse.h:97
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:220
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void initTracking(const vpImage< unsigned char > &I, bool trackArc=false)
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1446
#define vpTRACE
Definition: vpDebug.h:416
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:152
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:120
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
#define vpCTRACE
Definition: vpDebug.h:338
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:244
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:87
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:173
double getSampleStep() const
Definition: vpMe.h:285