ViSP
trackMeNurbs.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeNurbs.cpp 5108 2015-01-05 07:48:58Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Tracking of a nurbs.
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
55 #include <visp/vpDebug.h>
56 #include <visp/vpConfig.h>
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <sstream>
61 #include <iomanip>
62 
63 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
64 
65 #include <visp/vpImage.h>
66 #include <visp/vpImageIo.h>
67 #include <visp/vpImagePoint.h>
68 #include <visp/vpDisplayX.h>
69 #include <visp/vpDisplayGTK.h>
70 #include <visp/vpDisplayGDI.h>
71 #include <visp/vpDisplayOpenCV.h>
72 #include <visp/vpColor.h>
73 
74 #include <visp/vpMeNurbs.h>
75 #include <visp/vpParseArgv.h>
76 #include <visp/vpIoTools.h>
77 #include <visp/vpVideoReader.h>
78 
79 // List of allowed command line options
80 #define GETOPTARGS "cdi:h"
81 
82 void usage(const char *name, const char *badparam, std::string ipath);
83 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
84 
94 void usage(const char *name, const char *badparam, std::string ipath)
95 {
96  fprintf(stdout, "\n\
97 Tracking of a nurbs using vpMe.\n\
98 \n\
99 SYNOPSIS\n\
100  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
101 
102  fprintf(stdout, "\n\
103 OPTIONS: Default\n\
104  -i <input image path> %s\n\
105  Set image input path.\n\
106  From this path read images \n\
107  \"ViSP-images/ellipse-1/image.%%04d.pgm\"\n\
108  Setting the VISP_INPUT_IMAGE_PATH environment\n\
109  variable produces the same behaviour than using\n\
110  this option.\n\
111 \n\
112  -c\n\
113  Disable the mouse click. Useful to automaze the \n\
114  execution of this program without humain intervention.\n\
115 \n\
116  -d \n\
117  Turn off the display.\n\
118 \n\
119  -h\n\
120  Print the help.\n",
121  ipath.c_str());
122 
123  if (badparam)
124  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
125 }
139 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
140 {
141  const char *optarg_;
142  int c;
143  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
144 
145  switch (c) {
146  case 'c': click_allowed = false; break;
147  case 'd': display = false; break;
148  case 'i': ipath = optarg_; break;
149  case 'h': usage(argv[0], NULL, ipath); return false; break;
150 
151  default:
152  usage(argv[0], optarg_, ipath);
153  return false; break;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], NULL, ipath);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 
169 int
170 main(int argc, const char ** argv)
171 {
172  try {
173  std::string env_ipath;
174  std::string opt_ipath;
175  std::string ipath;
176  std::string filename;
177  bool opt_click_allowed = true;
178  bool opt_display = true;
179 
180  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
181  env_ipath = vpIoTools::getViSPImagesDataPath();
182 
183  // Set the default input path
184  if (! env_ipath.empty())
185  ipath = env_ipath;
186 
187 
188  // Read the command line options
189  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
190  exit (-1);
191  }
192 
193  // Get the option values
194  if (!opt_ipath.empty())
195  ipath = opt_ipath;
196 
197  // Compare ipath and env_ipath. If they differ, we take into account
198  // the input path comming from the command line option
199  if (!opt_ipath.empty() && !env_ipath.empty()) {
200  if (ipath != env_ipath) {
201  std::cout << std::endl
202  << "WARNING: " << std::endl;
203  std::cout << " Since -i <visp image path=" << ipath << "> "
204  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
205  << " we skip the environment variable." << std::endl;
206  }
207  }
208 
209  // Test if an input path is set
210  if (opt_ipath.empty() && env_ipath.empty()){
211  usage(argv[0], NULL, ipath);
212  std::cerr << std::endl
213  << "ERROR:" << std::endl;
214  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
215  << std::endl
216  << " environment variable to specify the location of the " << std::endl
217  << " image path where test images are located." << std::endl << std::endl;
218  exit(-1);
219  }
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 is
224  // read on the disk
226 
227  // Set the path location of the image sequence
228  filename = vpIoTools::createFilePath(ipath, "ViSP-images/ellipse-1/image.%04d.pgm");
229 
230  // Build the name of the image file
231  vpVideoReader reader;
232  //Initialize the reader and get the first frame.
233  reader.setFileName(filename.c_str());
234  reader.setFirstFrameIndex(1);
235  reader.open(I);
236 
237  // We open a window using either X11, GTK or GDI.
238 #if defined VISP_HAVE_X11
239  vpDisplayX display;
240 #elif defined VISP_HAVE_GTK
241  vpDisplayGTK display;
242 #elif defined VISP_HAVE_GDI
243  vpDisplayGDI display;
244 #elif defined VISP_HAVE_OPENCV
245  vpDisplayOpenCV display;
246 #endif
247 
248  if (opt_display) {
249  // Display size is automatically defined by the image (I) size
250  display.init(I, 100, 100,"Display...") ;
251  // Display the image
252  // The image class has a member that specify a pointer toward
253  // the display that has been initialized in the display declaration
254  // therefore is is no longuer necessary to make a reference to the
255  // display variable.
256  vpDisplay::display(I) ;
257  vpDisplay::flush(I) ;
258  }
259 
260  vpMeNurbs nurbs ;
261 
262  vpMe me ;
263  me.setRange(30) ;
264  me.setSampleStep(5) ;
265  me.setPointsToTrack(60) ;
266  me.setThreshold(15000) ;
267 
268  nurbs.setMe(&me);
270  nurbs.setNbControlPoints(14);
271 
272  if (opt_click_allowed)
273  {
274  std::cout << "Click on points along the edge with the left button." << std::endl;
275  std::cout << "Then click on the right button to continue." << std::endl;
276  nurbs.initTracking(I);
277  }
278  else
279  {
280  // Create a list of points to automate the test
281  std::list<vpImagePoint> list;
282  list.push_back(vpImagePoint(178,357));
283  list.push_back(vpImagePoint(212,287));
284  list.push_back(vpImagePoint(236,210));
285  list.push_back(vpImagePoint(240, 118));
286  list.push_back(vpImagePoint(210, 40));
287 
288  nurbs.initTracking(I, list) ;
289  }
290  if (opt_display) {
291  nurbs.display(I, vpColor::green) ;
292  }
293 
294  nurbs.track(I) ;
295  if (opt_display && opt_click_allowed) {
296  std::cout << "A click to continue..." << std::endl;
298  }
299  std::cout <<"------------------------------------------------------------"<<std::endl;
300 
301  for (int iter = 1 ; iter < 40 ; iter++)
302  {
303  //read the image
304  reader.getFrame(I,iter);
305  if (opt_display) {
306  // Display the image
307  vpDisplay::display(I) ;
308  }
309 
310  //Track the nurbs
311  nurbs.track(I) ;
312 
313 
314  if (opt_display) {
315  nurbs.display(I,vpColor::green) ;
316  vpDisplay::flush(I) ;
317  vpTime::wait(100);
318  }
319  }
320  if (opt_display && opt_click_allowed) {
321  std::cout << "A click to exit..." << std::endl;
323  }
324  return 0;
325  }
326  catch(vpException e) {
327  std::cout << "Catch an exception: " << e << std::endl;
328  return 1;
329  }
330 }
331 #else
332 int
333 main()
334 {
335  vpERROR_TRACE("You do not have X11, GTK, GDI or OpenCV display functionalities...");
336 }
337 
338 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:395
void track(const vpImage< unsigned char > &Im)
Definition: vpMeNurbs.cpp:1063
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setSampleStep(const double &s)
Definition: vpMe.h:271
Define the X11 console to display images.
Definition: vpDisplayX.h:152
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
error that can be emited by ViSP classes.
Definition: vpException.h:76
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeNurbs.cpp:272
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
Class that tracks in an image a edge defined by a Nurbs.
Definition: vpMeNurbs.h:129
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeNurbs.cpp:1119
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void setFileName(const char *filename)
void setNbControlPoints(const unsigned int nb_point)
Definition: vpMeNurbs.h:165
void setThreshold(const double &t)
Definition: vpMe.h:299
void setFirstFrameIndex(const long first_frame)
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
void setRange(const unsigned int &r)
Definition: vpMe.h:229
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:142