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