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