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