Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
testGenericTracker.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  * Regression test for MBT.
33  *
34 *****************************************************************************/
35 
42 #include <cstdlib>
43 #include <iostream>
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_HAVE_MODULE_MBT) && \
47  (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
48 
49 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
50 #include <type_traits>
51 #endif
52 
53 #include <visp3/core/vpFont.h>
54 #include <visp3/core/vpImageDraw.h>
55 #include <visp3/core/vpIoTools.h>
56 #include <visp3/gui/vpDisplayD3D.h>
57 #include <visp3/gui/vpDisplayGDI.h>
58 #include <visp3/gui/vpDisplayGTK.h>
59 #include <visp3/gui/vpDisplayOpenCV.h>
60 #include <visp3/gui/vpDisplayX.h>
61 #include <visp3/io/vpImageIo.h>
62 #include <visp3/io/vpParseArgv.h>
63 #include <visp3/mbt/vpMbGenericTracker.h>
64 
65 #define GETOPTARGS "i:dsclt:e:DmCh"
66 
67 namespace
68 {
69 void usage(const char *name, const char *badparam)
70 {
71  fprintf(stdout, "\n\
72  Regression test for vpGenericTracker.\n\
73  \n\
74  SYNOPSIS\n\
75  %s [-i <test image path>] [-c] [-d] [-s] [-h] [-l] \n\
76  [-t <tracker type>] [-e <last frame index>] [-D] [-m] [-C]\n",
77  name);
78 
79  fprintf(stdout, "\n\
80  OPTIONS: \n\
81  -i <input image path> \n\
82  Set image input path.\n\
83  These images come from ViSP-images-x.y.z.tar.gz available \n\
84  on the ViSP website.\n\
85  Setting the VISP_INPUT_IMAGE_PATH environment\n\
86  variable produces the same behavior than using\n\
87  this option.\n\
88  \n\
89  -d \n\
90  Turn off the display.\n\
91  \n\
92  -s \n\
93  If display is turn off, tracking results are saved in a video folder.\n\
94  \n\
95  -c\n\
96  Disable the mouse click. Useful to automate the \n\
97  execution of this program without human intervention.\n\
98  \n\
99  -t <tracker type>\n\
100  Set tracker type (<1 (Edge)>, <2 (KLT)>, <3 (both)>) for color sensor.\n\
101  \n\
102  -l\n\
103  Use the scanline for visibility tests.\n\
104  \n\
105  -e <last frame index>\n\
106  Specify the index of the last frame. Once reached, the tracking is stopped.\n\
107  \n\
108  -D \n\
109  Use depth.\n\
110  \n\
111  -m \n\
112  Set a tracking mask.\n\
113  \n\
114  -C \n\
115  Use color images.\n\
116  \n\
117  -h \n\
118  Print the help.\n\n");
119 
120  if (badparam)
121  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
122 }
123 
124 bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display, bool &save,
125  bool &useScanline, int &trackerType, int &lastFrame, bool &use_depth, bool &use_mask,
126  bool &use_color_image)
127 {
128  const char *optarg_;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
131 
132  switch (c) {
133  case 'i':
134  ipath = optarg_;
135  break;
136  case 'c':
137  click_allowed = false;
138  break;
139  case 'd':
140  display = false;
141  break;
142  case 's':
143  save = true;
144  break;
145  case 'l':
146  useScanline = true;
147  break;
148  case 't':
149  trackerType = atoi(optarg_);
150  break;
151  case 'e':
152  lastFrame = atoi(optarg_);
153  break;
154  case 'D':
155  use_depth = true;
156  break;
157  case 'm':
158  use_mask = true;
159  break;
160  case 'C':
161  use_color_image = true;
162  break;
163  case 'h':
164  usage(argv[0], nullptr);
165  return false;
166  break;
167 
168  default:
169  usage(argv[0], optarg_);
170  return false;
171  break;
172  }
173  }
174 
175  if ((c == 1) || (c == -1)) {
176  // standalone param or error
177  usage(argv[0], nullptr);
178  std::cerr << "ERROR: " << std::endl;
179  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
180  return false;
181  }
182 
183  return true;
184 }
185 
186 template <typename Type>
187 bool read_data(const std::string &input_directory, int cpt, const vpCameraParameters &cam_depth, vpImage<Type> &I,
188  vpImage<uint16_t> &I_depth, std::vector<vpColVector> &pointcloud, vpHomogeneousMatrix &cMo)
189 {
190 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
191  static_assert(std::is_same<Type, unsigned char>::value || std::is_same<Type, vpRGBa>::value,
192  "Template function supports only unsigned char and vpRGBa images!");
193 #endif
194 #if VISP_HAVE_DATASET_VERSION >= 0x030600
195  std::string ext("png");
196 #else
197  std::string ext("pgm");
198 #endif
199  char buffer[FILENAME_MAX];
200  snprintf(buffer, FILENAME_MAX, std::string(input_directory + "/Images/Image_%04d." + ext).c_str(), cpt);
201  std::string image_filename = buffer;
202 
203  snprintf(buffer, FILENAME_MAX, std::string(input_directory + "/Depth/Depth_%04d.bin").c_str(), cpt);
204  std::string depth_filename = buffer;
205 
206  snprintf(buffer, FILENAME_MAX, std::string(input_directory + "/CameraPose/Camera_%03d.txt").c_str(), cpt);
207  std::string pose_filename = buffer;
208 
209  if (!vpIoTools::checkFilename(image_filename) || !vpIoTools::checkFilename(depth_filename) ||
210  !vpIoTools::checkFilename(pose_filename))
211  return false;
212 
213  vpImageIo::read(I, image_filename);
214 
215  unsigned int depth_width = 0, depth_height = 0;
216  std::ifstream file_depth(depth_filename.c_str(), std::ios::in | std::ios::binary);
217  if (!file_depth.is_open())
218  return false;
219 
220  vpIoTools::readBinaryValueLE(file_depth, depth_height);
221  vpIoTools::readBinaryValueLE(file_depth, depth_width);
222  I_depth.resize(depth_height, depth_width);
223  pointcloud.resize(depth_height * depth_width);
224 
225  const float depth_scale = 0.000030518f;
226  for (unsigned int i = 0; i < I_depth.getHeight(); i++) {
227  for (unsigned int j = 0; j < I_depth.getWidth(); j++) {
228  vpIoTools::readBinaryValueLE(file_depth, I_depth[i][j]);
229  double x = 0.0, y = 0.0, Z = I_depth[i][j] * depth_scale;
230  vpPixelMeterConversion::convertPoint(cam_depth, j, i, x, y);
231  vpColVector pt3d(4, 1.0);
232  pt3d[0] = x * Z;
233  pt3d[1] = y * Z;
234  pt3d[2] = Z;
235  pointcloud[i * I_depth.getWidth() + j] = pt3d;
236  }
237  }
238 
239  std::ifstream file_pose(pose_filename.c_str());
240  if (!file_pose.is_open()) {
241  return false;
242  }
243 
244  for (unsigned int i = 0; i < 4; i++) {
245  for (unsigned int j = 0; j < 4; j++) {
246  file_pose >> cMo[i][j];
247  }
248  }
249 
250  return true;
251 }
252 
253 void convert(const vpImage<vpRGBa> &src, vpImage<vpRGBa> &dst) { dst = src; }
254 
255 void convert(const vpImage<unsigned char> &src, vpImage<vpRGBa> &dst) { vpImageConvert::convert(src, dst); }
256 
257 template <typename Type>
258 bool run(const std::string &input_directory, bool opt_click_allowed, bool opt_display, bool useScanline,
259  int trackerType_image, int opt_lastFrame, bool use_depth, bool use_mask, bool save)
260 {
261 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
262  static_assert(std::is_same<Type, unsigned char>::value || std::is_same<Type, vpRGBa>::value,
263  "Template function supports only unsigned char and vpRGBa images!");
264 #endif
265  // Initialise a display
266 #if defined(VISP_HAVE_X11)
267  vpDisplayX display1, display2;
268 #elif defined(VISP_HAVE_GDI)
269  vpDisplayGDI display1, display2;
270 #elif defined(HAVE_OPENCV_HIGHGUI)
271  vpDisplayOpenCV display1, display2;
272 #elif defined(VISP_HAVE_D3D9)
273  vpDisplayD3D display1, display2;
274 #elif defined(VISP_HAVE_GTK)
275  vpDisplayGTK display1, display2;
276 #else
277  opt_display = false;
278 #endif
279 
280  std::vector<int> tracker_type(2);
281  tracker_type[0] = trackerType_image;
282  tracker_type[1] = vpMbGenericTracker::DEPTH_DENSE_TRACKER;
283  vpMbGenericTracker tracker(tracker_type);
284 
285 #if defined(VISP_HAVE_PUGIXML)
286  std::string configFileCam1 = input_directory + std::string("/Config/chateau.xml");
287  std::string configFileCam2 = input_directory + std::string("/Config/chateau_depth.xml");
288  std::cout << "Load config file for camera 1: " << configFileCam1 << std::endl;
289  std::cout << "Load config file for camera 2: " << configFileCam2 << std::endl;
290  tracker.loadConfigFile(configFileCam1, configFileCam2);
291 #else
292  // Corresponding parameters manually set to have an example code
293  {
294  vpCameraParameters cam_color, cam_depth;
295  cam_color.initPersProjWithoutDistortion(700.0, 700.0, 320.0, 240.0);
296  cam_depth.initPersProjWithoutDistortion(700.0, 700.0, 320.0, 240.0);
297  tracker.setCameraParameters(cam_color, cam_depth);
298  }
299 
300  // Edge
301  vpMe me;
302  me.setMaskSize(5);
303  me.setMaskNumber(180);
304  me.setRange(8);
306  me.setThreshold(5);
307  me.setMu1(0.5);
308  me.setMu2(0.5);
309  me.setSampleStep(5);
310  tracker.setMovingEdge(me);
311 
312  // Klt
313 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
314  vpKltOpencv klt;
315  tracker.setKltMaskBorder(5);
316  klt.setMaxFeatures(10000);
317  klt.setWindowSize(5);
318  klt.setQuality(0.01);
319  klt.setMinDistance(5);
320  klt.setHarrisFreeParameter(0.02);
321  klt.setBlockSize(3);
322  klt.setPyramidLevels(3);
323 
324  tracker.setKltOpencv(klt);
325 #endif
326 
327  // Depth
328  tracker.setDepthNormalFeatureEstimationMethod(vpMbtFaceDepthNormal::ROBUST_FEATURE_ESTIMATION);
329  tracker.setDepthNormalPclPlaneEstimationMethod(2);
330  tracker.setDepthNormalPclPlaneEstimationRansacMaxIter(200);
331  tracker.setDepthNormalPclPlaneEstimationRansacThreshold(0.001);
332  tracker.setDepthNormalSamplingStep(2, 2);
333 
334  tracker.setDepthDenseSamplingStep(4, 4);
335 
336  tracker.setAngleAppear(vpMath::rad(85.0));
337  tracker.setAngleDisappear(vpMath::rad(89.0));
338  tracker.setNearClippingDistance(0.01);
339  tracker.setFarClippingDistance(2.0);
340  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
341 #endif
342 
343 #ifdef VISP_HAVE_COIN3D
344  tracker.loadModel(input_directory + "/Models/chateau.wrl", input_directory + "/Models/chateau.cao");
345 #else
346  tracker.loadModel(input_directory + "/Models/chateau.cao", input_directory + "/Models/chateau.cao");
347 #endif
349  T[0][0] = -1;
350  T[0][3] = -0.2;
351  T[1][1] = 0;
352  T[1][2] = 1;
353  T[1][3] = 0.12;
354  T[2][1] = 1;
355  T[2][2] = 0;
356  T[2][3] = -0.15;
357  tracker.loadModel(input_directory + "/Models/cube.cao", false, T);
358  vpCameraParameters cam_color, cam_depth;
359  tracker.getCameraParameters(cam_color, cam_depth);
360  tracker.setDisplayFeatures(true);
361  tracker.setScanLineVisibilityTest(useScanline);
362 
363  std::map<int, std::pair<double, double> > map_thresh;
364  // Take the highest thresholds between all CI machines
365 #ifdef VISP_HAVE_COIN3D
367  useScanline ? std::pair<double, double>(0.005, 6.) : std::pair<double, double>(0.007, 3.9);
368 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
369  map_thresh[vpMbGenericTracker::KLT_TRACKER] =
370  useScanline ? std::pair<double, double>(0.007, 1.9) : std::pair<double, double>(0.007, 1.8);
372  useScanline ? std::pair<double, double>(0.005, 3.7) : std::pair<double, double>(0.006, 3.4);
373 #endif
375  useScanline ? std::pair<double, double>(0.003, 1.7) : std::pair<double, double>(0.002, 0.8);
376 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
378  std::pair<double, double>(0.002, 0.3);
381  useScanline ? std::pair<double, double>(0.002, 1.8) : std::pair<double, double>(0.002, 0.7);
382 #endif
383 #else
385  useScanline ? std::pair<double, double>(0.015, 3.0) : std::pair<double, double>(0.009, 4.0);
386 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
387  map_thresh[vpMbGenericTracker::KLT_TRACKER] =
388  useScanline ? std::pair<double, double>(0.006, 1.7) : std::pair<double, double>(0.005, 1.4);
390  useScanline ? std::pair<double, double>(0.004, 1.2) : std::pair<double, double>(0.004, 1.2);
391 #endif
393  useScanline ? std::pair<double, double>(0.002, 0.7) : std::pair<double, double>(0.001, 0.4);
394 #if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
396  std::pair<double, double>(0.002, 0.3);
399  useScanline ? std::pair<double, double>(0.001, 0.5) : std::pair<double, double>(0.001, 0.4);
400 #endif
401 #endif
402 
403  vpImage<Type> I, I_depth;
404  vpImage<uint16_t> I_depth_raw;
405  vpHomogeneousMatrix cMo_truth;
406  std::vector<vpColVector> pointcloud;
407  int cpt_frame = 1;
408  if (!read_data(input_directory, cpt_frame, cam_depth, I, I_depth_raw, pointcloud, cMo_truth)) {
409  std::cerr << "Cannot read first frame!" << std::endl;
410  return EXIT_FAILURE;
411  }
412 
413  vpImage<bool> mask(I.getHeight(), I.getWidth());
414  const double roi_step = 7.0;
415  const double roi_step2 = 6.0;
416  if (use_mask) {
417  mask = false;
418  for (unsigned int i = (unsigned int)(I.getRows() / roi_step);
419  i < (unsigned int)(I.getRows() * roi_step2 / roi_step); i++) {
420  for (unsigned int j = (unsigned int)(I.getCols() / roi_step);
421  j < (unsigned int)(I.getCols() * roi_step2 / roi_step); j++) {
422  mask[i][j] = true;
423  }
424  }
425  tracker.setMask(mask);
426  }
427 
428  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
429 
430  vpImage<vpRGBa> results(I.getHeight(), I.getWidth() + I_depth.getWidth());
431  vpImage<vpRGBa> resultsColor(I.getHeight(), I.getWidth());
432  vpImage<vpRGBa> resultsDepth(I_depth.getHeight(), I_depth.getWidth());
433  if (save) {
434  vpIoTools::makeDirectory("results");
435  }
436  if (opt_display) {
437 #ifdef VISP_HAVE_DISPLAY
438  display1.init(I, 0, 0, "Image");
439  display2.init(I_depth, (int)I.getWidth(), 0, "Depth");
440 #endif
441  }
442 
443  vpHomogeneousMatrix depth_M_color;
444  depth_M_color[0][3] = -0.05;
445  tracker.setCameraTransformationMatrix("Camera2", depth_M_color);
446  tracker.initFromPose(I, cMo_truth);
447 
448  vpFont font(24);
449  bool click = false, quit = false, correct_accuracy = true;
450  std::vector<double> vec_err_t, vec_err_tu;
451  std::vector<double> time_vec;
452  while (read_data(input_directory, cpt_frame, cam_depth, I, I_depth_raw, pointcloud, cMo_truth) && !quit &&
453  (opt_lastFrame > 0 ? (int)cpt_frame <= opt_lastFrame : true)) {
454  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
455 
456  if (opt_display) {
458  vpDisplay::display(I_depth);
459  }
460  else if (save) {
461  convert(I, resultsColor);
462  convert(I_depth, resultsDepth);
463  }
464 
465  double t = vpTime::measureTimeMs();
466  std::map<std::string, const vpImage<Type> *> mapOfImages;
467  mapOfImages["Camera1"] = &I;
468  std::map<std::string, const std::vector<vpColVector> *> mapOfPointclouds;
469  mapOfPointclouds["Camera2"] = &pointcloud;
470  std::map<std::string, unsigned int> mapOfWidths, mapOfHeights;
471  if (!use_depth) {
472  mapOfWidths["Camera2"] = 0;
473  mapOfHeights["Camera2"] = 0;
474  }
475  else {
476  mapOfWidths["Camera2"] = I_depth.getWidth();
477  mapOfHeights["Camera2"] = I_depth.getHeight();
478  }
479 
480  tracker.track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
481  vpHomogeneousMatrix cMo = tracker.getPose();
482  t = vpTime::measureTimeMs() - t;
483  time_vec.push_back(t);
484 
485  if (opt_display) {
486  tracker.display(I, I_depth, cMo, depth_M_color * cMo, cam_color, cam_depth, vpColor::red, 3);
487  vpDisplay::displayFrame(I, cMo, cam_depth, 0.05, vpColor::none, 3);
488  vpDisplay::displayFrame(I_depth, depth_M_color * cMo, cam_depth, 0.05, vpColor::none, 3);
489 
490  std::stringstream ss;
491  ss << "Frame: " << cpt_frame;
492  vpDisplay::displayText(I_depth, 20, 20, ss.str(), vpColor::red);
493  ss.str("");
494  ss << "Nb features: " << tracker.getError().getRows();
495  vpDisplay::displayText(I_depth, 40, 20, ss.str(), vpColor::red);
496  }
497  else if (save) {
499  std::map<std::string, std::vector<std::vector<double> > > mapOfModels;
500  std::map<std::string, unsigned int> mapOfW;
501  mapOfW["Camera1"] = I.getWidth();
502  mapOfW["Camera2"] = I_depth.getWidth();
503  std::map<std::string, unsigned int> mapOfH;
504  mapOfH["Camera1"] = I.getHeight();
505  mapOfH["Camera2"] = I_depth.getHeight();
506  std::map<std::string, vpHomogeneousMatrix> mapOfcMos;
507  mapOfcMos["Camera1"] = cMo;
508  mapOfcMos["Camera2"] = depth_M_color * cMo;
509  std::map<std::string, vpCameraParameters> mapOfCams;
510  mapOfCams["Camera1"] = cam_color;
511  mapOfCams["Camera2"] = cam_depth;
512  tracker.getModelForDisplay(mapOfModels, mapOfW, mapOfH, mapOfcMos, mapOfCams);
513  for (std::map<std::string, std::vector<std::vector<double> > >::const_iterator it = mapOfModels.begin();
514  it != mapOfModels.end(); ++it) {
515  for (size_t i = 0; i < it->second.size(); i++) {
516  // test if it->second[i][0] = 0
517  if (std::fabs(it->second[i][0]) <= std::numeric_limits<double>::epsilon()) {
518  vpImageDraw::drawLine(it->first == "Camera1" ? resultsColor : resultsDepth,
519  vpImagePoint(it->second[i][1], it->second[i][2]),
520  vpImagePoint(it->second[i][3], it->second[i][4]), vpColor::red, 3);
521  }
522  }
523  }
525 
527  std::map<std::string, std::vector<std::vector<double> > > mapOfFeatures;
528  tracker.getFeaturesForDisplay(mapOfFeatures);
529  for (std::map<std::string, std::vector<std::vector<double> > >::const_iterator it = mapOfFeatures.begin();
530  it != mapOfFeatures.end(); ++it) {
531  for (size_t i = 0; i < it->second.size(); i++) {
532  if (std::fabs(it->second[i][0]) <=
533  std::numeric_limits<double>::epsilon()) { // test it->second[i][0] = 0 for ME
534  vpColor color = vpColor::yellow;
535  if (std::fabs(it->second[i][3]) <= std::numeric_limits<double>::epsilon()) { // test it->second[i][3] = 0
536  color = vpColor::green;
537  }
538  else if (std::fabs(it->second[i][3] - 1) <=
539  std::numeric_limits<double>::epsilon()) { // test it->second[i][3] = 1
540  color = vpColor::blue;
541  }
542  else if (std::fabs(it->second[i][3] - 2) <=
543  std::numeric_limits<double>::epsilon()) { // test it->second[i][3] = 2
544  color = vpColor::purple;
545  }
546  else if (std::fabs(it->second[i][3] - 3) <=
547  std::numeric_limits<double>::epsilon()) { // test it->second[i][3] = 3
548  color = vpColor::red;
549  }
550  else if (std::fabs(it->second[i][3] - 4) <=
551  std::numeric_limits<double>::epsilon()) { // test it->second[i][3] = 4
552  color = vpColor::cyan;
553  }
554  vpImageDraw::drawCross(it->first == "Camera1" ? resultsColor : resultsDepth,
555  vpImagePoint(it->second[i][1], it->second[i][2]), 3, color, 1);
556  }
557  else if (std::fabs(it->second[i][0] - 1) <=
558  std::numeric_limits<double>::epsilon()) { // test it->second[i][0] = 1 for KLT
559  vpImageDraw::drawCross(it->first == "Camera1" ? resultsColor : resultsDepth,
560  vpImagePoint(it->second[i][1], it->second[i][2]), 10, vpColor::red, 1);
561  }
562  }
563  }
565 
566  // Computation time
567  std::ostringstream oss;
568  oss << "Tracking time: " << t << " ms";
569  font.drawText(resultsColor, oss.str(), vpImagePoint(20, 20), vpColor::red);
570  }
571 
572  vpPoseVector pose_est(cMo);
573  vpPoseVector pose_truth(cMo_truth);
574  vpColVector t_est(3), t_truth(3);
575  vpColVector tu_est(3), tu_truth(3);
576  for (unsigned int i = 0; i < 3; i++) {
577  t_est[i] = pose_est[i];
578  t_truth[i] = pose_truth[i];
579  tu_est[i] = pose_est[i + 3];
580  tu_truth[i] = pose_truth[i + 3];
581  }
582 
583  vpColVector t_err = t_truth - t_est, tu_err = tu_truth - tu_est;
584  const double t_thresh =
585  map_thresh[!use_depth ? trackerType_image : trackerType_image | vpMbGenericTracker::DEPTH_DENSE_TRACKER].first;
586  const double tu_thresh =
587  map_thresh[!use_depth ? trackerType_image : trackerType_image | vpMbGenericTracker::DEPTH_DENSE_TRACKER].second;
588  double t_err2 = sqrt(t_err.sumSquare()), tu_err2 = vpMath::deg(sqrt(tu_err.sumSquare()));
589  vec_err_t.push_back(t_err2);
590  vec_err_tu.push_back(tu_err2);
591  if (!use_mask && (t_err2 > t_thresh || tu_err2 > tu_thresh)) { // no accuracy test with mask
592  std::cerr << "Pose estimated exceeds the threshold (t_thresh = " << t_thresh << " ; tu_thresh = " << tu_thresh
593  << ")!" << std::endl;
594  std::cout << "t_err: " << t_err2 << " ; tu_err: " << tu_err2 << std::endl;
595  correct_accuracy = false;
596  }
597 
598  if (opt_display) {
599  if (use_mask) {
600  vpRect roi(vpImagePoint(I.getRows() / roi_step, I.getCols() / roi_step),
601  vpImagePoint(I.getRows() * roi_step2 / roi_step, I.getCols() * roi_step2 / roi_step));
604  }
605 
606  vpDisplay::flush(I);
607  vpDisplay::flush(I_depth);
608  }
609  else if (save) {
611  char buffer[FILENAME_MAX];
612  std::ostringstream oss;
613  oss << "results/image_%04d.png";
614  snprintf(buffer, FILENAME_MAX, oss.str().c_str(), cpt_frame);
615 
616  results.insert(resultsColor, vpImagePoint());
617  results.insert(resultsDepth, vpImagePoint(0, resultsColor.getWidth()));
618 
619  vpImageIo::write(results, buffer);
621  }
622 
623  if (opt_display && opt_click_allowed) {
625  if (vpDisplay::getClick(I, button, click)) {
626  switch (button) {
628  quit = !click;
629  break;
630 
632  click = !click;
633  break;
634 
635  default:
636  break;
637  }
638  }
639  }
640 
641  cpt_frame++;
642  }
643 
644  if (!time_vec.empty())
645  std::cout << "Computation time, Mean: " << vpMath::getMean(time_vec)
646  << " ms ; Median: " << vpMath::getMedian(time_vec) << " ms ; Std: " << vpMath::getStdev(time_vec) << " ms"
647  << std::endl;
648 
649  if (!vec_err_t.empty())
650  std::cout << "Max translation error: " << *std::max_element(vec_err_t.begin(), vec_err_t.end()) << std::endl;
651 
652  if (!vec_err_tu.empty())
653  std::cout << "Max thetau error: " << *std::max_element(vec_err_tu.begin(), vec_err_tu.end()) << std::endl;
654 
655  std::cout << "Test result: " << (correct_accuracy ? "success" : "failure") << std::endl;
656  return correct_accuracy ? EXIT_SUCCESS : EXIT_FAILURE;
657 }
658 } // namespace
659 
660 int main(int argc, const char *argv[])
661 {
662  try {
663  std::string env_ipath;
664  std::string opt_ipath = "";
665  bool opt_click_allowed = true;
666  bool opt_display = true;
667  bool opt_save = false;
668  bool useScanline = false;
669  int trackerType_image = vpMbGenericTracker::EDGE_TRACKER;
670 #if defined(__mips__) || defined(__mips) || defined(mips) || defined(__MIPS__)
671  // To avoid Debian test timeout
672  int opt_lastFrame = 5;
673 #else
674  int opt_lastFrame = -1;
675 #endif
676  bool use_depth = false;
677  bool use_mask = false;
678  bool use_color_image = false;
679 
680  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
681  // environment variable value
682  env_ipath = vpIoTools::getViSPImagesDataPath();
683 
684  // Read the command line options
685  if (!getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display, opt_save, useScanline, trackerType_image,
686  opt_lastFrame, use_depth, use_mask, use_color_image)) {
687  return EXIT_FAILURE;
688  }
689 
690 #if ! (defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO))
691  if (trackerType_image == 2 || trackerType_image == 3) {
692  std::cout << "Using klt tracker is not possible without OpenCV imgproc and video modules." << std::endl;
693  std::cout << "Use rather command line option -t 1 to use edges." << std::endl;
694  return EXIT_SUCCESS;
695  }
696 #endif
697  std::cout << "trackerType_image: " << trackerType_image << std::endl;
698  std::cout << "useScanline: " << useScanline << std::endl;
699  std::cout << "use_depth: " << use_depth << std::endl;
700  std::cout << "use_mask: " << use_mask << std::endl;
701  std::cout << "use_color_image: " << use_color_image << std::endl;
702 #ifdef VISP_HAVE_COIN3D
703  std::cout << "COIN3D available." << std::endl;
704 #endif
705 
706 #if !defined(VISP_HAVE_MODULE_KLT) || (!defined(VISP_HAVE_OPENCV) || (VISP_HAVE_OPENCV_VERSION < 0x020100))
707  if (trackerType_image & 2) {
708  std::cout << "KLT features cannot be used: ViSP is not built with "
709  "KLT module or OpenCV is not available.\nTest is not run."
710  << std::endl;
711  return EXIT_SUCCESS;
712  }
713 #endif
714 
715  // Test if an input path is set
716  if (opt_ipath.empty() && env_ipath.empty()) {
717  usage(argv[0], nullptr);
718  std::cerr << std::endl << "ERROR:" << std::endl;
719  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
720  << " environment variable to specify the location of the " << std::endl
721  << " image path where test images are located." << std::endl
722  << std::endl;
723 
724  return EXIT_FAILURE;
725  }
726 
727  std::string input_directory =
728  vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/Castle-simu");
729  if (!vpIoTools::checkDirectory(input_directory)) {
730  std::cerr << "ViSP-images does not contain the folder: " << input_directory << "!" << std::endl;
731  return EXIT_SUCCESS;
732  }
733 
734  if (use_color_image) {
735  return run<vpRGBa>(input_directory, opt_click_allowed, opt_display, useScanline, trackerType_image, opt_lastFrame,
736  use_depth, use_mask, opt_save);
737  }
738  else {
739  return run<unsigned char>(input_directory, opt_click_allowed, opt_display, useScanline, trackerType_image,
740  opt_lastFrame, use_depth, use_mask, opt_save);
741  }
742 
743  std::cout << "Test succeed" << std::endl;
744  return EXIT_SUCCESS;
745  }
746  catch (const vpException &e) {
747  std::cout << "Catch an exception: " << e << std::endl;
748  return EXIT_FAILURE;
749  }
750 }
751 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
752 int main()
753 {
754  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
755  return EXIT_SUCCESS;
756 }
757 #else
758 int main()
759 {
760  std::cout << "Enable MBT module (VISP_HAVE_MODULE_MBT) to launch this test." << std::endl;
761  return EXIT_SUCCESS;
762 }
763 #endif
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:163
double sumSquare() const
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor cyan
Definition: vpColor.h:220
static const vpColor none
Definition: vpColor.h:223
static const vpColor blue
Definition: vpColor.h:217
static const vpColor purple
Definition: vpColor.h:222
static const vpColor yellow
Definition: vpColor.h:219
static const vpColor green
Definition: vpColor.h:214
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)
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)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
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
Font drawing functions for image.
Definition: vpFont.h:54
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void drawLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
static void drawCross(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, unsigned char color, unsigned int thickness=1)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Definition of the vpImage class member functions.
Definition: vpImage.h:69
unsigned int getWidth() const
Definition: vpImage.h:245
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:783
unsigned int getCols() const
Definition: vpImage.h:175
unsigned int getHeight() const
Definition: vpImage.h:184
unsigned int getRows() const
Definition: vpImage.h:215
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1832
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:1213
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
Definition: vpIoTools.cpp:2520
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:832
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2195
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:981
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
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:323
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition: vpMath.cpp:354
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:303
static double deg(double rad)
Definition: vpMath.h:117
Real-time 6D object pose tracking using its CAD model.
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
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
Defines a rectangle in the plane.
Definition: vpRect.h:76
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT double measureTimeMs()