Visual Servoing Platform  version 3.0.0
trackMeNurbs.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 a nurbs.
32  *
33  * Authors:
34  * Nicolas Melchior
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
51 #include <visp3/core/vpDebug.h>
52 #include <visp3/core/vpConfig.h>
53 
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <sstream>
57 #include <iomanip>
58 
59 #if defined(VISP_HAVE_MODULE_ME) && (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
60 
61 #include <visp3/core/vpImage.h>
62 #include <visp3/io/vpImageIo.h>
63 #include <visp3/core/vpImagePoint.h>
64 #include <visp3/gui/vpDisplayX.h>
65 #include <visp3/gui/vpDisplayGTK.h>
66 #include <visp3/gui/vpDisplayGDI.h>
67 #include <visp3/gui/vpDisplayOpenCV.h>
68 #include <visp3/core/vpColor.h>
69 
70 #include <visp3/me/vpMeNurbs.h>
71 #include <visp3/io/vpParseArgv.h>
72 #include <visp3/core/vpIoTools.h>
73 #include <visp3/io/vpVideoReader.h>
74 
75 // List of allowed command line options
76 #define GETOPTARGS "cdi:h"
77 
78 void usage(const char *name, const char *badparam, std::string ipath);
79 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
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  try {
169  std::string env_ipath;
170  std::string opt_ipath;
171  std::string ipath;
172  std::string filename;
173  bool opt_click_allowed = true;
174  bool opt_display = true;
175 
176  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
177  env_ipath = vpIoTools::getViSPImagesDataPath();
178 
179  // Set the default input path
180  if (! env_ipath.empty())
181  ipath = env_ipath;
182 
183 
184  // Read the command line options
185  if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
186  exit (-1);
187  }
188 
189  // Get the option values
190  if (!opt_ipath.empty())
191  ipath = opt_ipath;
192 
193  // Compare ipath and env_ipath. If they differ, we take into account
194  // the input path comming from the command line option
195  if (!opt_ipath.empty() && !env_ipath.empty()) {
196  if (ipath != env_ipath) {
197  std::cout << std::endl
198  << "WARNING: " << std::endl;
199  std::cout << " Since -i <visp image path=" << ipath << "> "
200  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
201  << " we skip the environment variable." << std::endl;
202  }
203  }
204 
205  // Test if an input path is set
206  if (opt_ipath.empty() && env_ipath.empty()){
207  usage(argv[0], NULL, ipath);
208  std::cerr << std::endl
209  << "ERROR:" << std::endl;
210  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
211  << std::endl
212  << " environment variable to specify the location of the " << std::endl
213  << " image path where test images are located." << std::endl << std::endl;
214  exit(-1);
215  }
216 
217 
218  // Declare an image, this is a gray level image (unsigned char)
219  // it size is not defined yet, it will be defined when the image is
220  // read on the disk
222 
223  // Set the path location of the image sequence
224  filename = vpIoTools::createFilePath(ipath, "ViSP-images/ellipse-1/image.%04d.pgm");
225 
226  // Build the name of the image file
227  vpVideoReader reader;
228  //Initialize the reader and get the first frame.
229  reader.setFileName(filename.c_str());
230  reader.setFirstFrameIndex(1);
231  reader.open(I);
232 
233  // We open a window using either X11, GTK or GDI.
234 #if defined VISP_HAVE_X11
235  vpDisplayX display;
236 #elif defined VISP_HAVE_GTK
237  vpDisplayGTK display;
238 #elif defined VISP_HAVE_GDI
239  vpDisplayGDI display;
240 #elif defined VISP_HAVE_OPENCV
241  vpDisplayOpenCV display;
242 #endif
243 
244  if (opt_display) {
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 
256  vpMeNurbs nurbs ;
257 
258  vpMe me ;
259  me.setRange(30) ;
260  me.setSampleStep(5) ;
261  me.setPointsToTrack(60) ;
262  me.setThreshold(15000) ;
263 
264  nurbs.setMe(&me);
266  nurbs.setNbControlPoints(14);
267 
268  if (opt_click_allowed)
269  {
270  std::cout << "Click on points along the edge with the left button." << std::endl;
271  std::cout << "Then click on the right button to continue." << std::endl;
272  nurbs.initTracking(I);
273  }
274  else
275  {
276  // Create a list of points to automate the test
277  std::list<vpImagePoint> list;
278  list.push_back(vpImagePoint(178,357));
279  list.push_back(vpImagePoint(212,287));
280  list.push_back(vpImagePoint(236,210));
281  list.push_back(vpImagePoint(240, 118));
282  list.push_back(vpImagePoint(210, 40));
283 
284  nurbs.initTracking(I, list) ;
285  }
286  if (opt_display) {
287  nurbs.display(I, vpColor::green) ;
288  }
289 
290  nurbs.track(I) ;
291  if (opt_display && opt_click_allowed) {
292  std::cout << "A click to continue..." << std::endl;
294  }
295  std::cout <<"------------------------------------------------------------"<<std::endl;
296 
297  for (int iter = 1 ; iter < 40 ; iter++)
298  {
299  //read the image
300  reader.getFrame(I,iter);
301  if (opt_display) {
302  // Display the image
303  vpDisplay::display(I) ;
304  }
305 
306  //Track the nurbs
307  nurbs.track(I) ;
308 
309 
310  if (opt_display) {
311  nurbs.display(I,vpColor::green) ;
312  vpDisplay::flush(I) ;
313  vpTime::wait(100);
314  }
315  }
316  if (opt_display && opt_click_allowed) {
317  std::cout << "A click to exit..." << std::endl;
319  }
320  return 0;
321  }
322  catch(vpException e) {
323  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
324  return 0;
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
const char * getMessage(void)
Definition: vpException.cpp:97
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1091
void setPointsToTrack(const int &n)
Definition: vpMe.h:204
void track(const vpImage< unsigned char > &Im)
Definition: vpMeNurbs.cpp:1031
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
void setSampleStep(const double &s)
Definition: vpMe.h:260
Define the X11 console to display images.
Definition: vpDisplayX.h:148
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:73
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeNurbs.cpp:268
Definition: vpMe.h:59
Class that tracks in an image a edge defined by a Nurbs.
Definition: vpMeNurbs.h:125
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:101
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1265
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeNurbs.cpp:1090
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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:141
void setFileName(const char *filename)
void setNbControlPoints(const unsigned int nb_point)
Definition: vpMeNurbs.h:161
void setThreshold(const double &t)
Definition: vpMe.h:288
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:88
void setRange(const unsigned int &r)
Definition: vpMe.h:218
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:135