ViSP  2.9.0
mbtEdgeKltTracking.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 Hybrid Tracking of MBT and MBT KTL.
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/vpMbEdgeKltTracker.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:dchtfCo"
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 &displayFeatures, bool &click_allowed, bool &display,
71  bool& cao3DModel, bool& trackCylinder, 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] [-C]",
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  -C \n\
111  Track only the cube (not the cylinder). In this case the models files are\n\
112  cube.cao or cube.wrl instead of cube_and_cylinder.cao and \n\
113  cube_and_cylinder.wrl.\n\
114 \n\
115  -n <initialisation file base name> \n\
116  Base name of the initialisation file. The file will be 'base_name'.init .\n\
117  This base name is also used for the optionnal picture specifying where to \n\
118  click (a .ppm picture).\
119 \n\
120  -t \n\
121  Turn off the display of the the moving edges and Klt points. \n\
122 \n\
123  -d \n\
124  Turn off the display.\n\
125 \n\
126  -c\n\
127  Disable the mouse click. Useful to automaze the \n\
128  execution of this program without humain intervention.\n\
129 \n\
130  -o\n\
131  Use Ogre3D for visibility tests\n\
132 \n\
133  -h \n\
134  Print the help.\n\n");
135 
136  if (badparam)
137  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
138 }
139 
140 
141 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile,
142  std::string &initFile, bool &displayFeatures, bool &click_allowed, bool &display,
143  bool& cao3DModel, bool& trackCylinder, bool &useOgre)
144 {
145  const char *optarg_;
146  int c;
147  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
148 
149  switch (c) {
150  case 'i': ipath = optarg_; break;
151  case 'x': configFile = optarg_; break;
152  case 'm': modelFile = optarg_; break;
153  case 'n': initFile = optarg_; break;
154  case 't': displayFeatures = false; break;
155  case 'f': cao3DModel = true; break;
156  case 'c': click_allowed = false; break;
157  case 'd': display = false; break;
158  case 'C': trackCylinder = false; break;
159  case 'o' : useOgre = true; break;
160  case 'h': usage(argv[0], NULL); return false; break;
161 
162  default:
163  usage(argv[0], optarg_);
164  return false; break;
165  }
166  }
167 
168  if ((c == 1) || (c == -1)) {
169  // standalone param or error
170  usage(argv[0], NULL);
171  std::cerr << "ERROR: " << std::endl;
172  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
173  return false;
174  }
175 
176  return true;
177 }
178 
179 int
180 main(int argc, const char ** argv)
181 {
182  try {
183  std::string env_ipath;
184  std::string opt_ipath;
185  std::string ipath;
186  std::string opt_configFile;
187  std::string configFile;
188  std::string opt_modelFile;
189  std::string modelFile;
190  std::string opt_initFile;
191  std::string initFile;
192  bool displayFeatures = true;
193  bool opt_click_allowed = true;
194  bool opt_display = true;
195  bool cao3DModel = false;
196  bool trackCylinder = true;
197  bool useOgre = false;
198 
199  // Get the VISP_IMAGE_PATH environment variable value
200  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
201  if (ptenv != NULL)
202  env_ipath = ptenv;
203 
204  // Set the default input path
205  if (! env_ipath.empty())
206  ipath = env_ipath;
207 
208 
209  // Read the command line options
210  if (!getOptions(argc, argv, opt_ipath, opt_configFile, opt_modelFile, opt_initFile, displayFeatures, opt_click_allowed, opt_display, cao3DModel, trackCylinder, useOgre)) {
211  return (-1);
212  }
213 
214  // Test if an input path is set
215  if (opt_ipath.empty() && env_ipath.empty() ){
216  usage(argv[0], NULL);
217  std::cerr << std::endl
218  << "ERROR:" << std::endl;
219  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
220  << std::endl
221  << " environment variable to specify the location of the " << std::endl
222  << " image path where test images are located." << std::endl
223  << std::endl;
224 
225  return (-1);
226  }
227 
228  // Get the option values
229  if (!opt_ipath.empty())
230  ipath = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube/image%04d.pgm");
231  else
232  ipath = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube/image%04d.pgm");
233 
234  if (!opt_configFile.empty())
235  configFile = opt_configFile;
236  else if (!opt_ipath.empty())
237  configFile = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube.xml");
238  else
239  configFile = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube.xml");
240 
241  if (!opt_modelFile.empty()){
242  modelFile = opt_modelFile;
243  }else{
244  std::string modelFileCao;
245  std::string modelFileWrl;
246  if(trackCylinder){
247  modelFileCao = "/ViSP-images/mbt/cube_and_cylinder.cao";
248  modelFileWrl = "/ViSP-images/mbt/cube_and_cylinder.wrl";
249  }else{
250  modelFileCao = "/ViSP-images/mbt/cube.cao";
251  modelFileWrl = "/ViSP-images/mbt/cube.wrl";
252  }
253 
254  if(!opt_ipath.empty()){
255  if(cao3DModel){
256  modelFile = opt_ipath + vpIoTools::path(modelFileCao);
257  }
258  else{
259 #ifdef VISP_HAVE_COIN
260  modelFile = opt_ipath + vpIoTools::path(modelFileWrl);
261 #else
262  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
263  modelFile = opt_ipath + vpIoTools::path(modelFileCao);
264 #endif
265  }
266  }
267  else{
268  if(cao3DModel){
269  modelFile = env_ipath + vpIoTools::path(modelFileCao);
270  }
271  else{
272 #ifdef VISP_HAVE_COIN
273  modelFile = env_ipath + vpIoTools::path(modelFileWrl);
274 #else
275  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
276  modelFile = env_ipath + vpIoTools::path(modelFileCao);
277 #endif
278  }
279  }
280  }
281 
282  if (!opt_initFile.empty())
283  initFile = opt_initFile;
284  else if (!opt_ipath.empty())
285  initFile = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube");
286  else
287  initFile = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube");
288 
290  vpVideoReader reader;
291 
292  reader.setFileName(ipath.c_str());
293  try{
294  reader.open(I);
295  }catch(...){
296  std::cout << "Cannot open sequence: " << ipath << std::endl;
297  return -1;
298  }
299 
300  reader.acquire(I);
301 
302  // initialise a display
303 #if defined VISP_HAVE_X11
304  vpDisplayX display;
305 #elif defined VISP_HAVE_GDI
306  vpDisplayGDI display;
307 #elif defined VISP_HAVE_OPENCV
308  vpDisplayOpenCV display;
309 #elif defined VISP_HAVE_D3D9
310  vpDisplayD3D display;
311 #elif defined VISP_HAVE_GTK
312  vpDisplayGTK display;
313 #else
314  opt_display = false;
315 #endif
316  if (opt_display)
317  {
318 #if (defined VISP_HAVE_DISPLAY)
319  display.init(I, 100, 100, "Test tracking") ;
320 #endif
321  vpDisplay::display(I) ;
322  vpDisplay::flush(I);
323  }
324 
325  vpMbEdgeKltTracker tracker;
327  vpCameraParameters cam;
328 
329  // Initialise the tracker: camera parameters, moving edge and KLT settings
330 #if defined (VISP_HAVE_XML2)
331  // From the xml file
332  tracker.loadConfigFile(configFile.c_str());
333 #else
334  // By setting the parameters:
335  cam.initPersProjWithoutDistortion(547, 542, 338, 234);
336 
337  vpMe me;
338  me.setMaskSize(5);
339  me.setMaskNumber(180);
340  me.setRange(7);
341  me.setThreshold(5000);
342  me.setMu1(0.5);
343  me.setMu2(0.5);
344  me.setMinSampleStep(4);
345  me.setNbTotalSample(250);
346 
347  vpKltOpencv klt;
348  klt.setMaxFeatures(10000);
349  klt.setWindowSize(5);
350  klt.setQuality(0.01);
351  klt.setMinDistance(5);
352  klt.setHarrisFreeParameter(0.01);
353  klt.setBlockSize(3);
354  klt.setPyramidLevels(3);
355 
356  tracker.setCameraParameters(cam);
357  tracker.setMovingEdge(me);
358  tracker.setKltOpencv(klt);
359  tracker.setAngleAppear( vpMath::rad(65) );
360  tracker.setAngleDisappear( vpMath::rad(75) );
361  tracker.setMaskBorder(5);
362 
363  // Specify the clipping to
364  tracker.setNearClippingDistance(0.01);
365  tracker.setFarClippingDistance(0.90);
367  // tracker.setClipping(tracker.getClipping() | vpMbtPolygon::LEFT_CLIPPING | vpMbtPolygon::RIGHT_CLIPPING | vpMbtPolygon::UP_CLIPPING | vpMbtPolygon::DOWN_CLIPPING); // Equivalent to FOV_CLIPPING
368 #endif
369 
370  // Display the moving edges, and the Klt points
371  tracker.setDisplayFeatures(displayFeatures);
372 
373  // Tells if the tracker has to use Ogre3D for visibility tests
374  tracker.setOgreVisibilityTest(useOgre);
375 
376  // Retrieve the camera parameters from the tracker
377  tracker.getCameraParameters(cam);
378 
379  // Loop to position the cube
380  if (opt_display && opt_click_allowed)
381  {
382  while(!vpDisplay::getClick(I,false)){
385  "click after positioning the object",
386  vpColor::red);
387  vpDisplay::flush(I) ;
388  }
389  }
390 
391  // Load the 3D model (either a vrml file or a .cao file)
392  tracker.loadModel(modelFile.c_str());
393 
394  // Initialise the tracker by clicking on the image
395  // This function looks for
396  // - a ./cube/cube.init file that defines the 3d coordinates (in meter, in the object basis) of the points used for the initialisation
397  // - a ./cube/cube.ppm file to display where the user have to click (optionnal, set by the third parameter)
398  if (opt_display && opt_click_allowed)
399  {
400  tracker.initClick(I, initFile.c_str(), true);
401  tracker.getPose(cMo);
402  // display the 3D model at the given pose
403  tracker.display(I,cMo, cam, vpColor::red);
404  }
405  else
406  {
407  vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
408  tracker.initFromPose(I, cMoi);
409  }
410 
411  //track the model
412  tracker.track(I);
413  tracker.getPose(cMo);
414 
415  if (opt_display)
416  vpDisplay::flush(I);
417 
418  // Uncomment if you want to compute the covariance matrix.
419  // tracker.setCovarianceComputation(true); //Important if you want tracker.getCovarianceMatrix() to work.
420 
421  while (!reader.end())
422  {
423  // acquire a new image
424  reader.acquire(I);
425  // display the image
426  if (opt_display)
428  // track the object
429  tracker.track(I);
430  tracker.getPose(cMo);
431  // display the 3D model
432  if (opt_display)
433  {
434  tracker.display(I, cMo, cam, vpColor::darkRed);
435  // display the frame
436  vpDisplay::displayFrame (I, cMo, cam, 0.05, vpColor::blue);
437  }
438 
439  // Uncomment if you want to print the covariance matrix.
440  // Make sure tracker.setCovarianceComputation(true) has been called (uncomment below).
441  // std::cout << tracker.getCovarianceMatrix() << std::endl << std::endl;
442 
443  vpDisplay::flush(I) ;
444  }
445  reader.close();
446 
447 #if defined (VISP_HAVE_XML2)
448  // Cleanup memory allocated by xml library used to parse the xml config file in vpMbEdgeKltTracker::loadConfigFile()
450 #endif
451 
452 #ifdef VISP_HAVE_COIN
453  // Cleanup memory allocated by Coin library used to load a vrml model in vpMbEdgeKltTracker::loadModel()
454  SoDB::finish();
455 #endif
456 
457  return 0;
458  }
459  catch(vpException e) {
460  std::cout << "Catch an exception: " << e << std::endl;
461  return 1;
462  }
463 }
464 
465 #else
466 
467 int main()
468 {
469  std::cout << "OpenCV and display are required." << std::endl;
470  return 0;
471 }
472 
473 #endif
void setKltOpencv(const vpKltOpencv &t)
void setMovingEdge(const vpMe &me)
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...
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
void setPyramidLevels(const int input)
Definition: vpKltOpencv.h:263
virtual void setClipping(const unsigned int &flags)
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
virtual void setFarClippingDistance(const double &dist)
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)
void open(vpImage< vpRGBa > &I)
virtual void setOgreVisibilityTest(const bool &v)
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:488
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 void track(const vpImage< unsigned char > &I)
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:158
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)
Generic class defining intrinsic camera parameters.
virtual void setAngleAppear(const double &a)
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)
virtual void setAngleDisappear(const double &a)
void loadConfigFile(const char *configFile)
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 setCameraParameters(const vpCameraParameters &cam)
Hybrid tracker based on moving-edges and keypoints tracked using KLT tracker.
static void cleanup()
Definition: vpXmlParser.h:220
virtual unsigned int getClipping() const
void setMu2(const double &mu_2)
Definition: vpMe.h:185
void setMaskBorder(const unsigned int &e)
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
void setThreshold(const double &t)
Definition: vpMe.h:299
virtual bool getClick(bool blocking=true)=0
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:247
void setRange(const unsigned int &r)
Definition: vpMe.h:229
void setMaxFeatures(const int input)
void setMinDistance(double input)
Definition: vpKltOpencv.h:243
void setMinSampleStep(const double &min)
Definition: vpMe.h:257
virtual void loadModel(const std::string &modelFile)
static const vpColor blue
Definition: vpColor.h:173
void setHarrisFreeParameter(double input)
Definition: vpKltOpencv.h:227
virtual void setNearClippingDistance(const double &dist)