Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
readRealSenseData.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 *****************************************************************************/
32 
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_THREADS) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
44 #include <condition_variable>
45 #include <fstream>
46 #include <mutex>
47 #include <queue>
48 #include <thread>
49 
50 #include <visp3/core/vpImageConvert.h>
51 #include <visp3/core/vpIoException.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/gui/vpDisplayGDI.h>
54 #include <visp3/gui/vpDisplayX.h>
55 #include <visp3/gui/vpDisplayPCL.h>
56 #include <visp3/io/vpImageIo.h>
57 #include <visp3/io/vpParseArgv.h>
58 #include <visp3/io/vpVideoWriter.h>
59 
60 #if defined(VISP_HAVE_PCL)
61 #include <pcl/pcl_config.h>
62 #if defined(VISP_HAVE_PCL_COMMON)
63 #include <pcl/point_types.h>
64 #include <pcl/point_cloud.h>
65 #endif
66 #if defined(VISP_HAVE_PCL_IO)
67 #include <pcl/io/pcd_io.h>
68 #endif
69 #endif
70 
71 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
72 #define GETOPTARGS "ci:e:jbzodh"
73 #else
74 #define GETOPTARGS "ci:e:jbodh"
75 #endif
76 
77 #ifdef ENABLE_VISP_NAMESPACE
78 using namespace VISP_NAMESPACE_NAME;
79 #endif
80 
81 namespace
82 {
83 
84 void usage(const char *name, const char *badparam)
85 {
86  std::cout << "\nNAME " << std::endl
87  << " " << vpIoTools::getName(name)
88  << " - Read data acquired with a Realsense device." << std::endl
89  << std::endl
90  << "SYNOPSIS " << std::endl
91  << " " << name
92  << " [-i <directory>]"
93  << " [-e <filename pattern (e.g. %04d)>]"
94  << " [-c]"
95  << " [-j]"
96  << " [-b]"
97 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
98  << " [-z]"
99 #endif
100  << " [-o]"
101  << " [-d]"
102  << " [--help,-h]"
103  << std::endl;
104  std::cout << "\nOPTIONS " << std::endl
105  << " -i <directory>" << std::endl
106  << " Input folder that contains the data to read." << std::endl
107  << std::endl
108  << " -e <pattern>" << std::endl
109  << " Filename pattern, e.g. %04d." << std::endl
110  << std::endl
111  << " -c" << std::endl
112  << " Flag to display data in step by step mode triggered by a user click." << std::endl
113  << std::endl
114  << " -j" << std::endl
115  << " Image data are saved in JPEG format, otherwise in PNG." << std::endl
116  << std::endl
117  << " -b" << std::endl
118  << " Depth and Pointcloud streams are saved in binary format." << std::endl
119  << std::endl
120 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
121  << " -z" << std::endl
122  << " Pointcloud stream is saved in NPZ format." << std::endl
123  << std::endl
124 #endif
125  << " -o" << std::endl
126  << " Save color images in PNG format (lossless) in a new folder." << std::endl
127  << std::endl
128  << " -d" << std::endl
129  << " Display depth in color." << std::endl
130  << std::endl
131  << " --help, -h" << std::endl
132  << " Display this helper message." << std::endl
133  << std::endl;
134 
135  if (badparam) {
136  std::cout << "\nERROR: Bad parameter " << badparam << std::endl;
137  }
138 }
139 
140 bool getOptions(int argc, const char *argv[], std::string &input_directory, std::string &pattern, bool &click,
141  bool &force_binary_format, bool &read_jpeg, bool &read_npz, bool &save_video, bool &color_depth)
142 {
143  const char *optarg;
144  const char **argv1 = (const char **)argv;
145  int c;
146  while ((c = vpParseArgv::parse(argc, argv1, GETOPTARGS, &optarg)) > 1) {
147 
148  switch (c) {
149  case 'i':
150  input_directory = optarg;
151  break;
152  case 'e':
153  pattern = optarg;
154  break;
155  case 'c':
156  click = true;
157  break;
158  case 'j':
159  read_jpeg = true;
160  break;
161  case 'b':
162  force_binary_format = true;
163  break;
164 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
165  case 'z':
166  read_npz = true;
167  break;
168 #else
169  (void)read_npz;
170 #endif
171  case 'o':
172  save_video = true;
173  break;
174  case 'd':
175  color_depth = true;
176  break;
177 
178  case 'h':
179  usage(argv[0], nullptr);
180  return false;
181  break;
182 
183  default:
184  usage(argv[0], optarg);
185  return false;
186  break;
187  }
188  }
189 
190  if ((c == 1) || (c == -1)) {
191  // standalone param or error
192  usage(argv[0], nullptr);
193  std::cerr << "ERROR: " << std::endl;
194  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
195  return false;
196  }
197 
198  return true;
199 }
200 
201 bool readData(int cpt, const std::string &input_directory, const std::string &pattern, vpImage<vpRGBa> &I_color,
202  vpImage<uint16_t> &I_depth_raw, bool force_binary_format, bool read_jpeg, bool read_npz
203 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
204  , pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud
205 #endif
206 )
207 {
208  std::string image_filename_ext = read_jpeg ? ".jpg" : ".png";
209  std::string depth_filename_ext = force_binary_format ? ".bin" : ".npz";
210  char buffer[FILENAME_MAX];
211  std::stringstream ss;
212  ss << input_directory << "/color_image_" << pattern << image_filename_ext;
213  snprintf(buffer, FILENAME_MAX, ss.str().c_str(), cpt);
214  std::string filename_color = buffer;
215 
216  ss.str("");
217  ss << input_directory << "/depth_image_" << pattern << depth_filename_ext;
218  snprintf(buffer, FILENAME_MAX, ss.str().c_str(), cpt);
219  std::string filename_depth = buffer;
220 
221  ss.str("");
222  ss << input_directory << "/point_cloud_" << pattern << (force_binary_format ? ".bin" :
223  (read_npz ? ".npz" : ".pcd"));
224  snprintf(buffer, FILENAME_MAX, ss.str().c_str(), cpt);
225  std::string filename_pointcloud = buffer;
226 
227  if (!vpIoTools::checkFilename(filename_color) && !vpIoTools::checkFilename(filename_depth) &&
228  !vpIoTools::checkFilename(filename_pointcloud)) {
229  std::cerr << "End of sequence." << std::endl;
230  return false;
231  }
232 
233  // Read color
234  if (vpIoTools::checkFilename(filename_color)) {
235  vpImageIo::read(I_color, filename_color);
236  }
237 
238  // Read raw depth
239  if (vpIoTools::checkFilename(filename_depth)) {
240  if (force_binary_format) {
241  std::ifstream file_depth(filename_depth.c_str(), std::ios::in | std::ios::binary);
242  if (file_depth.is_open()) {
243  unsigned int height = 0, width = 0;
244  vpIoTools::readBinaryValueLE(file_depth, height);
245  vpIoTools::readBinaryValueLE(file_depth, width);
246  I_depth_raw.resize(height, width);
247 
248  uint16_t depth_value = 0;
249  for (unsigned int i = 0; i < height; i++) {
250  for (unsigned int j = 0; j < width; j++) {
251  vpIoTools::readBinaryValueLE(file_depth, depth_value);
252  I_depth_raw[i][j] = depth_value;
253  }
254  }
255  }
256  }
257 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
258  else {
259  visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(filename_depth);
260 
261  // Load depth data
262  visp::cnpy::NpyArray arr_depth_data = npz_data["data"];
263  if (arr_depth_data.data_holder == nullptr) {
264  throw vpIoException(vpIoException::ioError, "Loaded NPZ data is null.");
265  }
266 
267  uint16_t *depth_data_ptr = arr_depth_data.data<uint16_t>();
268  assert(arr_depth_data.shape.size() == 3); // H x W x C
269  assert(arr_depth_data.shape[2] == 1); // Single channel
270 
271  unsigned int height = static_cast<unsigned int>(arr_depth_data.shape[0]);
272  unsigned int width = static_cast<unsigned int>(arr_depth_data.shape[1]);
273  const bool copyData = true;
274  I_depth_raw = vpImage<uint16_t>(depth_data_ptr, height, width, copyData);
275  }
276 #else
277  else {
278  throw(vpIoException(vpIoException::ioError, "Cannot open non-binary depth file when npz I/O functions are disabled."));
279  }
280 #endif
281  }
282 
283  // Read pointcloud
284  if (vpIoTools::checkFilename(filename_pointcloud)) {
285 #if defined(VISP_HAVE_PCL)
286  // TODO: not tested
287  if (force_binary_format) {
288  std::ifstream file_pointcloud(filename_pointcloud.c_str(), std::ios::in | std::ios::binary);
289  if (!file_pointcloud.is_open()) {
290  std::cerr << "Cannot read pointcloud file: " << filename_pointcloud << std::endl;
291  }
292 
293  uint32_t height = 0, width = 0;
294  const char is_dense = 1;
295  vpIoTools::readBinaryValueLE(file_pointcloud, height);
296  vpIoTools::readBinaryValueLE(file_pointcloud, width);
297  file_pointcloud.read((char *)(&is_dense), sizeof(is_dense));
298 
299  point_cloud->width = width;
300  point_cloud->height = height;
301  point_cloud->is_dense = (is_dense != 0);
302  point_cloud->resize((size_t)width * height);
303 
304  float x = 0.0f, y = 0.0f, z = 0.0f;
305  for (uint32_t i = 0; i < height; i++) {
306  for (uint32_t j = 0; j < width; j++) {
307  vpIoTools::readBinaryValueLE(file_pointcloud, x);
308  vpIoTools::readBinaryValueLE(file_pointcloud, y);
309  vpIoTools::readBinaryValueLE(file_pointcloud, z);
310 
311  point_cloud->points[(size_t)(i * width + j)].x = x;
312  point_cloud->points[(size_t)(i * width + j)].y = y;
313  point_cloud->points[(size_t)(i * width + j)].z = z;
314  }
315  }
316  }
317 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
318  else if (read_npz) {
319  visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(filename_pointcloud);
320 
321  // Load pointcloud data
322  visp::cnpy::NpyArray arr_pcl_data = npz_data["data"];
323  if (arr_pcl_data.data_holder == nullptr) {
324  throw vpIoException(vpIoException::ioError, "Loaded NPZ data is null.");
325  }
326 
327  float *pcl_data_ptr = arr_pcl_data.data<float>();
328  assert(arr_pcl_data.shape.size() == 3); // H x W x C
329  assert(arr_pcl_data.shape[2] == 3); // 3-channels: X, Y, Z
330 
331  uint32_t height = arr_pcl_data.shape[0], width = arr_pcl_data.shape[1];
332  const char is_dense = 1;
333 
334  point_cloud->width = width;
335  point_cloud->height = height;
336  point_cloud->is_dense = (is_dense != 0);
337  point_cloud->resize((size_t)width * height);
338 
339  for (uint32_t i = 0; i < height; i++) {
340  for (uint32_t j = 0; j < width; j++) {
341  point_cloud->points[(size_t)(i * width + j)].x = pcl_data_ptr[(size_t)(i * width + j)*3 + 0];
342  point_cloud->points[(size_t)(i * width + j)].y = pcl_data_ptr[(size_t)(i * width + j)*3 + 1];
343  point_cloud->points[(size_t)(i * width + j)].z = pcl_data_ptr[(size_t)(i * width + j)*3 + 2];
344  }
345  }
346  }
347 #endif
348  else {
349 #if defined(VISP_HAVE_PCL_IO)
350  if (pcl::io::loadPCDFile<pcl::PointXYZ>(filename_pointcloud, *point_cloud) == -1) {
351  std::cerr << "Cannot read PCD: " << filename_pointcloud << std::endl;
352  }
353 #else
354  throw(vpIoException(vpIoException::ioError, "Cannot read pcd file without PCL io module"));
355 #endif
356  }
357 #endif
358  }
359 
360  return true;
361 }
362 } // Namespace
363 
364 int main(int argc, const char *argv[])
365 {
366  std::string input_directory = "";
367  std::string pattern = "%04d";
368  bool click = false;
369  bool force_binary_format = false;
370  bool save_video = false;
371  bool color_depth = false;
372  bool read_jpeg = false;
373  bool read_npz = false;
374 
375  // Read the command line options
376  if (!getOptions(argc, argv, input_directory, pattern, click, force_binary_format, read_jpeg, read_npz,
377  save_video, color_depth)) {
378  return EXIT_FAILURE;
379  }
380 
381  vpImage<vpRGBa> I_color(480, 640), I_depth_color(480, 640);
382  vpImage<uint16_t> I_depth_raw(480, 640);
383  vpImage<unsigned char> I_depth(480, 640);
384 
385 #ifdef VISP_HAVE_X11
386  vpDisplayX d1, d2;
387 #else
388  vpDisplayGDI d1, d2;
389 #endif
390  bool init_display = false;
391 
392 #if defined(VISP_HAVE_PCL)
393  std::mutex mutex;
394  pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
395 #if defined(VISP_HAVE_PCL_VISUALIZATION)
396  vpDisplayPCL pcl_viewer;
397 #endif
398 #endif
399 
400  vpVideoWriter writer;
401  vpImage<vpRGBa> O;
402  if (save_video) {
403  std::string output_directory = vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
404  vpIoTools::makeDirectory(output_directory);
405  writer.setFileName(output_directory + "/" + pattern + ".png");
406  }
407 
408  int cpt_frame = 0;
409  bool quit = false;
410  while (!quit) {
411  double t = vpTime::measureTimeMs();
412 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON)
413  {
414  std::lock_guard<std::mutex> lock(mutex);
415  quit = !readData(cpt_frame, input_directory, pattern, I_color, I_depth_raw, force_binary_format, read_jpeg,
416  read_npz, pointcloud);
417  }
418 #else
419  quit = !readData(cpt_frame, input_directory, pattern, I_color, I_depth_raw, force_binary_format, read_jpeg,
420  read_npz);
421 #endif
422 
423  if (color_depth)
424  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth_color);
425  else
426  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
427 
428  if (!init_display) {
429  init_display = true;
430  d1.init(I_color, 0, 0, "Color image");
431  if (color_depth) {
432  d2.init(I_depth_color, I_color.getWidth() + 10, 0, "Depth image");
433  }
434  else {
435  d2.init(I_depth, I_color.getWidth() + 10, 0, "Depth image");
436  }
437 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_VISUALIZATION)
438  if (pointcloud->size() > 0) {
439  pcl_viewer.setPosition(I_color.getWidth() + 10, I_color.getHeight() + 70);
440  pcl_viewer.setWindowName("3D point cloud");
441  pcl_viewer.startThread(std::ref(mutex), pointcloud);
442  }
443 #endif
444  }
445 
446  vpDisplay::display(I_color);
447  if (color_depth)
448  vpDisplay::display(I_depth_color);
449  else
450  vpDisplay::display(I_depth);
451 
452  std::stringstream ss;
453  ss << "Frame: " << cpt_frame;
454  vpDisplay::displayText(I_color, 20, 20, ss.str(), vpColor::red);
455  if (color_depth)
456  vpDisplay::displayText(I_depth_color, 20, 20, ss.str(), vpColor::red);
457  else
458  vpDisplay::displayText(I_depth, 20, 20, ss.str(), vpColor::red);
459 
460  vpDisplay::flush(I_color);
461  if (color_depth)
462  vpDisplay::flush(I_depth_color);
463  else
464  vpDisplay::flush(I_depth);
465 
466  if (save_video) {
467  if (O.getSize() == 0) {
468  O.resize(I_color.getHeight(), I_color.getWidth() + I_depth_color.getWidth());
469  writer.open(O);
470  }
471 
472  O.insert(I_color, vpImagePoint());
473  if (!color_depth)
474  vpImageConvert::convert(I_depth, I_depth_color);
475  O.insert(I_depth_color, vpImagePoint(0, I_color.getWidth()));
476  writer.saveFrame(O);
477  }
478 
480  if (vpDisplay::getClick(I_color, button, click)) {
481  switch (button) {
483  if (!quit)
484  quit = !click;
485  break;
486 
488  click = !click;
489  break;
490 
491  default:
492  break;
493  }
494  }
495 
496  vpTime::wait(t, 30);
497  cpt_frame++;
498  }
499 
500  return EXIT_SUCCESS;
501 }
502 #else
503 int main()
504 {
505  std::cerr << "Enable C++11 or higher (cmake -DUSE_CXX_STANDARD=11) and install X11 or GDI!" << std::endl;
506  return EXIT_SUCCESS;
507 }
508 #endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
void setPosition(int posx, int posy)
void startThread(std::mutex &mutex, pcl::PointCloud< pcl::PointXYZ >::Ptr pointcloud)
void setWindowName(const std::string &window_name)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
@ ioError
I/O error.
Definition: vpException.h:67
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 read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:542
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:637
unsigned int getSize() const
Definition: vpImage.h:221
unsigned int getHeight() const
Definition: vpImage.h:181
Error that can be emitted by the vpIoTools class and its derivatives.
Definition: vpIoException.h:55
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1205
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
VISP_EXPORT npz_t npz_load(std::string fname)
std::map< std::string, NpyArray > npz_t
Definition: vpIoTools.h:130
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
VISP_EXPORT double measureTimeMs()
std::shared_ptr< std::vector< char > > data_holder
Definition: vpIoTools.h:123
std::vector< size_t > shape
Definition: vpIoTools.h:124