Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trackMeCircle.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  * Tracking of an ellipse.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
50 #include <visp3/core/vpConfig.h>
51 
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <sstream>
55 #include <iomanip>
56 
57 #if defined(VISP_HAVE_MODULE_ME) && (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
58 
59 #include <visp3/core/vpImage.h>
60 #include <visp3/io/vpImageIo.h>
61 #include <visp3/gui/vpDisplayX.h>
62 #include <visp3/gui/vpDisplayGTK.h>
63 #include <visp3/gui/vpDisplayGDI.h>
64 #include <visp3/gui/vpDisplayOpenCV.h>
65 #include <visp3/core/vpColor.h>
66 
67 #include <visp3/me/vpMeEllipse.h>
68 #include <visp3/io/vpParseArgv.h>
69 #include <visp3/core/vpIoTools.h>
70 
71 // List of allowed command line options
72 #define GETOPTARGS "cdi:h"
73 
74 void usage(const char *name, const char *badparam, std::string ipath);
75 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
76 
86 void usage(const char *name, const char *badparam, std::string ipath)
87 {
88  fprintf(stdout, "\n\
89 Test auto detection of dots using vpDot2.\n\
90 \n\
91 SYNOPSIS\n\
92  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
93 
94  fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  -i <input image path> %s\n\
97  Set image input path.\n\
98  From this path read \"ViSP-images/circle/circle.pgm\"\n\
99  image. \n\
100  Setting the VISP_INPUT_IMAGE_PATH environment\n\
101  variable produces the same behaviour than using\n\
102  this option.\n\
103 \n\
104  -c\n\
105  Disable the mouse click. Useful to automaze the \n\
106  execution of this program without humain intervention.\n\
107 \n\
108  -d \n\
109  Turn off the display.\n\
110 \n\
111  -h\n\
112  Print the help.\n",
113  ipath.c_str());
114 
115  if (badparam)
116  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
117 }
131 bool getOptions(int argc, const char **argv, std::string &ipath, 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  // Read the command line options
181  if (getOptions(argc, argv, opt_ipath, opt_click_allowed,
182  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 = vpIoTools::createFilePath(ipath, "ViSP-images/circle");
222 
223  // Build the name of the image file
224  filename = vpIoTools::createFilePath(dirname, "circle.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 
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 #elif defined VISP_HAVE_OPENCV
261  vpDisplayOpenCV display;
262 #endif
263 
264  if (opt_display) {
265  // Display size is automatically defined by the image (I) size
266  display.init(I, 100, 100,"Display...") ;
267  // Display the image
268  // The image class has a member that specify a pointer toward
269  // the display that has been initialized in the display declaration
270  // therefore is is no longuer necessary to make a reference to the
271  // display variable.
272  vpDisplay::display(I) ;
273  vpDisplay::flush(I) ;
274  }
275 
276  vpMeEllipse E1 ;
277 
278  vpMe me ;
279  me.setRange(20) ;
280  me.setSampleStep(2) ;
281  me.setPointsToTrack(60) ;
282  me.setThreshold(15000) ;
283 
284  E1.setMe(&me) ;
286  // If click is allowed, wait for a mouse click to select the points
287  // on the ellipse
288  if (opt_display && opt_click_allowed) {
289  E1.initTracking(I) ;
290  }
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 0;
321  }
322  catch(vpException &e) {
323  std::cout << "Catch an exception: " << e << std::endl;
324  return 1;
325  }
326 }
327 #else
328 #include <iostream>
329 
330 int main()
331 {
332  std::cout << "visp_me module or X11, GTK, GDI or OpenCV display functionalities are required..." << std::endl;
333 }
334 
335 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
void initTracking(const vpImage< unsigned char > &I)
void setPointsToTrack(const int &n)
Definition: vpMe.h:249
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:263
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
Class that tracks an ellipse moving edges.
Definition: vpMeEllipse.h:95
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpMe.h:59
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void track(const vpImage< unsigned char > &Im)
#define vpTRACE
Definition: vpDebug.h:414
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:101
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
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:142
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:337
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
void setThreshold(const double &t)
Definition: vpMe.h:284
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:256
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:135
double getSampleStep() const
Definition: vpMe.h:270