Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
mbtGenericTracking2.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Example of Hybrid Tracking of MBT and MBT KTL.
33  *
34 *****************************************************************************/
35 
43 #include <iostream>
44 #include <visp3/core/vpConfig.h>
45 
46 #if (defined(VISP_HAVE_MODULE_MBT) && defined(VISP_HAVE_DISPLAY)) && \
47  (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
48 
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpHomogeneousMatrix.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/core/vpMath.h>
53 #include <visp3/gui/vpDisplayD3D.h>
54 #include <visp3/gui/vpDisplayGDI.h>
55 #include <visp3/gui/vpDisplayGTK.h>
56 #include <visp3/gui/vpDisplayOpenCV.h>
57 #include <visp3/gui/vpDisplayX.h>
58 #include <visp3/io/vpImageIo.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/io/vpVideoReader.h>
61 #include <visp3/mbt/vpMbGenericTracker.h>
62 
63 #define GETOPTARGS "x:m:i:n:de:chtfColwvpT:"
64 
65 #ifdef ENABLE_VISP_NAMESPACE
66 using namespace VISP_NAMESPACE_NAME;
67 #endif
68 
69 void usage(const char *name, const char *badparam)
70 {
71 #if VISP_HAVE_DATASET_VERSION >= 0x030600
72  std::string ext("png");
73 #else
74  std::string ext("pgm");
75 #endif
76  fprintf(stdout, "\n\
77 Example of tracking based on the 3D model.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-i <test image path>] [-x <config file>]\n\
81  [-m <model name>] [-n <initialisation file base name>] [-e <last frame index>]\n\
82  [-t] [-c] [-d] [-h] [-f] [-C] [-o] [-w] [-l] [-v] [-p]\n\
83  [-T <tracker type>]\n",
84  name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: \n\
88  -i <input image path> \n\
89  Set image input path.\n\
90  From this path read images \n\
91  \"mbt/cube/image%%04d.%s\". These \n\
92  images come from visp-images-x.y.z.tar.gz available \n\
93  on the ViSP website.\n\
94  Setting the VISP_INPUT_IMAGE_PATH environment\n\
95  variable produces the same behavior than using\n\
96  this option.\n\
97 \n\
98  -x <config file> \n\
99  Set the config file (the xml file) to use.\n\
100  The config file is used to specify the parameters of the tracker.\n\
101 \n\
102  -m <model name> \n\
103  Specify the name of the file of the model\n\
104  The model can either be a vrml model (.wrl) or a .cao file.\n\
105 \n\
106  -e <last frame index> \n\
107  Specify the index of the last frame. Once reached, the tracking is stopped\n\
108 \n\
109  -f \n\
110  Do not use the vrml model, use the .cao one. These two models are \n\
111  equivalent and comes from ViSP-images-x.y.z.tar.gz available on the ViSP\n\
112  website. However, the .cao model allows to use the 3d model based tracker \n\
113  without Coin.\n\
114 \n\
115  -C \n\
116  Track only the cube (not the cylinder). In this case the models files are\n\
117  cube.cao or cube.wrl instead of cube_and_cylinder.cao and \n\
118  cube_and_cylinder.wrl.\n\
119 \n\
120  -n <initialisation file base name> \n\
121  Base name of the initialisation file. The file will be 'base_name'.init .\n\
122  This base name is also used for the optional picture specifying where to \n\
123  click (a .ppm picture).\n\
124 \n\
125  -t \n\
126  Turn off the display of the the moving edges and Klt points. \n\
127 \n\
128  -d \n\
129  Turn off the display.\n\
130 \n\
131  -c\n\
132  Disable the mouse click. Useful to automate the \n\
133  execution of this program without human intervention.\n\
134 \n\
135  -o\n\
136  Use Ogre3D for visibility tests\n\
137 \n\
138  -w\n\
139  When Ogre3D is enable [-o] show Ogre3D configuration dialog that allows to set the renderer.\n\
140 \n\
141  -l\n\
142  Use the scanline for visibility tests.\n\
143 \n\
144  -v\n\
145  Compute covariance matrix.\n\
146 \n\
147  -p\n\
148  Compute gradient projection error.\n\
149 \n\
150  -T <tracker type>\n\
151  Set tracker type (<1 (Edge)>, <2 (KLT)>, <3 (EdgeKlt)>).\n\
152 \n\
153  -h \n\
154  Print the help.\n\n", ext.c_str());
155 
156  if (badparam)
157  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
158 }
159 
160 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile,
161  std::string &initFile, long &lastFrame, bool &displayFeatures, bool &click_allowed, bool &display,
162  bool &cao3DModel, bool &trackCylinder, bool &useOgre, bool &showOgreConfigDialog, bool &useScanline,
163  bool &computeCovariance, bool &projectionError, int &trackerType)
164 {
165  const char *optarg_;
166  int c;
167  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
168 
169  switch (c) {
170  case 'e':
171  lastFrame = atol(optarg_);
172  break;
173  case 'i':
174  ipath = optarg_;
175  break;
176  case 'x':
177  configFile = optarg_;
178  break;
179  case 'm':
180  modelFile = optarg_;
181  break;
182  case 'n':
183  initFile = optarg_;
184  break;
185  case 't':
186  displayFeatures = false;
187  break;
188  case 'f':
189  cao3DModel = true;
190  break;
191  case 'c':
192  click_allowed = false;
193  break;
194  case 'd':
195  display = false;
196  break;
197  case 'C':
198  trackCylinder = false;
199  break;
200  case 'o':
201  useOgre = true;
202  break;
203  case 'l':
204  useScanline = true;
205  break;
206  case 'w':
207  showOgreConfigDialog = true;
208  break;
209  case 'v':
210  computeCovariance = true;
211  break;
212  case 'p':
213  projectionError = true;
214  break;
215  case 'T':
216  trackerType = atoi(optarg_);
217  break;
218  case 'h':
219  usage(argv[0], nullptr);
220  return false;
221  break;
222 
223  default:
224  usage(argv[0], optarg_);
225  return false;
226  break;
227  }
228  }
229 
230  if ((c == 1) || (c == -1)) {
231  // standalone param or error
232  usage(argv[0], nullptr);
233  std::cerr << "ERROR: " << std::endl;
234  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
235  return false;
236  }
237 
238  return true;
239 }
240 
241 int main(int argc, const char **argv)
242 {
243  try {
244  std::string env_ipath;
245  std::string opt_ipath;
246  std::string ipath;
247  std::string opt_configFile;
248  std::string opt_modelFile;
249  std::string modelFile;
250  std::string opt_initFile;
251  std::string initFile;
252  long opt_lastFrame = -1;
253  bool displayFeatures = true;
254  bool opt_click_allowed = true;
255  bool opt_display = true;
256  bool cao3DModel = false;
257  bool trackCylinder = true;
258  bool useOgre = false;
259  bool showOgreConfigDialog = false;
260  bool useScanline = false;
261  bool computeCovariance = false;
262  bool projectionError = false;
263  int trackerType = vpMbGenericTracker::EDGE_TRACKER;
264 
265 #if VISP_HAVE_DATASET_VERSION >= 0x030600
266  std::string ext("png");
267 #else
268  std::string ext("pgm");
269 #endif
270 
271  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
272  // environment variable value
273  env_ipath = vpIoTools::getViSPImagesDataPath();
274 
275  // Set the default input path
276  if (!env_ipath.empty())
277  ipath = env_ipath;
278 
279  // Read the command line options
280  if (!getOptions(argc, argv, opt_ipath, opt_configFile, opt_modelFile, opt_initFile, opt_lastFrame, displayFeatures,
281  opt_click_allowed, opt_display, cao3DModel, trackCylinder, useOgre, showOgreConfigDialog,
282  useScanline, computeCovariance, projectionError, trackerType)) {
283  return EXIT_FAILURE;
284  }
285 
286  // Test if an input path is set
287  if (opt_ipath.empty() && env_ipath.empty()) {
288  usage(argv[0], nullptr);
289  std::cerr << std::endl << "ERROR:" << std::endl;
290  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
291  << " environment variable to specify the location of the " << std::endl
292  << " image path where test images are located." << std::endl
293  << std::endl;
294 
295  return EXIT_FAILURE;
296  }
297 
298  // Get the option values
299  if (!opt_ipath.empty())
300  ipath = vpIoTools::createFilePath(opt_ipath, "mbt/cube/image%04d." + ext);
301  else
302  ipath = vpIoTools::createFilePath(env_ipath, "mbt/cube/image%04d." + ext);
303 
304 #if defined(VISP_HAVE_PUGIXML)
305  std::string configFile;
306  if (!opt_configFile.empty())
307  configFile = opt_configFile;
308  else if (!opt_ipath.empty())
309  configFile = vpIoTools::createFilePath(opt_ipath, "mbt/cube.xml");
310  else
311  configFile = vpIoTools::createFilePath(env_ipath, "mbt/cube.xml");
312 #endif
313 
314  if (!opt_modelFile.empty()) {
315  modelFile = opt_modelFile;
316  }
317  else {
318  std::string modelFileCao;
319  std::string modelFileWrl;
320  if (trackCylinder) {
321  modelFileCao = "mbt/cube_and_cylinder.cao";
322  modelFileWrl = "mbt/cube_and_cylinder.wrl";
323  }
324  else {
325  modelFileCao = "mbt/cube.cao";
326  modelFileWrl = "mbt/cube.wrl";
327  }
328 
329  if (!opt_ipath.empty()) {
330  if (cao3DModel) {
331  modelFile = vpIoTools::createFilePath(opt_ipath, modelFileCao);
332  }
333  else {
334 #ifdef VISP_HAVE_COIN3D
335  modelFile = vpIoTools::createFilePath(opt_ipath, modelFileWrl);
336 #else
337  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
338  modelFile = vpIoTools::createFilePath(opt_ipath, modelFileCao);
339 #endif
340  }
341  }
342  else {
343  if (cao3DModel) {
344  modelFile = vpIoTools::createFilePath(env_ipath, modelFileCao);
345  }
346  else {
347 #ifdef VISP_HAVE_COIN3D
348  modelFile = vpIoTools::createFilePath(env_ipath, modelFileWrl);
349 #else
350  std::cerr << "Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
351  modelFile = vpIoTools::createFilePath(env_ipath, modelFileCao);
352 #endif
353  }
354  }
355  }
356 
357  if (!opt_initFile.empty())
358  initFile = opt_initFile;
359  else if (!opt_ipath.empty())
360  initFile = vpIoTools::createFilePath(opt_ipath, "mbt/cube");
361  else
362  initFile = vpIoTools::createFilePath(env_ipath, "mbt/cube");
363 
364  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
365  vpImage<unsigned char> I1, I2, I3;
366  vpVideoReader reader;
367 
368  reader.setFileName(ipath);
369  try {
370  reader.open(I1);
371  I2 = I1;
372  I3 = I1;
373  }
374  catch (...) {
375  std::cerr << "Cannot open sequence: " << ipath << std::endl;
376  return EXIT_FAILURE;
377  }
378 
379  if (opt_lastFrame > 1 && opt_lastFrame < reader.getLastFrameIndex())
380  reader.setLastFrameIndex(opt_lastFrame);
381 
382  reader.acquire(I1);
383  I2 = I1;
384  I3 = I1;
385 
386  mapOfImages["Camera1"] = &I1;
387  mapOfImages["Camera2"] = &I2;
388  mapOfImages["Camera3"] = &I3;
389 
390  // initialise a display
391 #if defined(VISP_HAVE_X11)
392  vpDisplayX display1, display2, display3;
393 #elif defined(VISP_HAVE_GDI)
394  vpDisplayGDI display1, display2, display3;
395 #elif defined(HAVE_OPENCV_HIGHGUI)
396  vpDisplayOpenCV display1, display2, display3;
397 #elif defined(VISP_HAVE_D3D9)
398  vpDisplayD3D display1, display2, display3;
399 #elif defined(VISP_HAVE_GTK)
400  vpDisplayGTK display1, display2, display3;
401 #else
402  opt_display = false;
403 #endif
404 
405  if (opt_display) {
406 #if defined(VISP_HAVE_DISPLAY)
408  display2.setDownScalingFactor(vpDisplay::SCALE_AUTO);
409  display3.setDownScalingFactor(vpDisplay::SCALE_AUTO);
410 
411  display1.init(I1, 100, 100, "Test tracking (Cam1)");
412  display2.init(I2, (int)(I1.getWidth() / vpDisplay::getDownScalingFactor(I1)) + 110, 100, "Test tracking (Cam2)");
413  display3.init(I3, 100, (int)(I1.getHeight() / vpDisplay::getDownScalingFactor(I1)) + 110, "Test tracking (Cam3)");
414 #endif
415  vpDisplay::display(I1);
416  vpDisplay::display(I2);
417  vpDisplay::display(I3);
418 
419  vpDisplay::flush(I1);
420  vpDisplay::flush(I2);
421  vpDisplay::flush(I3);
422  }
423 
424  // Object pointer to check that inheritance is ok
425  vpMbTracker *tracker = new vpMbGenericTracker(3, trackerType);
426  std::map<std::string, vpHomogeneousMatrix> mapOfCameraPoses;
427  std::map<std::string, vpCameraParameters> mapOfCameraParams;
428 
429  // Initialise the tracker: camera parameters, moving edge and KLT settings
430 #if defined(VISP_HAVE_PUGIXML)
431  // From the xml file
432  std::map<std::string, std::string> mapOfConfigFiles;
433  mapOfConfigFiles["Camera1"] = configFile;
434  mapOfConfigFiles["Camera2"] = configFile;
435  mapOfConfigFiles["Camera3"] = configFile;
436  dynamic_cast<vpMbGenericTracker *>(tracker)->loadConfigFile(mapOfConfigFiles);
437 #else
438  // By setting the parameters:
439  vpCameraParameters cam;
440  cam.initPersProjWithoutDistortion(547, 542, 338, 234);
441  mapOfCameraParams["Camera1"] = cam;
442  mapOfCameraParams["Camera2"] = cam;
443  mapOfCameraParams["Camera3"] = cam;
444 
445  vpMe me;
446  me.setMaskSize(5);
447  me.setMaskNumber(180);
448  me.setRange(7);
450  me.setThreshold(10);
451  me.setMu1(0.5);
452  me.setMu2(0.5);
453  me.setSampleStep(4);
454  std::map<std::string, vpMe> mapOfMe;
455  mapOfMe["Camera1"] = me;
456  mapOfMe["Camera2"] = me;
457  mapOfMe["Camera3"] = me;
458 
459 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
460  vpKltOpencv klt;
461  klt.setMaxFeatures(10000);
462  klt.setWindowSize(5);
463  klt.setQuality(0.01);
464  klt.setMinDistance(5);
465  klt.setHarrisFreeParameter(0.01);
466  klt.setBlockSize(3);
467  klt.setPyramidLevels(3);
468  std::map<std::string, vpKltOpencv> mapOfKlt;
469  mapOfKlt["Camera1"] = klt;
470  mapOfKlt["Camera2"] = klt;
471  mapOfKlt["Camera3"] = klt;
472 
473  dynamic_cast<vpMbGenericTracker *>(tracker)->setKltOpencv(mapOfKlt);
474  dynamic_cast<vpMbGenericTracker *>(tracker)->setKltMaskBorder(5);
475 #endif
476 
477  dynamic_cast<vpMbGenericTracker *>(tracker)->setCameraParameters(mapOfCameraParams);
478  dynamic_cast<vpMbGenericTracker *>(tracker)->setMovingEdge(mapOfMe);
479  tracker->setAngleAppear(vpMath::rad(65));
480  tracker->setAngleDisappear(vpMath::rad(75));
481 
482  // Specify the clipping to
483  tracker->setNearClippingDistance(0.01);
484  tracker->setFarClippingDistance(0.90);
485 
486  std::map<std::string, unsigned int> mapOfClippingFlags;
487  dynamic_cast<vpMbGenericTracker *>(tracker)->getClipping(mapOfClippingFlags);
488  for (std::map<std::string, unsigned int>::iterator it = mapOfClippingFlags.begin(); it != mapOfClippingFlags.end();
489  ++it) {
490  it->second = (it->second | vpMbtPolygon::FOV_CLIPPING);
491  }
492 
493  dynamic_cast<vpMbGenericTracker *>(tracker)->setClipping(mapOfClippingFlags);
494  // tracker->setClipping(tracker->getClipping() | vpMbtPolygon::LEFT_CLIPPING
495  // | vpMbtPolygon::RIGHT_CLIPPING | vpMbtPolygon::UP_CLIPPING |
496  // vpMbtPolygon::DOWN_CLIPPING); // Equivalent to FOV_CLIPPING
497 #endif
498 
499  // Display the moving edges, and the Klt points
500  tracker->setDisplayFeatures(displayFeatures);
501 
502  // Tells if the tracker has to use Ogre3D for visibility tests
503  tracker->setOgreVisibilityTest(useOgre);
504  if (useOgre)
505  tracker->setOgreShowConfigDialog(showOgreConfigDialog);
506 
507  // Tells if the tracker has to use the scanline visibility tests
508  tracker->setScanLineVisibilityTest(useScanline);
509 
510  // Tells if the tracker has to compute the covariance matrix
511  tracker->setCovarianceComputation(computeCovariance);
512 
513  // Tells if the tracker has to compute the projection error
514  tracker->setProjectionErrorComputation(projectionError);
515 
516  // Retrieve the camera parameters from the tracker
517  dynamic_cast<vpMbGenericTracker *>(tracker)->getCameraParameters(mapOfCameraParams);
518 
519  // Loop to position the cube
520  if (opt_display && opt_click_allowed) {
521  while (!vpDisplay::getClick(I1, false)) {
522  vpDisplay::display(I1);
523  vpDisplay::displayText(I1, 15, 10, "click after positioning the object", vpColor::red);
524  vpDisplay::flush(I1);
525  }
526  }
527 
528  // Load the 3D model (either a vrml file or a .cao file)
529  tracker->loadModel(modelFile);
530 
531  // Initialise the tracker by clicking on the image
532  // This function looks for
533  // - a ./cube/cube.init file that defines the 3d coordinates (in meter,
534  // in the object basis) of the points used for the initialisation
535  // - a ./cube/cube.ppm file to display where the user have to click
536  // (Optional, set by the third parameter)
537  if (opt_display && opt_click_allowed) {
538  std::map<std::string, std::string> mapOfInitFiles;
539  mapOfInitFiles["Camera1"] = initFile;
540 
541  dynamic_cast<vpMbGenericTracker *>(tracker)->initClick(mapOfImages, mapOfInitFiles, true);
542  dynamic_cast<vpMbGenericTracker *>(tracker)->getPose(mapOfCameraPoses);
543 
544  // display the 3D model at the given pose
545  dynamic_cast<vpMbGenericTracker *>(tracker)->display(mapOfImages, mapOfCameraPoses, mapOfCameraParams,
546  vpColor::red);
547  }
548  else {
549  vpHomogeneousMatrix c1Moi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
550  std::map<std::string, vpHomogeneousMatrix> mapOfInitPoses;
551  mapOfInitPoses["Camera1"] = c1Moi;
552 
553  dynamic_cast<vpMbGenericTracker *>(tracker)->initFromPose(mapOfImages, mapOfInitPoses);
554  }
555 
556  // track the model
557  dynamic_cast<vpMbGenericTracker *>(tracker)->track(mapOfImages);
558  dynamic_cast<vpMbGenericTracker *>(tracker)->getPose(mapOfCameraPoses);
559 
560  if (opt_display) {
561  vpDisplay::flush(I1);
562  vpDisplay::flush(I2);
563  vpDisplay::flush(I3);
564  }
565 
566  bool quit = false, click = false;
567  while (!reader.end() && !quit) {
568  // acquire a new image
569  reader.acquire(I1);
570  I2 = I1;
571  I3 = I1;
572  mapOfImages["Camera1"] = &I1;
573  mapOfImages["Camera2"] = &I2;
574  mapOfImages["Camera3"] = &I3;
575 
576  // display the image
577  if (opt_display) {
578  vpDisplay::display(I1);
579  vpDisplay::display(I2);
580  vpDisplay::display(I3);
581 
582  std::stringstream ss;
583  ss << "Num frame: " << reader.getFrameIndex() << "/" << reader.getLastFrameIndex();
584  vpDisplay::displayText(I1, 40, 20, ss.str(), vpColor::red);
585  }
586 
587  // Test to reset the tracker
588  if (reader.getFrameIndex() == reader.getFirstFrameIndex() + 10) {
589  std::cout << "----------Test reset tracker----------" << std::endl;
590  if (opt_display) {
591  vpDisplay::display(I1);
592  vpDisplay::display(I2);
593  vpDisplay::display(I3);
594  }
595 
596  tracker->resetTracker();
597 #if defined(VISP_HAVE_PUGIXML)
598  dynamic_cast<vpMbGenericTracker *>(tracker)->loadConfigFile(mapOfConfigFiles);
599 #else
600  // By setting the parameters:
601  cam.initPersProjWithoutDistortion(547, 542, 338, 234);
602  mapOfCameraParams["Camera1"] = cam;
603  mapOfCameraParams["Camera2"] = cam;
604  mapOfCameraParams["Camera3"] = cam;
605 
606  me.setMaskSize(5);
607  me.setMaskNumber(180);
608  me.setRange(7);
610  me.setThreshold(10);
611  me.setMu1(0.5);
612  me.setMu2(0.5);
613  me.setSampleStep(4);
614 
615  mapOfMe["Camera1"] = me;
616  mapOfMe["Camera2"] = me;
617  mapOfMe["Camera3"] = me;
618 
619 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
620  klt.setMaxFeatures(10000);
621  klt.setWindowSize(5);
622  klt.setQuality(0.01);
623  klt.setMinDistance(5);
624  klt.setHarrisFreeParameter(0.01);
625  klt.setBlockSize(3);
626  klt.setPyramidLevels(3);
627 
628  mapOfKlt["Camera1"] = klt;
629  mapOfKlt["Camera2"] = klt;
630  mapOfKlt["Camera3"] = klt;
631 
632  dynamic_cast<vpMbGenericTracker *>(tracker)->setKltOpencv(mapOfKlt);
633  dynamic_cast<vpMbGenericTracker *>(tracker)->setKltMaskBorder(5);
634 #endif
635 
636  dynamic_cast<vpMbGenericTracker *>(tracker)->setCameraParameters(mapOfCameraParams);
637  dynamic_cast<vpMbGenericTracker *>(tracker)->setMovingEdge(mapOfMe);
638  tracker->setAngleAppear(vpMath::rad(65));
639  tracker->setAngleDisappear(vpMath::rad(75));
640 
641  // Specify the clipping to
642  tracker->setNearClippingDistance(0.01);
643  tracker->setFarClippingDistance(0.90);
644 
645  dynamic_cast<vpMbGenericTracker *>(tracker)->getClipping(mapOfClippingFlags);
646  for (std::map<std::string, unsigned int>::iterator it = mapOfClippingFlags.begin();
647  it != mapOfClippingFlags.end(); ++it) {
648  it->second = (it->second | vpMbtPolygon::FOV_CLIPPING);
649  }
650 
651  dynamic_cast<vpMbGenericTracker *>(tracker)->setClipping(mapOfClippingFlags);
652  // tracker->setClipping(tracker->getClipping() | vpMbtPolygon::LEFT_CLIPPING
653  // | vpMbtPolygon::RIGHT_CLIPPING | vpMbtPolygon::UP_CLIPPING |
654  // vpMbtPolygon::DOWN_CLIPPING); // Equivalent to FOV_CLIPPING
655 #endif
656  tracker->loadModel(modelFile);
657  dynamic_cast<vpMbGenericTracker *>(tracker)->setCameraParameters(mapOfCameraParams);
658  tracker->setOgreVisibilityTest(useOgre);
659  tracker->setScanLineVisibilityTest(useScanline);
660  tracker->setCovarianceComputation(computeCovariance);
661  tracker->setProjectionErrorComputation(projectionError);
662  dynamic_cast<vpMbGenericTracker *>(tracker)->initFromPose(mapOfImages, mapOfCameraPoses);
663  }
664 
665  // Test to set an initial pose
666  if (reader.getFrameIndex() == reader.getFirstFrameIndex() + 50) {
667  vpHomogeneousMatrix c1Moi;
668  c1Moi.buildFrom(0.0439540832, 0.0845870108, 0.5477322481, 2.179498458, 0.8611798108, -0.3491961946);
669  std::map<std::string, vpHomogeneousMatrix> mapOfSetPoses;
670  mapOfSetPoses["Camera1"] = c1Moi;
671 
672  std::cout << "Test set pose" << std::endl;
673  dynamic_cast<vpMbGenericTracker *>(tracker)->setPose(mapOfImages, mapOfSetPoses);
674  }
675 
676  // track the object: stop tracking from frame 40 to 50
677  if (reader.getFrameIndex() - reader.getFirstFrameIndex() < 40 ||
678  reader.getFrameIndex() - reader.getFirstFrameIndex() >= 50) {
679  dynamic_cast<vpMbGenericTracker *>(tracker)->track(mapOfImages);
680  dynamic_cast<vpMbGenericTracker *>(tracker)->getPose(mapOfCameraPoses);
681  if (opt_display) {
682  // display the 3D model
683  if (reader.getFrameIndex() - reader.getFirstFrameIndex() >= 50) {
684  std::map<std::string, const vpImage<unsigned char> *> mapOfSubImages;
685  mapOfSubImages["Camera1"] = &I1;
686  mapOfSubImages["Camera2"] = &I2;
687 
688  dynamic_cast<vpMbGenericTracker *>(tracker)->display(mapOfSubImages, mapOfCameraPoses, mapOfCameraParams,
689  vpColor::red, 3);
690  }
691  else {
692  dynamic_cast<vpMbGenericTracker *>(tracker)->display(mapOfImages, mapOfCameraPoses, mapOfCameraParams,
693  vpColor::red, 3);
694  }
695  // display the frame
696  vpDisplay::displayFrame(I1, mapOfCameraPoses["Camera1"], mapOfCameraParams["Camera1"], 0.05);
697  vpDisplay::displayFrame(I2, mapOfCameraPoses["Camera2"], mapOfCameraParams["Camera2"], 0.05);
698  vpDisplay::displayFrame(I3, mapOfCameraPoses["Camera3"], mapOfCameraParams["Camera3"], 0.05);
699  }
700  }
701 
702  if (opt_click_allowed && opt_display) {
703  vpDisplay::displayText(I1, 10, 10, "Click to quit", vpColor::red);
705  if (vpDisplay::getClick(I1, button, click)) {
706  switch (button) {
708  quit = !click;
709  break;
710 
712  click = !click;
713  break;
714 
715  default:
716  break;
717  }
718  }
719  }
720 
721  if (computeCovariance) {
722  std::cout << "Covariance matrix: \n" << tracker->getCovarianceMatrix() << std::endl << std::endl;
723  }
724 
725  if (projectionError) {
726  std::cout << "Projection error: " << tracker->getProjectionError() << std::endl << std::endl;
727  }
728 
729  if (opt_display) {
730  vpDisplay::flush(I1);
731  vpDisplay::flush(I2);
732  vpDisplay::flush(I3);
733  }
734  }
735 
736  std::cout << "Reached last frame: " << reader.getFrameIndex() << std::endl;
737  std::cout << "\nFinal poses, c1Mo:\n"
738  << mapOfCameraPoses["Camera1"] << "\nc2Mo:\n"
739  << mapOfCameraPoses["Camera2"] << "\nc3Mo:\n"
740  << mapOfCameraPoses["Camera3"] << std::endl;
741 
742  if (opt_click_allowed && !quit) {
744  }
745  reader.close();
746 
747  delete tracker;
748  tracker = nullptr;
749 
750  return EXIT_SUCCESS;
751  }
752  catch (const vpException &e) {
753  std::cout << "Catch an exception: " << e << std::endl;
754  return EXIT_FAILURE;
755  }
756 }
757 
758 #elif !(defined(VISP_HAVE_MODULE_MBT) && defined(VISP_HAVE_DISPLAY))
759 int main()
760 {
761  std::cout << "Cannot run this example: visp_mbt, visp_gui modules are required." << std::endl;
762  return EXIT_SUCCESS;
763 }
764 #else
765 int main()
766 {
767  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
768  return EXIT_SUCCESS;
769 }
770 #endif
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:106
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:233
static void display(const vpImage< unsigned char > &I)
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, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
@ SCALE_AUTO
Definition: vpDisplay.h:184
unsigned int getDownScalingFactor()
Definition: vpDisplay.h:221
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:74
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:267
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:356
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:324
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:377
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:343
static double rad(double deg)
Definition: vpMath.h:129
Real-time 6D object pose tracking using its CAD model.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:107
virtual void resetTracker()=0
virtual void setOgreShowConfigDialog(bool showConfigDialog)
Definition: vpMbTracker.h:652
virtual void setDisplayFeatures(bool displayF)
Definition: vpMbTracker.h:520
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:483
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:502
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:610
virtual void setOgreVisibilityTest(const bool &v)
virtual vpMatrix getCovarianceMatrix() const
Definition: vpMbTracker.h:267
virtual void setNearClippingDistance(const double &dist)
virtual void setFarClippingDistance(const double &dist)
virtual double getProjectionError() const
Definition: vpMbTracker.h:312
virtual void setProjectionErrorComputation(const bool &flag)
Definition: vpMbTracker.h:587
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:472
Definition: vpMe.h:134
void setMu1(const double &mu_1)
Definition: vpMe.h:385
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:552
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:560
void setMu2(const double &mu_2)
Definition: vpMe.h:392
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void setLastFrameIndex(const long last_frame)
long getLastFrameIndex()
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFirstFrameIndex()
long getFrameIndex() const