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