Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-mb-generic-tracker-rgbd.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpDisplay.h>
5 #include <visp3/core/vpIoTools.h>
6 #include <visp3/io/vpImageIo.h>
7 #include <visp3/core/vpXmlParserCamera.h>
8 #include <visp3/gui/vpDisplayGDI.h>
9 #include <visp3/gui/vpDisplayX.h>
11 #include <visp3/mbt/vpMbGenericTracker.h>
13 
14 #if defined (VISP_HAVE_PCL)
15 #include <pcl/common/common.h>
16 #include <pcl/io/pcd_io.h>
17 
18 namespace {
19 struct rs_intrinsics {
20  float ppx;
22  float ppy;
24  float fx;
26  float fy;
28  float coeffs[5];
29 };
30 
31 void rs_deproject_pixel_to_point(float point[3], const rs_intrinsics &intrin, const float pixel[2], float depth) {
32  float x = (pixel[0] - intrin.ppx) / intrin.fx;
33  float y = (pixel[1] - intrin.ppy) / intrin.fy;
34 
35  float r2 = x * x + y * y;
36  float f = 1 + intrin.coeffs[0] * r2 + intrin.coeffs[1] * r2 * r2 + intrin.coeffs[4] * r2 * r2 * r2;
37  float ux = x * f + 2 * intrin.coeffs[2] * x * y + intrin.coeffs[3] * (r2 + 2 * x * x);
38  float uy = y * f + 2 * intrin.coeffs[3] * x * y + intrin.coeffs[2] * (r2 + 2 * y * y);
39 
40  x = ux;
41  y = uy;
42 
43  point[0] = depth * x;
44  point[1] = depth * y;
45  point[2] = depth;
46 }
47 
49 bool read_data(unsigned int cpt, const std::string &input_directory, vpImage<vpRGBa> &I_color,
50  vpImage<uint16_t> &I_depth_raw, pcl::PointCloud<pcl::PointXYZ>::Ptr &pointcloud)
52 {
53  char buffer[FILENAME_MAX];
54  // Read color
55  std::stringstream ss;
56  ss << input_directory << "/color_image_%04d.jpg";
57  sprintf(buffer, ss.str().c_str(), cpt);
58  std::string filename_color = buffer;
59 
60  if (!vpIoTools::checkFilename(filename_color)) {
61  std::cerr << "Cannot read: " << filename_color << std::endl;
62  return false;
63  }
64  vpImageIo::read(I_color, filename_color);
65 
66  // Read raw depth
67  ss.str("");
68  ss << input_directory << "/depth_image_%04d.bin";
69  sprintf(buffer, ss.str().c_str(), cpt);
70  std::string filename_depth = buffer;
71 
72  std::ifstream file_depth(filename_depth.c_str(), std::ios::in | std::ios::binary);
73  if (!file_depth.is_open()) {
74  return false;
75  }
76 
77  unsigned int height = 0, width = 0;
78  vpIoTools::readBinaryValueLE(file_depth, height);
79  vpIoTools::readBinaryValueLE(file_depth, width);
80 
81  I_depth_raw.resize(height, width);
82 
83  uint16_t depth_value = 0;
84  for (unsigned int i = 0; i < height; i++) {
85  for (unsigned int j = 0; j < width; j++) {
86  vpIoTools::readBinaryValueLE(file_depth, depth_value);
87  I_depth_raw[i][j] = depth_value;
88  }
89  }
90 
91  // Transform pointcloud
92  pointcloud->width = width;
93  pointcloud->height = height;
94  pointcloud->reserve((size_t)width * height);
95 
96  // Only for Creative SR300
97  const float depth_scale = 0.00100000005f;
98  rs_intrinsics depth_intrinsic;
99  depth_intrinsic.ppx = 320.503509521484f;
100  depth_intrinsic.ppy = 235.602951049805f;
101  depth_intrinsic.fx = 383.970001220703f;
102  depth_intrinsic.fy = 383.970001220703f;
103  depth_intrinsic.coeffs[0] = 0.0f;
104  depth_intrinsic.coeffs[1] = 0.0f;
105  depth_intrinsic.coeffs[2] = 0.0f;
106  depth_intrinsic.coeffs[3] = 0.0f;
107  depth_intrinsic.coeffs[4] = 0.0f;
108 
109  for (unsigned int i = 0; i < height; i++) {
110  for (unsigned int j = 0; j < width; j++) {
111  float scaled_depth = I_depth_raw[i][j] * depth_scale;
112  float point[3];
113  float pixel[2] = {(float)j, (float)i};
114  rs_deproject_pixel_to_point(point, depth_intrinsic, pixel, scaled_depth);
115  pointcloud->push_back(pcl::PointXYZ(point[0], point[1], point[2]));
116  }
117  }
118 
119  return true;
120 }
121 }
122 
123 int main(int argc, char *argv[])
124 {
125  std::string input_directory = "data"; // location of the data (images, depth_map, point_cloud)
126  std::string config_color = "model/cube/cube.xml", config_depth = "model/cube/cube_depth.xml";
127  std::string model_color = "model/cube/cube.cao", model_depth = "model/cube/cube.cao";
128  std::string init_file = "model/cube/cube.init";
129  int frame_cpt = 0;
130  bool disable_depth = false;
131 
132  for (int i = 1; i < argc; i++) {
133  if (std::string(argv[i]) == "--input_directory" && i+1 < argc) {
134  input_directory = std::string(argv[i+1]);
135  } else if (std::string(argv[i]) == "--config_color" && i+1 < argc) {
136  config_color = std::string(argv[i+1]);
137  } else if (std::string(argv[i]) == "--config_depth" && i+1 < argc) {
138  config_depth = std::string(argv[i+1]);
139  } else if (std::string(argv[i]) == "--model_color" && i+1 < argc) {
140  model_color = std::string(argv[i+1]);
141  } else if (std::string(argv[i]) == "--model_depth" && i+1 < argc) {
142  model_depth = std::string(argv[i+1]);
143  } else if (std::string(argv[i]) == "--init_file" && i+1 < argc) {
144  init_file = std::string(argv[i+1]);
145  } else if (std::string(argv[i]) == "--disable_depth") {
146  disable_depth = true;
147  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
148  std::cout << "Usage: \n" << argv[0] << " --input_directory <data directory> --config_color <object.xml> --config_depth <object.xml>"
149  " --model_color <object.cao> --model_depth <object.cao> --init_file <object.init> --disable_depth" << std::endl;
150  std::cout << "\nExample:\n" << argv[0] << " --config_color model/cube/cube.xml --config_depth model/cube/cube.xml"
151  " --model_color model/cube/cube.cao --model_depth model/cube/cube.cao --init_file model/cube/cube.init\n" << std::endl;
152  return 0;
153  }
154  }
155 
156  std::cout << "Config files: " << std::endl;
157  std::cout << " Input directory: " << "\"" << input_directory << "\"" << std::endl;
158  std::cout << " Config color: " << "\"" << config_color << "\"" << std::endl;
159  std::cout << " Config depth: " << "\"" << config_depth << "\"" << std::endl;
160  std::cout << " Model color : " << "\"" << model_color << "\"" << std::endl;
161  std::cout << " Model depth : " << "\"" << model_depth << "\"" << std::endl;
162  std::cout << " Init file : " << "\"" << init_file << "\"" << std::endl;
163 
164  vpImage<vpRGBa> I_color;
166  vpImage<unsigned char> I_gray, I_depth;
168  vpImage<uint16_t> I_depth_raw;
170  pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
172  vpCameraParameters cam_color, cam_depth;
173  unsigned int _posx = 100, _posy = 50, _posdx = 10;
174 
175  read_data(frame_cpt, input_directory, I_color, I_depth_raw, pointcloud);
176  vpImageConvert::convert(I_color, I_gray);
177  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
178 
179 #if defined(VISP_HAVE_X11)
180  vpDisplayX d1, d2;
181 #elif defined(VISP_HAVE_GDI)
182  vpDisplayGDI d1, d2;
183 #endif
184  d1.init(I_gray, _posx, _posy, "Color stream");
185  d2.init(I_depth, _posx + I_gray.getWidth()+_posdx, _posy, "Depth stream");
186  vpDisplay::display(I_gray);
187  vpDisplay::display(I_depth);
188  vpDisplay::flush(I_gray);
189  vpDisplay::flush(I_depth);
190 
192  std::vector<int> trackerTypes;
193 #ifdef VISP_HAVE_OPENCV
195  trackerTypes.push_back(vpMbGenericTracker::DEPTH_DENSE_TRACKER);
196 #else
197  trackerTypes.push_back(vpMbGenericTracker::EDGE_TRACKER);
198 #endif
199  vpMbGenericTracker tracker(trackerTypes);
202  tracker.loadConfigFile(config_color, config_depth);
205  tracker.loadModel(model_color, model_depth);
207 
208  tracker.getCameraParameters(cam_color, cam_depth);
209 
210  std::cout << "Camera parameters for color camera (from XML file): " << cam_color << std::endl;
211  std::cout << "Camera parameters for depth camera (from XML file): " << cam_depth << std::endl;
212 
214  tracker.setDisplayFeatures(true);
216 
218  vpHomogeneousMatrix depth_M_color;
219  {
220  std::ifstream file( (std::string(input_directory + "/depth_M_color.txt")).c_str() );
221  depth_M_color.load(file);
222  file.close();
223  }
224  std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformations;
225  mapOfCameraTransformations["Camera1"] = vpHomogeneousMatrix();
226  mapOfCameraTransformations["Camera2"] = depth_M_color;
227  tracker.setCameraTransformationMatrix(mapOfCameraTransformations);
229  std::cout << "depth_M_color: \n" << depth_M_color << std::endl;
230 
232  std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
233  mapOfImages["Camera1"] = &I_gray;
234  mapOfImages["Camera2"] = &I_depth;
236 
238  std::map<std::string, std::string> mapOfInitFiles;
239  mapOfInitFiles["Camera1"] = init_file;
240  tracker.initClick(mapOfImages, mapOfInitFiles, true);
242 
243  mapOfImages.clear();
244  pcl::PointCloud<pcl::PointXYZ>::Ptr empty_pointcloud(new pcl::PointCloud<pcl::PointXYZ>);
245  std::vector<double> times_vec;
246 
247  try {
248  bool quit = false;
249  while (! quit) {
250  double t = vpTime::measureTimeMs();
251  read_data(frame_cpt, input_directory, I_color, I_depth_raw, pointcloud);
252  vpImageConvert::convert(I_color, I_gray);
253  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
254 
255  vpDisplay::display(I_gray);
256  vpDisplay::display(I_depth);
257 
258  mapOfImages["Camera1"] = &I_gray;
259  std::map<std::string, pcl::PointCloud< pcl::PointXYZ >::ConstPtr> mapOfPointclouds;
260  if (disable_depth)
261  mapOfPointclouds["Camera2"] = empty_pointcloud;
262  else
263  mapOfPointclouds["Camera2"] = pointcloud;
264 
266  tracker.track(mapOfImages, mapOfPointclouds);
268 
270  vpHomogeneousMatrix cMo = tracker.getPose();
272 
273  std::cout << "iter: " << frame_cpt << " cMo:\n" << cMo << std::endl;
274 
276  tracker.display(I_gray, I_depth, cMo, depth_M_color*cMo, cam_color, cam_depth, vpColor::red, 3);
277  vpDisplay::displayFrame(I_gray, cMo, cam_color, 0.05, vpColor::none, 3);
278  vpDisplay::displayFrame(I_depth, depth_M_color*cMo, cam_depth, 0.05, vpColor::none, 3);
280 
281  t = vpTime::measureTimeMs() - t;
282  times_vec.push_back(t);
283 
284  std::stringstream ss;
285  ss << "Computation time: " << t << " ms";
286  vpDisplay::displayText(I_gray, 20, 20, ss.str(), vpColor::red);
287 
288  vpDisplay::flush(I_gray);
289  vpDisplay::flush(I_depth);
290 
292  if (vpDisplay::getClick(I_gray, button, false)) {
293  quit = true;
294  }
295 
296  frame_cpt ++;
297  }
298  } catch (const vpException &e) {
299  std::cout << "Catch exception: " << e.getStringMessage() << std::endl;
300  }
301 
302  std::cout << "\nProcessing time, Mean: " << vpMath::getMean(times_vec) << " ms ; Median: " << vpMath::getMedian(times_vec)
303  << " ; Std: " << vpMath::getStdev(times_vec) << " ms" << std::endl;
304 
305  return EXIT_SUCCESS;
306 }
307 #else
308 int main()
309 {
310  std::cout << "To run this tutorial, ViSP should be build with PCL library."
311  " Install libpcl, configure and build again ViSP..." << std::endl;
312  return EXIT_SUCCESS;
313 }
314 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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.
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
Real-time 6D object pose tracking using its CAD model.
static void flush(const vpImage< unsigned char > &I)
void load(std::ifstream &f)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:179
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:202
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
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))
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:730
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
unsigned int getWidth() const
Definition: vpImage.h:244
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92