ViSP  2.8.0
trackMeNurbs.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeNurbs.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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))
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/vpColor.h>
72 
73 #include <visp/vpMeNurbs.h>
74 #include <visp/vpParseArgv.h>
75 #include <visp/vpIoTools.h>
76 #include <visp/vpVideoReader.h>
77 
78 // List of allowed command line options
79 #define GETOPTARGS "cdi:h"
80 
90 void usage(const char *name, const char *badparam, std::string ipath)
91 {
92  fprintf(stdout, "\n\
93 Tracking of a nurbs using vpMe.\n\
94 \n\
95 SYNOPSIS\n\
96  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  -i <input image path> %s\n\
101  Set image input path.\n\
102  From this path read images \n\
103  \"ViSP-images/ellipse-1/image.%%04d.pgm\"\n\
104  Setting the VISP_INPUT_IMAGE_PATH environment\n\
105  variable produces the same behaviour than using\n\
106  this option.\n\
107 \n\
108  -c\n\
109  Disable the mouse click. Useful to automaze the \n\
110  execution of this program without humain intervention.\n\
111 \n\
112  -d \n\
113  Turn off the display.\n\
114 \n\
115  -h\n\
116  Print the help.\n",
117  ipath.c_str());
118 
119  if (badparam)
120  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
121 }
135 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
136 {
137  const char *optarg;
138  int c;
139  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
140 
141  switch (c) {
142  case 'c': click_allowed = false; break;
143  case 'd': display = false; break;
144  case 'i': ipath = optarg; break;
145  case 'h': usage(argv[0], NULL, ipath); return false; break;
146 
147  default:
148  usage(argv[0], optarg, ipath);
149  return false; break;
150  }
151  }
152 
153  if ((c == 1) || (c == -1)) {
154  // standalone param or error
155  usage(argv[0], NULL, ipath);
156  std::cerr << "ERROR: " << std::endl;
157  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
158  return false;
159  }
160 
161  return true;
162 }
163 
164 
165 int
166 main(int argc, const char ** argv)
167 {
168  std::string env_ipath;
169  std::string opt_ipath;
170  std::string ipath;
171  std::string filename;
172  bool opt_click_allowed = true;
173  bool opt_display = true;
174 
175  // Get the VISP_IMAGE_PATH environment variable value
176  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
177  if (ptenv != NULL)
178  env_ipath = ptenv;
179 
180  // Set the default input path
181  if (! env_ipath.empty())
182  ipath = env_ipath;
183 
184 
185  // Read the command line options
186  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
187  exit (-1);
188  }
189 
190  // Get the option values
191  if (!opt_ipath.empty())
192  ipath = opt_ipath;
193 
194  // Compare ipath and env_ipath. If they differ, we take into account
195  // the input path comming from the command line option
196  if (!opt_ipath.empty() && !env_ipath.empty()) {
197  if (ipath != env_ipath) {
198  std::cout << std::endl
199  << "WARNING: " << std::endl;
200  std::cout << " Since -i <visp image path=" << ipath << "> "
201  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
202  << " we skip the environment variable." << std::endl;
203  }
204  }
205 
206  // Test if an input path is set
207  if (opt_ipath.empty() && env_ipath.empty()){
208  usage(argv[0], NULL, ipath);
209  std::cerr << std::endl
210  << "ERROR:" << std::endl;
211  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
212  << std::endl
213  << " environment variable to specify the location of the " << std::endl
214  << " image path where test images are located." << std::endl << std::endl;
215  exit(-1);
216  }
217 
218 
219  // Declare an image, this is a gray level image (unsigned char)
220  // it size is not defined yet, it will be defined when the image is
221  // read on the disk
223 
224  // Set the path location of the image sequence
225  filename = ipath + vpIoTools::path("/ViSP-images/ellipse-1/image.%04d.pgm");
226 
227  // Build the name of the image file
228  vpVideoReader reader;
229  //Initialize the reader and get the first frame.
230  reader.setFileName(filename.c_str());
231  reader.setFirstFrameIndex(1);
232  reader.open(I);
233 
234  // We open a window using either X11, GTK or GDI.
235 #if defined VISP_HAVE_X11
236  vpDisplayX display;
237 #elif defined VISP_HAVE_GTK
238  vpDisplayGTK display;
239 #elif defined VISP_HAVE_GDI
240  vpDisplayGDI display;
241 #endif
242 
243  if (opt_display) {
244  try{
245  // Display size is automatically defined by the image (I) size
246  display.init(I, 100, 100,"Display...") ;
247  // Display the image
248  // The image class has a member that specify a pointer toward
249  // the display that has been initialized in the display declaration
250  // therefore is is no longuer necessary to make a reference to the
251  // display variable.
252  vpDisplay::display(I) ;
253  vpDisplay::flush(I) ;
254  }
255  catch(...)
256  {
257  vpERROR_TRACE("Error while displaying the image") ;
258  exit(-1);
259  }
260  }
261 
262  vpMeNurbs nurbs ;
263 
264  vpMe me ;
265  me.setRange(30) ;
266  me.setSampleStep(5) ;
267  me.setPointsToTrack(60) ;
268  me.setThreshold(15000) ;
269 
270 
271  nurbs.setMe(&me);
273  nurbs.setNbControlPoints(14);
274 
275  if (opt_click_allowed)
276  {
277  std::cout << "Click on points along the edge with the left button." << std::endl;
278  std::cout << "Then click on the right button to continue." << std::endl;
279  nurbs.initTracking(I);
280  }
281  else
282  {
283  // Create a list of points to automate the test
284  std::list<vpImagePoint> list;
285  list.push_back(vpImagePoint(178,357));
286  list.push_back(vpImagePoint(212,287));
287  list.push_back(vpImagePoint(236,210));
288  list.push_back(vpImagePoint(240, 118));
289  list.push_back(vpImagePoint(210, 40));
290 
291  nurbs.initTracking(I, list) ;
292  }
293  if (opt_display) {
294  nurbs.display(I, vpColor::green) ;
295  }
296 
297 
298  nurbs.track(I) ;
299  if (opt_display && opt_click_allowed) {
300  std::cout << "A click to continue..." << std::endl;
302  }
303  std::cout <<"------------------------------------------------------------"<<std::endl;
304 
305 
306  for (int iter = 1 ; iter < 40 ; iter++)
307  {
308  //read the image
309  reader.getFrame(I,iter);
310  if (opt_display) {
311  // Display the image
312  vpDisplay::display(I) ;
313  }
314  try
315  {
316  //Track the nurbs
317  nurbs.track(I) ;
318  }
319  catch(...)
320  {
321  vpERROR_TRACE("Error in tracking vpMeLine ") ;
322  exit(1) ;
323  }
324 
325  if (opt_display) {
326  nurbs.display(I,vpColor::green) ;
327  vpDisplay::flush(I) ;
328  vpTime::wait(100);
329  }
330  }
331  if (opt_display && opt_click_allowed) {
332  std::cout << "A click to exit..." << std::endl;
334  }
335 }
336 #else
337 int
338 main()
339 {
340  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
341 }
342 
343 #endif
void setFirstFrameIndex(const long firstFrame)
void setNbControlPoints(const unsigned int nbControlPoints)
Definition: vpMeNurbs.h:165
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
#define vpERROR_TRACE
Definition: vpDebug.h:379
void track(const vpImage< unsigned char > &Im)
Definition: vpMeNurbs.cpp:1046
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
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...
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeNurbs.cpp:263
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
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:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeNurbs.cpp:1102
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void setFileName(const char *filename)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setThreshold(const double &t)
Definition: vpMe.h:299
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:92
void setRange(const unsigned int &r)
Definition: vpMe.h:229
void setMe(vpMe *me)
Definition: vpMeTracker.h:140