Visual Servoing Platform  version 3.6.1 under development (2025-03-12)
mbtGenericTrackingDepth.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 tracking with vpGenericTracker on Castel.
33  *
34 *****************************************************************************/
35 
42 #include <cstdlib>
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:X:m:M:i:n:dchfolwvpt:T:e:"
64 
65 #define USE_SMALL_DATASET 1 // small depth dataset in ViSP-images
66 
67 #ifdef ENABLE_VISP_NAMESPACE
68 using namespace VISP_NAMESPACE_NAME;
69 #endif
70 
71 namespace
72 {
73 void usage(const char *name, const char *badparam)
74 {
75  fprintf(stdout, "\n\
76  Example of tracking with vpGenericTracker.\n\
77  \n\
78  SYNOPSIS\n\
79  %s [-i <test image path>] [-x <config file>] [-X <config file depth>]\n\
80  [-m <model name>] [-M <model name depth>] [-n <initialisation file base name>]\n\
81  [-f] [-c] [-d] [-h] [-o] [-w] [-l] [-v] [-p]\n\
82  [-t <tracker type>] [-T <tracker type>] [-e <last frame index>]\n",
83  name);
84 
85  fprintf(stdout, "\n\
86  OPTIONS: \n\
87  -i <input image path> \n\
88  Set image input path.\n\
89  These images come from visp-images-x.y.z.tar.gz available \n\
90  on the ViSP website.\n\
91  Setting the VISP_INPUT_IMAGE_PATH environment\n\
92  variable produces the same behavior than using\n\
93  this option.\n\
94  \n\
95  -x <config file> \n\
96  Set the config file (the xml file) to use.\n\
97  The config file is used to specify the parameters of the tracker.\n\
98  \n\
99  -X <config file> \n\
100  Set the config file (the xml file) to use for the depth sensor.\n\
101  The config file is used to specify the parameters of the tracker.\n\
102  \n\
103  -m <model name> \n\
104  Specify the name of the file of the model.\n\
105  The model can either be a vrml model (.wrl) or a .cao file.\n\
106  \n\
107  -M <model name> \n\
108  Specify the name of the file of the model for the depth sensor.\n\
109  The model can either be a vrml model (.wrl) or a .cao file.\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 optional picture specifying where to \n\
114  click (a .ppm picture).\n\
115  \n\
116  -f \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 automate the \n\
124  execution of this program without human intervention.\n\
125  \n\
126  -o\n\
127  Use Ogre3D for visibility tests\n\
128  \n\
129  -w\n\
130  When Ogre3D is enable [-o] show Ogre3D configuration dialog that allows to set the renderer.\n\
131  \n\
132  -l\n\
133  Use the scanline for visibility tests.\n\
134  \n\
135  -v\n\
136  Compute covariance matrix.\n\
137  \n\
138  -p\n\
139  Compute gradient projection error.\n\
140  \n\
141  -t <tracker type>\n\
142  Set tracker type (<1 (Edge)>, <2 (KLT)>, <3 (both)>) for color sensor.\n\
143  \n\
144  -T <tracker type>\n\
145  Set tracker type (<4 (Depth normal)>, <8 (Depth dense)>, <12 (both)>) for depth sensor.\n\
146  \n\
147  -e <last frame index>\n\
148  Specify the index of the last frame. Once reached, the tracking is stopped.\n\
149  \n\
150  -h \n\
151  Print the help.\n\n");
152 
153  if (badparam)
154  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
155 }
156 
157 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &configFile, std::string &configFile_depth,
158  std::string &modelFile, std::string &modelFile_depth, std::string &initFile, bool &displayFeatures,
159  bool &click_allowed, bool &display, bool &useOgre, bool &showOgreConfigDialog, bool &useScanline,
160  bool &computeCovariance, bool &projectionError, int &trackerType, int &tracker_type_depth,
161  int &lastFrame)
162 {
163  const char *optarg_;
164  int c;
165  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
166 
167  switch (c) {
168  case 'i':
169  ipath = optarg_;
170  break;
171  case 'x':
172  configFile = optarg_;
173  break;
174  case 'X':
175  configFile_depth = optarg_;
176  break;
177  case 'm':
178  modelFile = optarg_;
179  break;
180  case 'M':
181  modelFile_depth = optarg_;
182  break;
183  case 'n':
184  initFile = optarg_;
185  break;
186  case 'f':
187  displayFeatures = false;
188  break;
189  case 'c':
190  click_allowed = false;
191  break;
192  case 'd':
193  display = false;
194  break;
195  case 'o':
196  useOgre = true;
197  break;
198  case 'l':
199  useScanline = true;
200  break;
201  case 'w':
202  showOgreConfigDialog = true;
203  break;
204  case 'v':
205  computeCovariance = true;
206  break;
207  case 'p':
208  projectionError = true;
209  break;
210  case 't':
211  trackerType = atoi(optarg_);
212  break;
213  case 'T':
214  tracker_type_depth = atoi(optarg_);
215  break;
216  case 'e':
217  lastFrame = atoi(optarg_);
218  break;
219  case 'h':
220  usage(argv[0], nullptr);
221  return false;
222  break;
223 
224  default:
225  usage(argv[0], optarg_);
226  return false;
227  break;
228  }
229  }
230 
231  if ((c == 1) || (c == -1)) {
232  // standalone param or error
233  usage(argv[0], nullptr);
234  std::cerr << "ERROR: " << std::endl;
235  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
236  return false;
237  }
238 
239  return true;
240 }
241 
242 struct vpRealsenseIntrinsics_t
243 {
244  float ppx;
246  float ppy;
248  float fx;
250  float fy;
252  float coeffs[5];
253 };
254 
255 void rs_deproject_pixel_to_point(float point[3], const vpRealsenseIntrinsics_t &intrin, const float pixel[2], float depth)
256 {
257  float x = (pixel[0] - intrin.ppx) / intrin.fx;
258  float y = (pixel[1] - intrin.ppy) / intrin.fy;
259 
260  float r2 = x * x + y * y;
261  float f = 1 + intrin.coeffs[0] * r2 + intrin.coeffs[1] * r2 * r2 + intrin.coeffs[4] * r2 * r2 * r2;
262  float ux = x * f + 2 * intrin.coeffs[2] * x * y + intrin.coeffs[3] * (r2 + 2 * x * x);
263  float uy = y * f + 2 * intrin.coeffs[3] * x * y + intrin.coeffs[2] * (r2 + 2 * y * y);
264 
265  x = ux;
266  y = uy;
267 
268  point[0] = depth * x;
269  point[1] = depth * y;
270  point[2] = depth;
271 }
272 
273 bool read_data(unsigned int cpt, const std::string &input_directory, vpImage<unsigned char> &I,
274  vpImage<uint16_t> &I_depth_raw, std::vector<vpColVector> &pointcloud, unsigned int &pointcloud_width,
275  unsigned int &pointcloud_height)
276 {
277 #if defined(VISP_HAVE_DATASET)
278 #if VISP_HAVE_DATASET_VERSION >= 0x030600
279  std::string ext("png");
280 #else
281  std::string ext("pgm");
282 #endif
283 #else
284  // We suppose that the user will download a recent dataset
285  std::string ext("png");
286 #endif
287  // Read image
288  std::stringstream ss;
289  ss << input_directory << "/image_";
290  ss << std::setfill('0') << std::setw(4);
291  ss << cpt;
292  ss << ".";
293  ss << ext;
294  std::string filename_image = ss.str();
295  if (!vpIoTools::checkFilename(filename_image)) {
296  std::cerr << "Cannot read: " << filename_image << std::endl;
297  return false;
298  }
299  vpImageIo::read(I, filename_image);
300 
301  // Read raw depth
302  ss.str("");
303  ss << input_directory << "/depth_image_";
304  ss << std::setfill('0') << std::setw(4);
305  ss << cpt;
306  ss << ".bin";
307  std::string filename_depth = ss.str();
308 
309  std::ifstream file_depth(filename_depth.c_str(), std::ios::in | std::ios::binary);
310  if (!file_depth.is_open()) {
311  return false;
312  }
313 
314  unsigned int height = 0, width = 0;
315  vpIoTools::readBinaryValueLE(file_depth, height);
316  vpIoTools::readBinaryValueLE(file_depth, width);
317 
318  I_depth_raw.resize(height, width);
319 
320  uint16_t depth_value = 0;
321  for (unsigned int i = 0; i < height; i++) {
322  for (unsigned int j = 0; j < width; j++) {
323  vpIoTools::readBinaryValueLE(file_depth, depth_value);
324  I_depth_raw[i][j] = depth_value;
325  }
326  }
327 
328  // Transform pointcloud
329  pointcloud_width = width;
330  pointcloud_height = height;
331  pointcloud.resize((size_t)width * height);
332 
333  // Only for Creative SR300
334  const float depth_scale = 0.000124986647f;
335  vpRealsenseIntrinsics_t depth_intrinsic;
336  depth_intrinsic.ppx = 311.484558f;
337  depth_intrinsic.ppy = 246.283234f;
338  depth_intrinsic.fx = 476.053619f;
339  depth_intrinsic.fy = 476.053497f;
340  depth_intrinsic.coeffs[0] = 0.165056542f;
341  depth_intrinsic.coeffs[1] = -0.0508309528f;
342  depth_intrinsic.coeffs[2] = 0.00435937941f;
343  depth_intrinsic.coeffs[3] = 0.00541406544f;
344  depth_intrinsic.coeffs[4] = 0.250085592f;
345 
346  for (unsigned int i = 0; i < height; i++) {
347  for (unsigned int j = 0; j < width; j++) {
348  float scaled_depth = I_depth_raw[i][j] * depth_scale;
349  float point[3];
350  float pixel[2] = { (float)j, (float)i };
351  rs_deproject_pixel_to_point(point, depth_intrinsic, pixel, scaled_depth);
352 
353  vpColVector data_3D(3);
354  data_3D[0] = point[0];
355  data_3D[1] = point[1];
356  data_3D[2] = point[2];
357 
358  pointcloud[(size_t)(i * width + j)] = data_3D;
359  }
360  }
361 
362  return true;
363 }
364 
365 void loadConfiguration(vpMbTracker *const tracker,
366  const std::string &
367 #if defined(VISP_HAVE_PUGIXML)
368  configFile
369 #endif
370  ,
371  const std::string &
372 #if defined(VISP_HAVE_PUGIXML)
373  configFile_depth
374 #endif
375 )
376 {
377 #if defined(VISP_HAVE_PUGIXML)
378  // From the xml file
379  dynamic_cast<vpMbGenericTracker *>(tracker)->loadConfigFile(configFile, configFile_depth);
380 #else
381  // Edge
382  vpMe me;
383  me.setMaskSize(5);
384  me.setMaskNumber(180);
385  me.setRange(8);
387  me.setThreshold(10);
388  me.setMu1(0.5);
389  me.setMu2(0.5);
390  me.setSampleStep(4);
391  dynamic_cast<vpMbGenericTracker *>(tracker)->setMovingEdge(me);
392 
393  // Klt
394 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
395  vpKltOpencv klt;
396  klt.setMaxFeatures(10000);
397  klt.setWindowSize(5);
398  klt.setQuality(0.01);
399  klt.setMinDistance(5);
400  klt.setHarrisFreeParameter(0.02);
401  klt.setBlockSize(3);
402  klt.setPyramidLevels(3);
403 
404  dynamic_cast<vpMbGenericTracker *>(tracker)->setKltOpencv(klt);
405  dynamic_cast<vpMbGenericTracker *>(tracker)->setKltMaskBorder(5);
406 #endif
407 
408  // Depth
409  dynamic_cast<vpMbGenericTracker *>(tracker)->setDepthNormalFeatureEstimationMethod(
411  dynamic_cast<vpMbGenericTracker *>(tracker)->setDepthNormalPclPlaneEstimationMethod(2);
412  dynamic_cast<vpMbGenericTracker *>(tracker)->setDepthNormalPclPlaneEstimationRansacMaxIter(200);
413  dynamic_cast<vpMbGenericTracker *>(tracker)->setDepthNormalPclPlaneEstimationRansacThreshold(0.001);
414  dynamic_cast<vpMbGenericTracker *>(tracker)->setDepthNormalSamplingStep(2, 2);
415 
416  dynamic_cast<vpMbGenericTracker *>(tracker)->setDepthDenseSamplingStep(4, 4);
417 
418  vpCameraParameters cam1, cam2;
419  cam1.initPersProjWithoutDistortion(615.1674804688, 615.1675415039, 312.1889953613, 243.4373779297);
420  cam2.initPersProjWithoutDistortion(476.0536193848, 476.0534973145, 311.4845581055, 246.2832336426);
421 
422  dynamic_cast<vpMbGenericTracker *>(tracker)->setCameraParameters(cam1, cam2);
423 
424  tracker->setAngleAppear(vpMath::rad(70));
425  tracker->setAngleDisappear(vpMath::rad(80));
426 
427  // Specify the clipping to
428  tracker->setNearClippingDistance(0.01);
429  tracker->setFarClippingDistance(2.0);
430  tracker->setClipping(tracker->getClipping() | vpMbtPolygon::FOV_CLIPPING);
431  // tracker->setClipping(tracker->getClipping() | vpMbtPolygon::LEFT_CLIPPING
432  // | vpMbtPolygon::RIGHT_CLIPPING | vpMbtPolygon::UP_CLIPPING |
433  // vpMbtPolygon::DOWN_CLIPPING); // Equivalent to FOV_CLIPPING
434 #endif
435 }
436 } // namespace
437 
438 int main(int argc, const char **argv)
439 {
440  try {
441  std::string env_ipath;
442  std::string opt_ipath;
443  std::string ipath;
444  std::string opt_configFile;
445  std::string opt_configFile_depth;
446  std::string opt_modelFile;
447  std::string opt_modelFile_depth;
448  std::string opt_initFile;
449  std::string initFile;
450  bool displayFeatures = true;
451  bool opt_click_allowed = true;
452  bool opt_display = true;
453  bool useOgre = false;
454  bool showOgreConfigDialog = false;
455  bool useScanline = false;
456  bool computeCovariance = false;
457  bool projectionError = false;
458  int trackerType_image = vpMbGenericTracker::EDGE_TRACKER;
459  int trackerType_depth = vpMbGenericTracker::DEPTH_DENSE_TRACKER;
460 #if defined(__mips__) || defined(__mips) || defined(mips) || defined(__MIPS__)
461  // To avoid Debian test timeout
462  int opt_lastFrame = 5;
463 #else
464  int opt_lastFrame = -1;
465 #endif
466 
467  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
468  // environment variable value
469  env_ipath = vpIoTools::getViSPImagesDataPath();
470 
471  // Set the default input path
472  if (!env_ipath.empty())
473  ipath = env_ipath;
474 
475  // Read the command line options
476  if (!getOptions(argc, argv, opt_ipath, opt_configFile, opt_configFile_depth, opt_modelFile, opt_modelFile_depth,
477  opt_initFile, displayFeatures, opt_click_allowed, opt_display, useOgre, showOgreConfigDialog,
478  useScanline, computeCovariance, projectionError, trackerType_image, trackerType_depth,
479  opt_lastFrame)) {
480  return EXIT_FAILURE;
481  }
482 
483 #if !(defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO))
484  if (trackerType_image == 2 || trackerType_image == 3) { // Use vpMbGenericTracker::KLT_TRACKER
485  std::cout << "KLT features cannot be used: ViSP is not built with "
486  "KLT module or OpenCV imgproc and video modules are not available."
487  << std::endl;
488  return EXIT_SUCCESS;
489  }
490 #endif
491 
492  // Test if an input path is set
493  if (opt_ipath.empty() && env_ipath.empty()) {
494  usage(argv[0], nullptr);
495  std::cerr << std::endl << "ERROR:" << std::endl;
496  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
497  << " environment variable to specify the location of the " << std::endl
498  << " image path where test images are located." << std::endl
499  << std::endl;
500 
501  return EXIT_FAILURE;
502  }
503 
504  // Get the option values
505  ipath = vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/castel");
506 
507  std::string dir_path = vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth");
508  if (!vpIoTools::checkDirectory(dir_path)) {
509  std::cerr << "ViSP-images does not contain the folder: " << dir_path << "!" << std::endl;
510  return EXIT_SUCCESS;
511  }
512 
513  std::string configFile, configFile_depth;
514  if (!opt_configFile.empty())
515  configFile = opt_configFile;
516  else
517  configFile =
518  vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/chateau.xml");
519 
520  if (!opt_configFile_depth.empty())
521  configFile_depth = opt_configFile_depth;
522  else
523  configFile_depth =
524  vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/chateau_depth.xml");
525 
526  std::string modelFile, modelFile_depth;
527  if (!opt_modelFile.empty())
528  modelFile = opt_modelFile;
529  else {
530 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 2 || COIN_MAJOR_VERSION == 3 || COIN_MAJOR_VERSION == 4)
531  modelFile =
532  vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/chateau_gantry.wrl");
533 #else
534  modelFile = vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/chateau.cao");
535 #endif
536  }
537 
538  if (!opt_modelFile_depth.empty())
539  modelFile_depth = opt_modelFile_depth;
540  else
541  modelFile_depth =
542  vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/chateau.cao");
543 
544  std::string vrml_ext = ".wrl";
545  bool use_vrml =
546  (modelFile.compare(modelFile.length() - vrml_ext.length(), vrml_ext.length(), vrml_ext) == 0) ||
547  (modelFile_depth.compare(modelFile_depth.length() - vrml_ext.length(), vrml_ext.length(), vrml_ext) == 0);
548 
549  if (use_vrml) {
550 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 2 || COIN_MAJOR_VERSION == 3 || COIN_MAJOR_VERSION == 4)
551  std::cout << "use_vrml: " << use_vrml << std::endl;
552 #else
553  std::cerr << "Error: vrml model file is only supported if ViSP is "
554  "build with Coin3D 3rd party"
555  << std::endl;
556  return EXIT_FAILURE;
557 #endif
558  }
559 
560  if (!opt_initFile.empty())
561  initFile = opt_initFile;
562  else
563  initFile = vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/chateau.init");
564 
565  vpImage<unsigned char> I, I_depth;
566  vpImage<uint16_t> I_depth_raw;
567  std::vector<vpColVector> pointcloud;
568  unsigned int pointcloud_width, pointcloud_height;
569  if (!read_data(0, ipath, I, I_depth_raw, pointcloud, pointcloud_width, pointcloud_height)) {
570  std::cerr << "Cannot open sequence: " << ipath << std::endl;
571  return EXIT_FAILURE;
572  }
573 
574  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
575 
576  // initialise a display
577 #if defined(VISP_HAVE_X11)
578  vpDisplayX display1, display2;
579 #elif defined(VISP_HAVE_GDI)
580  vpDisplayGDI display1, display2;
581 #elif defined(HAVE_OPENCV_HIGHGUI)
582  vpDisplayOpenCV display1, display2;
583 #elif defined(VISP_HAVE_D3D9)
584  vpDisplayD3D display1, display2;
585 #elif defined(VISP_HAVE_GTK)
586  vpDisplayGTK display1, display2;
587 #else
588  opt_display = false;
589 #endif
590  if (opt_display) {
591 #if defined(VISP_HAVE_DISPLAY)
594  display1.init(I, 100, 100, "Test tracking (Left)");
595  display2.init(I_depth, (int)(I.getWidth() / vpDisplay::getDownScalingFactor(I)) + 110, 100,
596  "Test tracking (Right)");
597 #endif
599  vpDisplay::display(I_depth);
600  vpDisplay::flush(I);
601  vpDisplay::flush(I_depth);
602  }
603 
604  std::vector<int> trackerTypes(2);
605  trackerTypes[0] = trackerType_image;
606  trackerTypes[1] = trackerType_depth;
607  // Object pointer to check that inheritance is ok
608  vpMbTracker *tracker = new vpMbGenericTracker(trackerTypes);
609  vpHomogeneousMatrix c1Mo, c2Mo;
610  vpCameraParameters cam1, cam2;
611 
612  loadConfiguration(tracker, configFile, configFile_depth);
613 
614  vpHomogeneousMatrix depth_M_color;
615  std::string depth_M_color_filename =
616  vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/castel/depth_M_color.txt");
617  {
618  std::ifstream depth_M_color_file(depth_M_color_filename.c_str());
619  depth_M_color.load(depth_M_color_file);
620  std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformationMatrices;
621  mapOfCameraTransformationMatrices["Camera2"] = depth_M_color;
622  dynamic_cast<vpMbGenericTracker *>(tracker)->setCameraTransformationMatrix(mapOfCameraTransformationMatrices);
623  }
624 
625  // Display the moving edges, and the Klt points
626  tracker->setDisplayFeatures(displayFeatures);
627 
628  // Tells if the tracker has to use Ogre3D for visibility tests
629  tracker->setOgreVisibilityTest(useOgre);
630  if (useOgre)
631  tracker->setOgreShowConfigDialog(showOgreConfigDialog);
632 
633  // Tells if the tracker has to use the scanline visibility tests
634  tracker->setScanLineVisibilityTest(useScanline);
635 
636  // Tells if the tracker has to compute the covariance matrix
637  tracker->setCovarianceComputation(computeCovariance);
638 
639  // Tells if the tracker has to compute the projection error
640  tracker->setProjectionErrorComputation(projectionError);
641 
642  // Retrieve the camera parameters from the tracker
643  dynamic_cast<vpMbGenericTracker *>(tracker)->getCameraParameters(cam1, cam2);
644 
645  // Loop to position the cube
646  if (opt_display && opt_click_allowed) {
647  while (!vpDisplay::getClick(I, false)) {
649  vpDisplay::displayText(I, 15, 10, "click after positioning the object", vpColor::red);
650  vpDisplay::flush(I);
651  }
652  }
653 
654  // Load the 3D model (either a vrml file or a .cao file)
655  dynamic_cast<vpMbGenericTracker *>(tracker)->loadModel(modelFile, modelFile_depth);
656 
657  if (opt_display && opt_click_allowed) {
658  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
659  mapOfImages["Camera1"] = &I;
660  mapOfImages["Camera2"] = &I_depth;
661  std::map<std::string, std::string> mapOfInitFiles;
662  mapOfInitFiles["Camera1"] = initFile;
663 
664  // Initialise the tracker by clicking on the image
665  dynamic_cast<vpMbGenericTracker *>(tracker)->initClick(mapOfImages, mapOfInitFiles, true);
666  dynamic_cast<vpMbGenericTracker *>(tracker)->getPose(c1Mo, c2Mo);
667  // display the 3D model at the given pose
668  dynamic_cast<vpMbGenericTracker *>(tracker)->display(I, I_depth, c1Mo, c2Mo, cam1, cam2, vpColor::red);
669  }
670  else {
671  vpHomogeneousMatrix c1Moi(0.06846423368, 0.09062570884, 0.3401096693, -2.671882598, 0.1174275908, -0.6011935263);
672  vpHomogeneousMatrix c2Moi(0.04431452054, 0.09294637757, 0.3357760654, -2.677922443, 0.121297639, -0.6028463357);
673  dynamic_cast<vpMbGenericTracker *>(tracker)->initFromPose(I, I_depth, c1Moi, c2Moi);
674  }
675 
676  // track the model
677  {
678  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
679  mapOfImages["Camera1"] = &I;
680  std::map<std::string, const std::vector<vpColVector> *> mapOfPointclouds;
681  mapOfPointclouds["Camera2"] = &pointcloud;
682  std::map<std::string, unsigned int> mapOfWidths, mapOfHeights;
683  mapOfWidths["Camera2"] = pointcloud_width;
684  mapOfHeights["Camera2"] = pointcloud_height;
685 
686  dynamic_cast<vpMbGenericTracker *>(tracker)->track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
687  }
688  dynamic_cast<vpMbGenericTracker *>(tracker)->getPose(c1Mo, c2Mo);
689 
690  if (opt_display) {
691  vpDisplay::flush(I);
692  vpDisplay::flush(I_depth);
693  }
694 
695  bool quit = false, click = false;
696  unsigned int frame_index = 0;
697  std::vector<double> time_vec;
698  while (read_data(frame_index, ipath, I, I_depth_raw, pointcloud, pointcloud_width, pointcloud_height) && !quit &&
699  (opt_lastFrame > 0 ? (int)frame_index <= opt_lastFrame : true)) {
700  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
701 
702  if (opt_display) {
704  vpDisplay::display(I_depth);
705 
706  std::stringstream ss;
707  ss << "Num frame: " << frame_index;
708  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
709  }
710 
711  // Test reset the tracker
712  if (frame_index == 10) {
713  std::cout << "----------Test reset tracker----------" << std::endl;
714  if (opt_display) {
716  vpDisplay::display(I_depth);
717  }
718 
719  tracker->resetTracker();
720 
721  loadConfiguration(tracker, configFile, configFile_depth);
722  dynamic_cast<vpMbGenericTracker *>(tracker)->loadModel(modelFile, modelFile_depth);
723  dynamic_cast<vpMbGenericTracker *>(tracker)->setCameraParameters(cam1, cam2);
724  tracker->setOgreVisibilityTest(useOgre);
725  tracker->setScanLineVisibilityTest(useScanline);
726  tracker->setCovarianceComputation(computeCovariance);
727  tracker->setProjectionErrorComputation(projectionError);
728  dynamic_cast<vpMbGenericTracker *>(tracker)->initFromPose(I, I_depth, c1Mo, c2Mo);
729  }
730 
731  // Test to set an initial pose
732 #if USE_SMALL_DATASET
733  if (frame_index == 20) {
734  c1Mo.buildFrom(0.07734634051, 0.08993639906, 0.342344402, -2.708409543, 0.0669276477, -0.3798958303);
735  c2Mo.buildFrom(0.05319520317, 0.09223511976, 0.3380095812, -2.71438192, 0.07141055397, -0.3810081638);
736 #else
737  if (frame_index == 50) {
738  c1Mo.buildFrom(0.09280663035, 0.09277655672, 0.330415149, -2.724431817, 0.0293932671, 0.02027966377);
739  c2Mo.buildFrom(0.06865933578, 0.09494713501, 0.3260555142, -2.730027451, 0.03498390135, 0.01989831338);
740 #endif
741  std::cout << "Test set pose" << std::endl;
742  dynamic_cast<vpMbGenericTracker *>(tracker)->setPose(I, I_depth, c1Mo, c2Mo);
743  }
744 
745 #if USE_SMALL_DATASET
746  // track the object: stop tracking from frame 15 to 20
747  if (frame_index < 15 || frame_index >= 20) {
748 #else
749  // track the object: stop tracking from frame 30 to 50
750  if (frame_index < 30 || frame_index >= 50) {
751 #endif
752  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
753  mapOfImages["Camera1"] = &I;
754  std::map<std::string, const std::vector<vpColVector> *> mapOfPointclouds;
755  mapOfPointclouds["Camera2"] = &pointcloud;
756  std::map<std::string, unsigned int> mapOfWidths, mapOfHeights;
757  mapOfWidths["Camera2"] = pointcloud_width;
758  mapOfHeights["Camera2"] = pointcloud_height;
759 
760  double t = vpTime::measureTimeMs();
761  dynamic_cast<vpMbGenericTracker *>(tracker)->track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
762  t = vpTime::measureTimeMs() - t;
763  time_vec.push_back(t);
764 
765  dynamic_cast<vpMbGenericTracker *>(tracker)->getPose(c1Mo, c2Mo);
766 
767  if (opt_display) {
768  // display the 3D model
769  dynamic_cast<vpMbGenericTracker *>(tracker)->display(I, I_depth, c1Mo, c2Mo, cam1, cam2, vpColor::darkRed);
770  // display the frame
771  vpDisplay::displayFrame(I, c1Mo, cam1, 0.05);
772  vpDisplay::displayFrame(I_depth, c2Mo, cam2, 0.05);
773  // computation time
774  std::stringstream ss;
775  ss << "Computation time: " << t << " ms";
776  vpDisplay::displayText(I, 60, 20, ss.str(), vpColor::red);
777  // nb features
778  ss.str("");
779  ss << "nb features: " << tracker->getError().getRows();
780  vpDisplay::displayText(I_depth, 80, 20, ss.str(), vpColor::red);
781  {
782  std::stringstream ss;
783  ss << "Features: edges " << dynamic_cast<vpMbGenericTracker *>(tracker)->getNbFeaturesEdge() << ", klt "
784  << dynamic_cast<vpMbGenericTracker *>(tracker)->getNbFeaturesKlt() << ", depth "
785  << dynamic_cast<vpMbGenericTracker *>(tracker)->getNbFeaturesDepthDense();
786  vpDisplay::displayText(I, I.getHeight() - 30, 20, ss.str(), vpColor::red);
787  }
788  }
789  }
790 
791  if (opt_click_allowed && opt_display) {
792  vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
794  if (vpDisplay::getClick(I, button, click)) {
795  switch (button) {
797  quit = !click;
798  break;
799 
801  click = !click;
802  break;
803 
804  default:
805  break;
806  }
807  }
808  }
809 
810  if (computeCovariance) {
811  std::cout << "Covariance matrix: \n" << tracker->getCovarianceMatrix() << std::endl << std::endl;
812  }
813 
814  if (projectionError) {
815  std::cout << "Projection error: " << tracker->getProjectionError() << std::endl << std::endl;
816  }
817 
818  if (opt_display) {
819  vpDisplay::flush(I);
820  vpDisplay::flush(I_depth);
821  }
822 
823  frame_index++;
824  }
825 
826  std::cout << "\nFinal poses, c1Mo:\n" << c1Mo << "\nc2Mo:\n" << c2Mo << std::endl;
827  std::cout << "\nComputation time, Mean: " << vpMath::getMean(time_vec)
828  << " ms ; Median: " << vpMath::getMedian(time_vec) << " ms ; Std: " << vpMath::getStdev(time_vec) << " ms"
829  << std::endl;
830 
831  if (opt_click_allowed && !quit) {
833  }
834 
835  delete tracker;
836  tracker = nullptr;
837 
838  return EXIT_SUCCESS;
839  }
840  catch (const vpException &e) {
841  std::cout << "Catch an exception: " << e << std::endl;
842  return EXIT_FAILURE;
843  }
844 }
845 
846 #elif !(defined(VISP_HAVE_MODULE_MBT) && defined(VISP_HAVE_DISPLAY))
847 int main()
848 {
849  std::cout << "Cannot run this example: visp_mbt, visp_gui modules are required." << std::endl;
850  return EXIT_SUCCESS;
851 }
852 #else
853 int main()
854 {
855  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
856  return EXIT_SUCCESS;
857 }
858 #endif
unsigned int getRows() const
Definition: vpArray2D.h:427
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor red
Definition: vpColor.h:198
static const vpColor darkRed
Definition: vpColor.h:199
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...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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: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.
void load(std::ifstream &f)
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
unsigned int getWidth() const
Definition: vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:544
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1439
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:79
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:272
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:361
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:280
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:320
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:329
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:382
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:348
static double rad(double deg)
Definition: vpMath.h:129
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:322
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition: vpMath.cpp:353
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:302
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 vpColVector getError() const =0
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 setClipping(const unsigned int &flags)
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:472
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:258
@ ROBUST_FEATURE_ESTIMATION
Robust scheme to estimate the normal of the plane.
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
VISP_EXPORT double measureTimeMs()