Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
testGenericTrackerDepth.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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 depth 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 
48 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
49 #include <type_traits>
50 #endif
51 
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayOpenCV.h>
58 #include <visp3/gui/vpDisplayD3D.h>
59 #include <visp3/gui/vpDisplayGTK.h>
60 #include <visp3/mbt/vpMbGenericTracker.h>
61 
62 #define GETOPTARGS "i:dcle:mCh"
63 
64 namespace
65 {
66  void usage(const char *name, const char *badparam)
67  {
68  fprintf(stdout, "\n\
69  Regression test for vpGenericTracker and depth.\n\
70  \n\
71  SYNOPSIS\n\
72  %s [-i <test image path>] [-c] [-d] [-h] [-l] \n\
73  [-e <last frame index>] [-m] [-C]\n", name);
74 
75  fprintf(stdout, "\n\
76  OPTIONS: \n\
77  -i <input image path> \n\
78  Set image input path.\n\
79  These images come from ViSP-images-x.y.z.tar.gz available \n\
80  on the ViSP website.\n\
81  Setting the VISP_INPUT_IMAGE_PATH environment\n\
82  variable produces the same behavior than using\n\
83  this option.\n\
84  \n\
85  -d \n\
86  Turn off the display.\n\
87  \n\
88  -c\n\
89  Disable the mouse click. Useful to automate the \n\
90  execution of this program without human intervention.\n\
91  \n\
92  -l\n\
93  Use the scanline for visibility tests.\n\
94  \n\
95  -e <last frame index>\n\
96  Specify the index of the last frame. Once reached, the tracking is stopped.\n\
97  \n\
98  -m \n\
99  Set a tracking mask.\n\
100  \n\
101  -C \n\
102  Use color images.\n\
103  \n\
104  -h \n\
105  Print the help.\n\n");
106 
107  if (badparam)
108  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
109  }
110 
111  bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display,
112  bool &useScanline, int &lastFrame, bool &use_mask, bool &use_color_image)
113  {
114  const char *optarg_;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117 
118  switch (c) {
119  case 'i':
120  ipath = optarg_;
121  break;
122  case 'c':
123  click_allowed = false;
124  break;
125  case 'd':
126  display = false;
127  break;
128  case 'l':
129  useScanline = true;
130  break;
131  case 'e':
132  lastFrame = atoi(optarg_);
133  break;
134  case 'm':
135  use_mask = true;
136  break;
137  case 'C':
138  use_color_image = true;
139  break;
140  case 'h':
141  usage(argv[0], NULL);
142  return false;
143  break;
144 
145  default:
146  usage(argv[0], optarg_);
147  return false;
148  break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161  }
162 
163  template <typename Type>
164  bool read_data(const std::string &input_directory, int cpt, const vpCameraParameters &cam_depth,
165  vpImage<Type> &I, vpImage<uint16_t> &I_depth,
166  std::vector<vpColVector> &pointcloud, vpHomogeneousMatrix &cMo)
167  {
168 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
169  static_assert(std::is_same<Type, unsigned char>::value || std::is_same<Type, vpRGBa>::value,
170  "Template function supports only unsigned char and vpRGBa images!");
171 #endif
172  char buffer[256];
173  sprintf(buffer, std::string(input_directory + "/Images/Image_%04d.pgm").c_str(), cpt);
174  std::string image_filename = buffer;
175 
176  sprintf(buffer, std::string(input_directory + "/Depth/Depth_%04d.bin").c_str(), cpt);
177  std::string depth_filename = buffer;
178 
179  sprintf(buffer, std::string(input_directory + "/CameraPose/Camera_%03d.txt").c_str(), cpt);
180  std::string pose_filename = buffer;
181 
182  if (!vpIoTools::checkFilename(image_filename) || !vpIoTools::checkFilename(depth_filename)
183  || !vpIoTools::checkFilename(pose_filename))
184  return false;
185 
186  vpImageIo::read(I, image_filename);
187 
188  unsigned int depth_width = 0, depth_height = 0;
189  std::ifstream file_depth(depth_filename.c_str(), std::ios::in | std::ios::binary);
190  if (!file_depth.is_open())
191  return false;
192 
193  vpIoTools::readBinaryValueLE(file_depth, depth_height);
194  vpIoTools::readBinaryValueLE(file_depth, depth_width);
195  I_depth.resize(depth_height, depth_width);
196  pointcloud.resize(depth_height*depth_width);
197 
198  const float depth_scale = 0.000030518f;
199  for (unsigned int i = 0; i < I_depth.getHeight(); i++) {
200  for (unsigned int j = 0; j < I_depth.getWidth(); j++) {
201  vpIoTools::readBinaryValueLE(file_depth, I_depth[i][j]);
202  double x = 0.0, y = 0.0, Z = I_depth[i][j] * depth_scale;
203  vpPixelMeterConversion::convertPoint(cam_depth, j, i, x, y);
204  vpColVector pt3d(4, 1.0);
205  pt3d[0] = x*Z;
206  pt3d[1] = y*Z;
207  pt3d[2] = Z;
208  pointcloud[i*I_depth.getWidth()+j] = pt3d;
209  }
210  }
211 
212  std::ifstream file_pose(pose_filename.c_str());
213  if (!file_pose.is_open()) {
214  return false;
215  }
216 
217  for (unsigned int i = 0; i < 4; i++) {
218  for (unsigned int j = 0; j < 4; j++) {
219  file_pose >> cMo[i][j];
220  }
221  }
222 
223  return true;
224  }
225 
226  template <typename Type>
227  bool run(vpImage<Type> &I, const std::string &input_directory, bool opt_click_allowed,
228  bool opt_display, bool useScanline, int opt_lastFrame, bool use_mask) {
229 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
230  static_assert(std::is_same<Type, unsigned char>::value || std::is_same<Type, vpRGBa>::value,
231  "Template function supports only unsigned char and vpRGBa images!");
232 #endif
233  // Initialise a display
234 #if defined VISP_HAVE_X11
235  vpDisplayX display1, display2;
236 #elif defined VISP_HAVE_GDI
237  vpDisplayGDI display1, display2;
238 #elif defined VISP_HAVE_OPENCV
239  vpDisplayOpenCV display1, display2;
240 #elif defined VISP_HAVE_D3D9
241  vpDisplayD3D display1, display2;
242 #elif defined VISP_HAVE_GTK
243  vpDisplayGTK display1, display2;
244 #else
245  opt_display = false;
246 #endif
247 
248  std::vector<int> tracker_type;
249  tracker_type.push_back(vpMbGenericTracker::DEPTH_DENSE_TRACKER);
250  vpMbGenericTracker tracker(tracker_type);
251 #if defined(VISP_HAVE_PUGIXML)
252  tracker.loadConfigFile(input_directory + "/Config/chateau_depth.xml");
253 #else
254  {
255  vpCameraParameters cam_depth;
256  cam_depth.initPersProjWithoutDistortion(700.0, 700.0, 320.0, 240.0);
257  tracker.setCameraParameters(cam_depth);
258  }
259  // Depth
260  tracker.setDepthNormalFeatureEstimationMethod(vpMbtFaceDepthNormal::ROBUST_FEATURE_ESTIMATION);
261  tracker.setDepthNormalPclPlaneEstimationMethod(2);
262  tracker.setDepthNormalPclPlaneEstimationRansacMaxIter(200);
263  tracker.setDepthNormalPclPlaneEstimationRansacThreshold(0.001);
264  tracker.setDepthNormalSamplingStep(2, 2);
265 
266  tracker.setDepthDenseSamplingStep(4, 4);
267 
268 #if defined(VISP_HAVE_MODULE_KLT) && (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
269  tracker.setKltMaskBorder(5);
270 #endif
271 
272  tracker.setAngleAppear(vpMath::rad(85.0));
273  tracker.setAngleDisappear(vpMath::rad(89.0));
274  tracker.setNearClippingDistance(0.01);
275  tracker.setFarClippingDistance(2.0);
276  tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
277 #endif
278  tracker.loadModel(input_directory + "/Models/chateau.cao");
280  T[0][0] = -1;
281  T[0][3] = -0.2;
282  T[1][1] = 0;
283  T[1][2] = 1;
284  T[1][3] = 0.12;
285  T[2][1] = 1;
286  T[2][2] = 0;
287  T[2][3] = -0.15;
288  tracker.loadModel(input_directory + "/Models/cube.cao", false, T);
289  vpCameraParameters cam_depth;
290  tracker.getCameraParameters(cam_depth);
291  tracker.setDisplayFeatures(true);
292  tracker.setScanLineVisibilityTest(useScanline);
293 
294  vpImage<uint16_t> I_depth_raw;
295  vpImage<vpRGBa> I_depth;
296  vpHomogeneousMatrix cMo_truth;
297  std::vector<vpColVector> pointcloud;
298  int cpt_frame = 1;
299  if (!read_data(input_directory, cpt_frame, cam_depth, I, I_depth_raw, pointcloud, cMo_truth)) {
300  std::cerr << "Cannot read first frame!" << std::endl;
301  return EXIT_FAILURE;
302  }
303 
304  vpImage<bool> mask(I.getHeight(), I.getWidth());
305  const double roi_step = 7.0;
306  const double roi_step2 = 6.0;
307  if (use_mask) {
308  mask = false;
309  for (unsigned int i = (unsigned int) (I.getRows()/roi_step); i < (unsigned int) (I.getRows()*roi_step2/roi_step); i++) {
310  for (unsigned int j = (unsigned int) (I.getCols()/roi_step); j < (unsigned int) (I.getCols()*roi_step2/roi_step); j++) {
311  mask[i][j] = true;
312  }
313  }
314  tracker.setMask(mask);
315  }
316 
317  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
318  if (opt_display) {
319 #ifdef VISP_HAVE_DISPLAY
320  display1.init(I, 0, 0, "Image");
321  display2.init(I_depth, (int)I.getWidth(), 0, "Depth");
322 #endif
323  }
324 
325  vpHomogeneousMatrix depth_M_color;
326  depth_M_color[0][3] = -0.05;
327  tracker.initFromPose(I, depth_M_color*cMo_truth);
328 
329  bool click = false, quit = false;
330  std::vector<double> vec_err_t, vec_err_tu;
331  std::vector<double> time_vec;
332  while (read_data(input_directory, cpt_frame, cam_depth, I, I_depth_raw, pointcloud, cMo_truth) && !quit
333  && (opt_lastFrame > 0 ? (int)cpt_frame <= opt_lastFrame : true)) {
334  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
335 
336  if (opt_display) {
338  vpDisplay::display(I_depth);
339  }
340 
341  double t = vpTime::measureTimeMs();
342  std::map<std::string, const vpImage<Type> *> mapOfImages;
343  std::map<std::string, const std::vector<vpColVector> *> mapOfPointclouds;
344  mapOfPointclouds["Camera"] = &pointcloud;
345  std::map<std::string, unsigned int> mapOfWidths, mapOfHeights;
346  mapOfWidths["Camera"] = I_depth.getWidth();
347  mapOfHeights["Camera"] = I_depth.getHeight();
348 
349  tracker.track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
350  vpHomogeneousMatrix cMo = tracker.getPose();
351  t = vpTime::measureTimeMs() - t;
352  time_vec.push_back(t);
353 
354  if (opt_display) {
355  tracker.display(I_depth, cMo, cam_depth, vpColor::red, 3);
356  vpDisplay::displayFrame(I_depth, cMo, cam_depth, 0.05, vpColor::none, 3);
357 
358  std::stringstream ss;
359  ss << "Frame: " << cpt_frame;
360  vpDisplay::displayText(I_depth, 20, 20, ss.str(), vpColor::red);
361  ss.str("");
362  ss << "Nb features: " << tracker.getError().getRows();
363  vpDisplay::displayText(I_depth, 40, 20, ss.str(), vpColor::red);
364  }
365 
366  vpPoseVector pose_est(cMo);
367  vpPoseVector pose_truth(depth_M_color*cMo_truth);
368  vpColVector t_est(3), t_truth(3);
369  vpColVector tu_est(3), tu_truth(3);
370  for (unsigned int i = 0; i < 3; i++) {
371  t_est[i] = pose_est[i];
372  t_truth[i] = pose_truth[i];
373  tu_est[i] = pose_est[i+3];
374  tu_truth[i] = pose_truth[i+3];
375  }
376 
377  vpColVector t_err = t_truth-t_est, tu_err = tu_truth-tu_est;
378  double t_err2 = sqrt(t_err.sumSquare()), tu_err2 = vpMath::deg(sqrt(tu_err.sumSquare()));
379  vec_err_t.push_back( t_err2 );
380  vec_err_tu.push_back( tu_err2 );
381  const double t_thresh = useScanline ? 0.003 : 0.002;
382  const double tu_thresh = useScanline ? 0.5 : 0.4;
383  if ( !use_mask && (t_err2 > t_thresh || tu_err2 > tu_thresh) ) { //no accuracy test with mask
384  std::cerr << "Pose estimated exceeds the threshold (t_thresh = 0.003, tu_thresh = 0.5)!" << std::endl;
385  std::cout << "t_err: " << sqrt(t_err.sumSquare()) << " ; tu_err: " << vpMath::deg(sqrt(tu_err.sumSquare())) << std::endl;
386  //TODO: fix MBT to make tests deterministic
387 // return EXIT_FAILURE;
388  }
389 
390  if (opt_display) {
391  if (use_mask) {
392  vpRect roi(vpImagePoint(I.getRows()/roi_step, I.getCols()/roi_step),
393  vpImagePoint(I.getRows()*roi_step2/roi_step, I.getCols()*roi_step2/roi_step));
395  }
396 
397  vpDisplay::flush(I);
398  vpDisplay::flush(I_depth);
399  }
400 
401  if (opt_display && opt_click_allowed) {
403  if (vpDisplay::getClick(I, button, click)) {
404  switch (button) {
406  quit = !click;
407  break;
408 
410  click = !click;
411  break;
412 
413  default:
414  break;
415  }
416  }
417  }
418 
419  cpt_frame++;
420  }
421 
422  if (!time_vec.empty())
423  std::cout << "Computation time, Mean: " << vpMath::getMean(time_vec) << " ms ; Median: " << vpMath::getMedian(time_vec)
424  << " ms ; Std: " << vpMath::getStdev(time_vec) << " ms" << std::endl;
425 
426  if (!vec_err_t.empty())
427  std::cout << "Max translation error: " << *std::max_element(vec_err_t.begin(), vec_err_t.end()) << std::endl;
428 
429  if (!vec_err_tu.empty())
430  std::cout << "Max thetau error: " << *std::max_element(vec_err_tu.begin(), vec_err_tu.end()) << std::endl;
431 
432  return EXIT_SUCCESS;
433  }
434 }
435 
436 int main(int argc, const char *argv[])
437 {
438  try {
439  std::string env_ipath;
440  std::string opt_ipath = "";
441  bool opt_click_allowed = true;
442  bool opt_display = true;
443  bool useScanline = false;
444 #if defined(__mips__) || defined(__mips) || defined(mips) || defined(__MIPS__)
445  // To avoid Debian test timeout
446  int opt_lastFrame = 5;
447 #else
448  int opt_lastFrame = -1;
449 #endif
450  bool use_mask = false;
451  bool use_color_image = false;
452 
453  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
454  // environment variable value
455  env_ipath = vpIoTools::getViSPImagesDataPath();
456 
457  // Read the command line options
458  if (!getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display,
459  useScanline, opt_lastFrame, use_mask, use_color_image)) {
460  return EXIT_FAILURE;
461  }
462 
463  std::cout << "useScanline: " << useScanline << std::endl;
464  std::cout << "use_mask: " << use_mask << std::endl;
465  std::cout << "use_color_image: " << use_color_image << std::endl;
466 
467  // Test if an input path is set
468  if (opt_ipath.empty() && env_ipath.empty()) {
469  usage(argv[0], NULL);
470  std::cerr << std::endl << "ERROR:" << std::endl;
471  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
472  << " environment variable to specify the location of the " << std::endl
473  << " image path where test images are located." << std::endl
474  << std::endl;
475 
476  return EXIT_FAILURE;
477  }
478 
479  std::string input_directory = vpIoTools::createFilePath(!opt_ipath.empty() ? opt_ipath : env_ipath, "mbt-depth/Castle-simu");
480  if (!vpIoTools::checkDirectory(input_directory)) {
481  std::cerr << "ViSP-images does not contain the folder: " << input_directory << "!" << std::endl;
482  return EXIT_SUCCESS;
483  }
484 
485  if (use_color_image) {
486  vpImage<vpRGBa> I_color;
487  return run(I_color, input_directory, opt_click_allowed, opt_display, useScanline, opt_lastFrame, use_mask);
488  } else {
489  vpImage<unsigned char> I_gray;
490  return run(I_gray, input_directory, opt_click_allowed, opt_display, useScanline, opt_lastFrame, use_mask);
491  }
492  } catch (const vpException &e) {
493  std::cout << "Catch an exception: " << e << std::endl;
494  return EXIT_FAILURE;
495  }
496 }
497 #else
498 int main() {
499  std::cout << "Enable MBT module (VISP_HAVE_MODULE_MBT) to launch this test." << std::endl;
500  return 0;
501 }
502 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1292
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:879
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned int getRows() const
Definition: vpImage.h:216
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
Definition: vpIoTools.cpp:1860
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:222
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition: vpMath.cpp:252
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
static const vpColor none
Definition: vpColor.h:191
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Real-time 6D object pose tracking using its CAD model.
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:179
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:202
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:106
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1537
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getCols() const
Definition: vpImage.h:177
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
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 double rad(double deg)
Definition: vpMath.h:108
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static double deg(double rad)
Definition: vpMath.h:101
unsigned int getHeight() const
Definition: vpImage.h:186
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
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))
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
double sumSquare() const
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:730
Defines a rectangle in the plane.
Definition: vpRect.h:78
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static const vpColor yellow
Definition: vpColor.h:187
unsigned int getWidth() const
Definition: vpImage.h:244
Definition of the vpImage class member functions.
Definition: vpImage.h:124
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)