ViSP  2.8.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 - 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 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 
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] [-C]",
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  -C \n\
107  Track only the cube (not the cylinder). In this case the models files are\n\
108  cube.cao or cube.wrl instead of cube_and_cylinder.cao and \n\
109  cube_and_cylinder.wrl.\n\
110 \n\
111  -n <initialisation file base name> \n\
112  Base name of the initialisation file. The file will be 'base_name'.init .\n\
113  This base name is also used for the optionnal picture specifying where to \n\
114  click (a .ppm picture).\
115 \n\
116  -t \n\
117  Turn off the display of the the moving edges and Klt points. \n\
118 \n\
119  -d \n\
120  Turn off the display.\n\
121 \n\
122  -c\n\
123  Disable the mouse click. Useful to automaze the \n\
124  execution of this program without humain intervention.\n\
125 \n\
126  -o\n\
127  Use Ogre3D for visibility tests\n\
128 \n\
129  -h \n\
130  Print the help.\n\n");
131 
132  if (badparam)
133  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
134 }
135 
136 
137 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile, std::string &initFile, bool &displayFeatures, bool &click_allowed, bool &display, bool& cao3DModel, bool& trackCylinder, bool &useOgre)
138 {
139  const char *optarg;
140  int c;
141  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
142 
143  switch (c) {
144  case 'i': ipath = optarg; break;
145  case 'x': configFile = optarg; break;
146  case 'm': modelFile = optarg; break;
147  case 'n': initFile = optarg; break;
148  case 't': displayFeatures = false; break;
149  case 'f': cao3DModel = true; break;
150  case 'c': click_allowed = false; break;
151  case 'd': display = false; break;
152  case 'C': trackCylinder = 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  std::string env_ipath;
177  std::string opt_ipath;
178  std::string ipath;
179  std::string opt_configFile;
180  std::string configFile;
181  std::string opt_modelFile;
182  std::string modelFile;
183  std::string opt_initFile;
184  std::string initFile;
185  bool displayFeatures = true;
186  bool opt_click_allowed = true;
187  bool opt_display = true;
188  bool cao3DModel = false;
189  bool trackCylinder = true;
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, displayFeatures, opt_click_allowed, opt_display, cao3DModel, trackCylinder, 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;
238  std::string modelFileWrl;
239  if(trackCylinder){
240  modelFileCao = "/ViSP-images/mbt/cube_and_cylinder.cao";
241  modelFileWrl = "/ViSP-images/mbt/cube_and_cylinder.wrl";
242  }else{
243  modelFileCao = "/ViSP-images/mbt/cube.cao";
244  modelFileWrl = "/ViSP-images/mbt/cube.wrl";
245  }
246 
247  if(!opt_ipath.empty()){
248  if(cao3DModel){
249  modelFile = opt_ipath + vpIoTools::path(modelFileCao);
250  }
251  else{
252 #ifdef VISP_HAVE_COIN
253  modelFile = opt_ipath + vpIoTools::path(modelFileWrl);
254 #else
255  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
256  modelFile = opt_ipath + vpIoTools::path(modelFileCao);
257 #endif
258  }
259  }
260  else{
261  if(cao3DModel){
262  modelFile = env_ipath + vpIoTools::path(modelFileCao);
263  }
264  else{
265 #ifdef VISP_HAVE_COIN
266  modelFile = env_ipath + vpIoTools::path(modelFileWrl);
267 #else
268  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
269  modelFile = env_ipath + vpIoTools::path(modelFileCao);
270 #endif
271  }
272  }
273  }
274 
275  if (!opt_initFile.empty())
276  initFile = opt_initFile;
277  else if (!opt_ipath.empty())
278  initFile = opt_ipath + vpIoTools::path("/ViSP-images/mbt/cube");
279  else
280  initFile = env_ipath + vpIoTools::path("/ViSP-images/mbt/cube");
281 
283  vpVideoReader reader;
284 
285  reader.setFileName(ipath.c_str());
286  try{
287  reader.open(I);
288  }catch(...){
289  std::cout << "Cannot open sequence: " << ipath << std::endl;
290  return -1;
291  }
292 
293  reader.acquire(I);
294 
295  // initialise a display
296 #if defined VISP_HAVE_X11
297  vpDisplayX display;
298 #elif defined VISP_HAVE_GDI
299  vpDisplayGDI display;
300 #elif defined VISP_HAVE_OPENCV
301  vpDisplayOpenCV display;
302 #elif defined VISP_HAVE_D3D9
303  vpDisplayD3D display;
304 #elif defined VISP_HAVE_GTK
305  vpDisplayGTK display;
306 #else
307  opt_display = false;
308 #endif
309  if (opt_display)
310  {
311 #if (defined VISP_HAVE_DISPLAY)
312  display.init(I, 100, 100, "Test tracking") ;
313 #endif
314  vpDisplay::display(I) ;
315  vpDisplay::flush(I);
316  }
317 
318  vpMbEdgeKltTracker tracker;
320  vpCameraParameters cam;
321 
322  // Initialise the tracker: camera parameters, moving edge and KLT settings
323 #if defined (VISP_HAVE_XML2)
324  // From the xml file
325  tracker.loadConfigFile(configFile.c_str());
326 #else
327  // By setting the parameters:
328  cam.initPersProjWithoutDistortion(547, 542, 338, 234);
329 
330  vpMe me;
331  me.setMaskSize(5);
332  me.setMaskNumber(180);
333  me.setRange(7);
334  me.setThreshold(5000);
335  me.setMu1(0.5);
336  me.setMu2(0.5);
337  me.setMinSampleStep(4);
338  me.setNbTotalSample(250);
339 
340  vpKltOpencv klt;
341  klt.setMaxFeatures(10000);
342  klt.setWindowSize(5);
343  klt.setQuality(0.01);
344  klt.setMinDistance(5);
345  klt.setHarrisFreeParameter(0.01);
346  klt.setBlockSize(3);
347  klt.setPyramidLevels(3);
348 
349  tracker.setCameraParameters(cam);
350  tracker.setMovingEdge(me);
351  tracker.setKltOpencv(klt);
352  tracker.setAngleAppear( vpMath::rad(65) );
353  tracker.setAngleDisappear( vpMath::rad(75) );
354  tracker.setMaskBorder(5);
355 
356  // Specify the clipping to
357  tracker.setNearClippingDistance(0.01);
358  tracker.setFarClippingDistance(0.90);
360 // tracker.setClipping(tracker.getClipping() | vpMbtPolygon::LEFT_CLIPPING | vpMbtPolygon::RIGHT_CLIPPING | vpMbtPolygon::UP_CLIPPING | vpMbtPolygon::DOWN_CLIPPING); // Equivalent to FOV_CLIPPING
361 #endif
362 
363  // Display the moving edges, and the Klt points
364  tracker.setDisplayFeatures(displayFeatures);
365 
366  // Tells if the tracker has to use Ogre3D for visibility tests
367  tracker.setOgreVisibilityTest(useOgre);
368 
369  // Retrieve the camera parameters from the tracker
370  tracker.getCameraParameters(cam);
371 
372  // Loop to position the cube
373  if (opt_display && opt_click_allowed)
374  {
375  while(!vpDisplay::getClick(I,false)){
378  "click after positioning the object",
379  vpColor::red);
380  vpDisplay::flush(I) ;
381  }
382  }
383 
384  // Load the 3D model (either a vrml file or a .cao file)
385  try{
386  tracker.loadModel(modelFile.c_str());
387  }
388  catch(...)
389  {
390  return 0;
391  }
392  // Initialise the tracker by clicking on the image
393  // This function looks for
394  // - a ./cube/cube.init file that defines the 3d coordinates (in meter, in the object basis) of the points used for the initialisation
395  // - a ./cube/cube.ppm file to display where the user have to click (optionnal, set by the third parameter)
396  if (opt_display && opt_click_allowed)
397  {
398  tracker.initClick(I, initFile.c_str(), true);
399  tracker.getPose(cMo);
400  // display the 3D model at the given pose
401  tracker.display(I,cMo, cam, vpColor::red);
402  }
403  else
404  {
405  vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
406  tracker.initFromPose(I, cMoi);
407  }
408 
409  //track the model
410  tracker.track(I);
411  tracker.getPose(cMo);
412 
413  if (opt_display)
414  vpDisplay::flush(I);
415 
416  // Uncomment if you want to compute the covariance matrix.
417  // tracker.setCovarianceComputation(true); //Important if you want tracker.getCovarianceMatrix() to work.
418 
419  while (!reader.end())
420  {
421  try
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  }
444  catch(...)
445  {
446  std::cout << "error caught" << std::endl;
447  break;
448  }
449  vpDisplay::flush(I) ;
450  }
451  reader.close();
452 
453 #if defined (VISP_HAVE_XML2)
454  // Cleanup memory allocated by xml library used to parse the xml config file in vpMbEdgeKltTracker::loadConfigFile()
456 #endif
457 
458 #ifdef VISP_HAVE_COIN
459  // Cleanup memory allocated by Coin library used to load a vrml model in vpMbEdgeKltTracker::loadModel()
460  SoDB::finish();
461 #endif
462 
463  return 0;
464 }
465 
466 #else
467 
468 int main()
469 {
470  std::cout << "OpenCV and display are required." << std::endl;
471  return 0;
472 }
473 
474 #endif
void setKltOpencv(const vpKltOpencv &t)
void setMovingEdge(const vpMe &me)
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...
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:486
static const vpColor darkRed
Definition: vpColor.h:168
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
void setMu2(const double &mu2)
Definition: vpMe.h:185
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...
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:262
virtual void setClipping(const unsigned int &flags)
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
virtual void setFarClippingDistance(const double &dist)
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:494
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 void track(const vpImage< unsigned char > &I)
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:368
static double rad(double deg)
Definition: vpMath.h:100
virtual void setCameraParameters(const vpCameraParameters &cam)
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:158
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 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 getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:177
void setMinDistance(double input)
Definition: vpKltOpencv.h:242
void setMinSampleStep(const double &min)
Definition: vpMe.h:257
void setMu1(const double &mu1)
Definition: vpMe.h:171
virtual void loadModel(const std::string &modelFile)
static const vpColor blue
Definition: vpColor.h:173
void setHarrisFreeParameter(double input)
Definition: vpKltOpencv.h:226
virtual void setNearClippingDistance(const double &dist)