ViSP  2.9.0
mbtEdgeTracking.cpp
1 /****************************************************************************
2  *
3  * $Id: mbtEdgeTracking.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  * Example of model based tracking.
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Romain Tallonneau
40  * Aurelien Yol
41  *
42  *****************************************************************************/
43 
50 #include <visp/vpConfig.h>
51 #include <visp/vpDebug.h>
52 #include <visp/vpDisplayD3D.h>
53 #include <visp/vpDisplayGTK.h>
54 #include <visp/vpDisplayGDI.h>
55 #include <visp/vpDisplayOpenCV.h>
56 #include <visp/vpDisplayX.h>
57 #include <visp/vpHomogeneousMatrix.h>
58 #include <visp/vpImageIo.h>
59 #include <visp/vpIoTools.h>
60 #include <visp/vpMath.h>
61 #include <visp/vpMbEdgeTracker.h>
62 #include <visp/vpVideoReader.h>
63 #include <visp/vpParseArgv.h>
64 
65 #if defined (VISP_HAVE_DISPLAY)
66 
67 #define GETOPTARGS "x:m:i:n:dchtfCo"
68 
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile,
71  std::string &initFile, bool &displayMovingEdge, bool &click_allowed, bool &display,
72  bool& cao3DModel, bool& trackCylinder, bool &useOgre);
73 
74 void usage(const char *name, const char *badparam)
75 {
76  fprintf(stdout, "\n\
77 Example of tracking based on the 3D model.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-i <test image path>] [-x <config file>]\n\
81  [-m <model name>] [-n <initialisation file base name>]\n\
82  [-t] [-c] [-d] [-h] [-f] [-C]",
83  name );
84 
85  fprintf(stdout, "\n\
86 OPTIONS: \n\
87  -i <input image path> \n\
88  Set image input path.\n\
89  From this path read images \n\
90  \"ViSP-images/mbt/cube/image%%04d.ppm\". These \n\
91  images come from ViSP-images-x.y.z.tar.gz available \n\
92  on the ViSP website.\n\
93  Setting the VISP_INPUT_IMAGE_PATH environment\n\
94  variable produces the same behaviour than using\n\
95  this option.\n\
96 \n\
97  -x <config file> \n\
98  Set the config file (the xml file) to use.\n\
99  The config file is used to specify the parameters of the tracker.\n\
100 \n\
101  -m <model name> \n\
102  Specify the name of the file of the model\n\
103  The model can either be a vrml model (.wrl) or a .cao file.\n\
104 \n\
105  -f \n\
106  Do not use the vrml model, use the .cao one. These two models are \n\
107  equivalent and comes from ViSP-images-x.y.z.tar.gz available on the ViSP\n\
108  website. However, the .cao model allows to use the 3d model based tracker \n\
109  without Coin.\n\
110 \n\
111  -C \n\
112  Track only the cube (not the cylinder). In this case the models files are\n\
113  cube.cao or cube.wrl instead of cube_and_cylinder.cao and \n\
114  cube_and_cylinder.wrl.\n\
115 \n\
116  -n <initialisation file base name> \n\
117  Base name of the initialisation file. The file will be 'base_name'.init .\n\
118  This base name is also used for the optionnal picture specifying where to \n\
119  click (a .ppm picture).\
120 \n\
121  -t \n\
122  Turn off the display of the the moving edges. \n\
123 \n\
124  -d \n\
125  Turn off the display.\n\
126 \n\
127  -c\n\
128  Disable the mouse click. Useful to automaze the \n\
129  execution of this program without humain intervention.\n\
130 \n\
131  -o\n\
132  Use Ogre3D for visibility tests\n\
133 \n\
134  -h \n\
135  Print the help.\n\n");
136 
137  if (badparam)
138  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
139 }
140 
141 
142 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile,
143  std::string &initFile, bool &displayMovingEdge, bool &click_allowed, bool &display,
144  bool& cao3DModel, bool& trackCylinder, bool &useOgre)
145 {
146  const char *optarg_;
147  int c;
148  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
149 
150  switch (c) {
151  case 'i': ipath = optarg_; break;
152  case 'x': configFile = optarg_; break;
153  case 'm': modelFile = optarg_; break;
154  case 'n': initFile = optarg_; break;
155  case 't': displayMovingEdge = false; break;
156  case 'f': cao3DModel = true; break;
157  case 'c': click_allowed = false; break;
158  case 'd': display = false; break;
159  case 'C': trackCylinder = false; break;
160  case 'o' : useOgre = true; break;
161  case 'h': usage(argv[0], NULL); return false; break;
162 
163  default:
164  usage(argv[0], optarg_);
165  return false; break;
166  }
167  }
168 
169  if ((c == 1) || (c == -1)) {
170  // standalone param or error
171  usage(argv[0], NULL);
172  std::cerr << "ERROR: " << std::endl;
173  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
174  return false;
175  }
176 
177  return true;
178 }
179 
180 int
181 main(int argc, const char ** argv)
182 {
183  try {
184  std::string env_ipath;
185  std::string opt_ipath;
186  std::string ipath;
187  std::string opt_configFile;
188  std::string configFile;
189  std::string opt_modelFile;
190  std::string modelFile;
191  std::string opt_initFile;
192  std::string initFile;
193  bool displayMovingEdge = true;
194  bool opt_click_allowed = true;
195  bool opt_display = true;
196  bool cao3DModel = false;
197  bool trackCylinder = true;
198  bool useOgre = false;
199 
200  // Get the VISP_IMAGE_PATH environment variable value
201  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
202  if (ptenv != NULL)
203  env_ipath = ptenv;
204 
205  // Set the default input path
206  if (! env_ipath.empty())
207  ipath = env_ipath;
208 
209 
210  // Read the command line options
211  if (!getOptions(argc, argv, opt_ipath, opt_configFile, opt_modelFile, opt_initFile, displayMovingEdge, opt_click_allowed, opt_display, cao3DModel, trackCylinder, useOgre)) {
212  return (-1);
213  }
214 
215  // Test if an input path is set
216  if (opt_ipath.empty() && env_ipath.empty() ){
217  usage(argv[0], NULL);
218  std::cerr << std::endl
219  << "ERROR:" << std::endl;
220  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
221  << std::endl
222  << " environment variable to specify the location of the " << std::endl
223  << " image path where test images are located." << std::endl
224  << std::endl;
225 
226  return (-1);
227  }
228 
229  // Get the option values
230  if (!opt_ipath.empty())
231  ipath = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube/image%04d.pgm");
232  else
233  ipath = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube/image%04d.pgm");
234 
235  if (!opt_configFile.empty())
236  configFile = opt_configFile;
237  else if (!opt_ipath.empty())
238  configFile = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube.xml");
239  else
240  configFile = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube.xml");
241 
242  if (!opt_modelFile.empty()){
243  modelFile = opt_modelFile;
244  }else{
245  std::string modelFileCao;
246  std::string modelFileWrl;
247  if(trackCylinder){
248  modelFileCao = "/ViSP-images/mbt/cube_and_cylinder.cao";
249  modelFileWrl = "/ViSP-images/mbt/cube_and_cylinder.wrl";
250  }else{
251  modelFileCao = "/ViSP-images/mbt/cube.cao";
252  modelFileWrl = "/ViSP-images/mbt/cube.wrl";
253  }
254 
255  if(!opt_ipath.empty()){
256  if(cao3DModel){
257  std::cout << "use cao" << std::endl;
258  modelFile = opt_ipath + vpIoTools::path(modelFileCao);
259  }
260  else{
261 #ifdef VISP_HAVE_COIN
262  std::cout << "use wrl" << std::endl;
263  modelFile = opt_ipath + vpIoTools::path(modelFileWrl);
264 #else
265  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
266  modelFile = opt_ipath + vpIoTools::path(modelFileCao);
267 #endif
268  }
269  }
270  else{
271  if(cao3DModel){
272  std::cout << "use cao" << std::endl;
273  modelFile = env_ipath + vpIoTools::path(modelFileCao);
274  }
275  else{
276 #ifdef VISP_HAVE_COIN
277  std::cout << "use wrl" << std::endl;
278  modelFile = env_ipath + vpIoTools::path(modelFileWrl);
279 #else
280  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
281  modelFile = env_ipath + vpIoTools::path(modelFileCao);
282 #endif
283  }
284  }
285  }
286 
287  if (!opt_initFile.empty())
288  initFile = opt_initFile;
289  else if (!opt_ipath.empty())
290  initFile = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube");
291  else
292  initFile = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube");
293 
295  vpVideoReader reader;
296 
297  reader.setFileName(ipath.c_str());
298  try{
299  reader.open(I);
300  }catch(...){
301  std::cout << "Cannot open sequence: " << ipath << std::endl;
302  return -1;
303  }
304 
305  reader.acquire(I);
306 
307  // initialise a display
308 #if defined VISP_HAVE_X11
309  vpDisplayX display;
310 #elif defined VISP_HAVE_GDI
311  vpDisplayGDI display;
312 #elif defined VISP_HAVE_OPENCV
313  vpDisplayOpenCV display;
314 #elif defined VISP_HAVE_D3D9
315  vpDisplayD3D display;
316 #elif defined VISP_HAVE_GTK
317  vpDisplayGTK display;
318 #else
319  opt_display = false;
320 #endif
321  if (opt_display)
322  {
323 #if (defined VISP_HAVE_DISPLAY)
324  display.init(I, 100, 100, "Test tracking") ;
325 #endif
326  vpDisplay::display(I) ;
327  vpDisplay::flush(I);
328  }
329 
330  vpMbEdgeTracker tracker;
332 
333  // Initialise the tracker: camera parameters, moving edge and KLT settings
334  vpCameraParameters cam;
335 #if defined (VISP_HAVE_XML2)
336  // From the xml file
337  tracker.loadConfigFile(configFile.c_str());
338 #else
339  // By setting the parameters:
340  cam.initPersProjWithoutDistortion(547, 542, 338, 234);
341 
342  vpMe me;
343  me.setMaskSize(5);
344  me.setMaskNumber(180);
345  me.setRange(7);
346  me.setThreshold(5000);
347  me.setMu1(0.5);
348  me.setMu2(0.5);
349  me.setMinSampleStep(4);
350  me.setNbTotalSample(250);
351 
352  tracker.setCameraParameters(cam);
353  tracker.setMovingEdge(me);
354 
355  // Specify the clipping to use
356  tracker.setNearClippingDistance(0.01);
357  tracker.setFarClippingDistance(0.90);
359  // tracker.setClipping(tracker.getClipping() | vpMbtPolygon::LEFT_CLIPPING | vpMbtPolygon::RIGHT_CLIPPING | vpMbtPolygon::UP_CLIPPING | vpMbtPolygon::DOWN_CLIPPING); // Equivalent to FOV_CLIPPING
360 #endif
361 
362  // Display the moving edges, see documentation for the significations of the colour
363  tracker.setDisplayMovingEdges(displayMovingEdge);
364 
365  // Tells if the tracker has to use Ogre3D for visibility tests
366  tracker.setOgreVisibilityTest(useOgre);
367 
368  // Retrieve the camera parameters from the tracker
369  tracker.getCameraParameters(cam);
370 
371  // Loop to position the cube
372  if (opt_display && opt_click_allowed)
373  {
374  while(!vpDisplay::getClick(I,false)){
377  "click after positioning the object",
378  vpColor::red);
379  vpDisplay::flush(I) ;
380  }
381  }
382 
383  // Load the 3D model (either a vrml file or a .cao file)
384  tracker.loadModel(modelFile.c_str());
385 
386  // Initialise the tracker by clicking on the image
387  // This function looks for
388  // - a ./cube/cube.init file that defines the 3d coordinates (in meter, in the object basis) of the points used for the initialisation
389  // - a ./cube/cube.ppm file to display where the user have to click (optionnal, set by the third parameter)
390  if (opt_display && opt_click_allowed)
391  {
392  tracker.initClick(I, initFile.c_str(), true);
393  tracker.getPose(cMo);
394  // display the 3D model at the given pose
395  tracker.display(I,cMo, cam, vpColor::red);
396  }
397  else
398  {
399  vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
400  tracker.initFromPose(I, cMoi);
401  }
402 
403  //track the model
404  tracker.track(I);
405  tracker.getPose(cMo);
406 
407  if (opt_display)
408  vpDisplay::flush(I);
409 
410  // Uncomment if you want to compute the covariance matrix.
411  // tracker.setCovarianceComputation(true); //Important if you want tracker.getCovarianceMatrix() to work.
412 
413  while (!reader.end())
414  {
415  // acquire a new image
416  reader.acquire(I);
417  // display the image
418  if (opt_display)
420  // track the object
421  tracker.track(I);
422  tracker.getPose(cMo);
423  // display the 3D model
424  if (opt_display)
425  {
426  tracker.display(I, cMo, cam, vpColor::darkRed);
427  // display the frame
428  vpDisplay::displayFrame (I, cMo, cam, 0.05, vpColor::blue);
429  }
430 
431  // Uncomment if you want to print the covariance matrix.
432  // Make sure tracker.setCovarianceComputation(true) has been called (uncomment below).
433  // std::cout << tracker.getCovarianceMatrix() << std::endl << std::endl;
434  vpDisplay::flush(I) ;
435  }
436  reader.close();
437 
438 #if defined (VISP_HAVE_XML2)
439  // Cleanup memory allocated by xml library used to parse the xml config file in vpMbEdgeTracker::loadConfigFile()
441 #endif
442 
443 #ifdef VISP_HAVE_COIN
444  // Cleanup memory allocated by Coin library used to load a vrml model in vpMbEdgeTracker::loadModel()
445  SoDB::finish();
446 #endif
447 
448  return 0;
449  }
450  catch(vpException e) {
451  std::cout << "Catch an exception: " << e << std::endl;
452  return 1;
453  }
454 }
455 
456 #else
457 
458 int main()
459 {
460  std::cout << "Display is required to run this example." << std::endl;
461  return 0;
462 }
463 
464 #endif
465 
void setDisplayMovingEdges(const bool displayMe)
void setMovingEdge(const vpMe &me)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void track(const vpImage< unsigned char > &I)
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:480
static const vpColor darkRed
Definition: vpColor.h:168
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setNbTotalSample(const int &nb)
Definition: vpMe.h:199
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
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
Make the complete tracking of an object by using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
void loadConfigFile(const std::string &configFile)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
void setMu1(const double &mu_1)
Definition: vpMe.h:171
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:167
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
virtual void setNearClippingDistance(const double &dist)
void open(vpImage< vpRGBa > &I)
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:488
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:177
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:158
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void acquire(vpImage< vpRGBa > &I)
virtual void setFarClippingDistance(const double &dist)
void setFileName(const char *filename)
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, const bool displayHelp=false)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay.cpp:371
virtual void setOgreVisibilityTest(const bool &v)
static void cleanup()
Definition: vpXmlParser.h:220
void setMu2(const double &mu_2)
Definition: vpMe.h:185
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
void setThreshold(const double &t)
Definition: vpMe.h:299
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)
virtual unsigned int getClipping() const
virtual bool getClick(bool blocking=true)=0
void loadModel(const std::string &cad_name)
void setRange(const unsigned int &r)
Definition: vpMe.h:229
virtual void setClipping(const unsigned int &flags)
void setMinSampleStep(const double &min)
Definition: vpMe.h:257
static const vpColor blue
Definition: vpColor.h:173