ViSP  2.9.0
trackMeNurbs.cpp
1 /****************************************************************************
2  *
3  * $Id: trackMeNurbs.cpp 4658 2014-02-09 09:50:14Z 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))
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 
81 void usage(const char *name, const char *badparam, std::string ipath);
82 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
83 
93 void usage(const char *name, const char *badparam, std::string ipath)
94 {
95  fprintf(stdout, "\n\
96 Tracking of a nurbs using vpMe.\n\
97 \n\
98 SYNOPSIS\n\
99  %s [-i <input image path>] [-c] [-d] [-h]\n", name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  -i <input image path> %s\n\
104  Set image input path.\n\
105  From this path read images \n\
106  \"ViSP-images/ellipse-1/image.%%04d.pgm\"\n\
107  Setting the VISP_INPUT_IMAGE_PATH environment\n\
108  variable produces the same behaviour than using\n\
109  this option.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -h\n\
119  Print the help.\n",
120  ipath.c_str());
121 
122  if (badparam)
123  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
124 }
138 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
139 {
140  const char *optarg_;
141  int c;
142  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
143 
144  switch (c) {
145  case 'c': click_allowed = false; break;
146  case 'd': display = false; break;
147  case 'i': ipath = optarg_; break;
148  case 'h': usage(argv[0], NULL, ipath); return false; break;
149 
150  default:
151  usage(argv[0], optarg_, ipath);
152  return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL, ipath);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 
168 int
169 main(int argc, const char ** argv)
170 {
171  try {
172  std::string env_ipath;
173  std::string opt_ipath;
174  std::string ipath;
175  std::string filename;
176  bool opt_click_allowed = true;
177  bool opt_display = true;
178 
179  // Get the VISP_IMAGE_PATH environment variable value
180  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
181  if (ptenv != NULL)
182  env_ipath = ptenv;
183 
184  // Set the default input path
185  if (! env_ipath.empty())
186  ipath = env_ipath;
187 
188 
189  // Read the command line options
190  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
191  exit (-1);
192  }
193 
194  // Get the option values
195  if (!opt_ipath.empty())
196  ipath = opt_ipath;
197 
198  // Compare ipath and env_ipath. If they differ, we take into account
199  // the input path comming from the command line option
200  if (!opt_ipath.empty() && !env_ipath.empty()) {
201  if (ipath != env_ipath) {
202  std::cout << std::endl
203  << "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
214  << "ERROR:" << std::endl;
215  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
216  << std::endl
217  << " environment variable to specify the location of the " << std::endl
218  << " image path where test images are located." << std::endl << std::endl;
219  exit(-1);
220  }
221 
222 
223  // Declare an image, this is a gray level image (unsigned char)
224  // it size is not defined yet, it will be defined when the image is
225  // read on the disk
227 
228  // Set the path location of the image sequence
229  filename = ipath + vpIoTools::path("/ViSP-images/ellipse-1/image.%04d.pgm");
230 
231  // Build the name of the image file
232  vpVideoReader reader;
233  //Initialize the reader and get the first frame.
234  reader.setFileName(filename.c_str());
235  reader.setFirstFrameIndex(1);
236  reader.open(I);
237 
238  // We open a window using either X11, GTK or GDI.
239 #if defined VISP_HAVE_X11
240  vpDisplayX display;
241 #elif defined VISP_HAVE_GTK
242  vpDisplayGTK display;
243 #elif defined VISP_HAVE_GDI
244  vpDisplayGDI display;
245 #endif
246 
247  if (opt_display) {
248  // Display size is automatically defined by the image (I) size
249  display.init(I, 100, 100,"Display...") ;
250  // Display the image
251  // The image class has a member that specify a pointer toward
252  // the display that has been initialized in the display declaration
253  // therefore is is no longuer necessary to make a reference to the
254  // display variable.
255  vpDisplay::display(I) ;
256  vpDisplay::flush(I) ;
257  }
258 
259  vpMeNurbs nurbs ;
260 
261  vpMe me ;
262  me.setRange(30) ;
263  me.setSampleStep(5) ;
264  me.setPointsToTrack(60) ;
265  me.setThreshold(15000) ;
266 
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 
295  nurbs.track(I) ;
296  if (opt_display && opt_click_allowed) {
297  std::cout << "A click to continue..." << std::endl;
299  }
300  std::cout <<"------------------------------------------------------------"<<std::endl;
301 
302 
303  for (int iter = 1 ; iter < 40 ; iter++)
304  {
305  //read the image
306  reader.getFrame(I,iter);
307  if (opt_display) {
308  // Display the image
309  vpDisplay::display(I) ;
310  }
311 
312  //Track the nurbs
313  nurbs.track(I) ;
314 
315 
316  if (opt_display) {
317  nurbs.display(I,vpColor::green) ;
318  vpDisplay::flush(I) ;
319  vpTime::wait(100);
320  }
321  }
322  if (opt_display && opt_click_allowed) {
323  std::cout << "A click to exit..." << std::endl;
325  }
326  return 0;
327  }
328  catch(vpException e) {
329  std::cout << "Catch an exception: " << e << std::endl;
330  return 1;
331  }
332 }
333 #else
334 int
335 main()
336 {
337  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
338 }
339 
340 #endif
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:1056
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:271
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:1994
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:1112
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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 init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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:92
void setRange(const unsigned int &r)
Definition: vpMe.h:229
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:140