Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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 (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && (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#ifdef VISP_HAVE_PCL
51#include <pcl/common/common.h>
52#include <pcl/io/pcd_io.h>
53#include <pcl/visualization/cloud_viewer.h>
54#endif
55
56#if defined(VISP_HAVE_PCL) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
57#define USE_PCL_VIEWER
58#endif
59
60#include <visp3/core/vpImageConvert.h>
61#include <visp3/core/vpIoTools.h>
62#include <visp3/gui/vpDisplayGDI.h>
63#include <visp3/gui/vpDisplayX.h>
64#include <visp3/io/vpImageIo.h>
65#include <visp3/io/vpParseArgv.h>
66#include <visp3/io/vpVideoWriter.h>
67
68#define GETOPTARGS "ci:bodh"
69
70namespace
71{
72
73void usage(const char *name, const char *badparam)
74{
75 fprintf(stdout, "\n\
76 Read RealSense data.\n\
77 \n\
78 %s\
79 OPTIONS: \n\
80 -i <directory> \n\
81 Input directory.\n\
82 \n\
83 -c \n\
84 Click enable.\n\
85 \n\
86 -b \n\
87 Pointcloud is in binary format.\n\
88 \n\
89 -o \n\
90 Save color and depth side by side to image sequence.\n\
91 \n\
92 -d \n\
93 Display depth in color.\n\
94 \n\
95 -h \n\
96 Print the help.\n\n",
97 name);
98
99 if (badparam)
100 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101}
102
103bool getOptions(int argc, char **argv, std::string &input_directory, bool &click, bool &pointcloud_binary_format,
104 bool &save_video, bool &color_depth)
105{
106 const char *optarg;
107 const char **argv1 = (const char **)argv;
108 int c;
109 while ((c = vpParseArgv::parse(argc, argv1, GETOPTARGS, &optarg)) > 1) {
110
111 switch (c) {
112 case 'i':
113 input_directory = optarg;
114 break;
115 case 'c':
116 click = true;
117 break;
118 case 'b':
119 pointcloud_binary_format = true;
120 break;
121 case 'o':
122 save_video = true;
123 break;
124 case 'd':
125 color_depth = true;
126 break;
127
128 case 'h':
129 usage(argv[0], NULL);
130 return false;
131 break;
132
133 default:
134 usage(argv[0], optarg);
135 return false;
136 break;
137 }
138 }
139
140 if ((c == 1) || (c == -1)) {
141 // standalone param or error
142 usage(argv[0], NULL);
143 std::cerr << "ERROR: " << std::endl;
144 std::cerr << " Bad argument " << optarg << std::endl << std::endl;
145 return false;
146 }
147
148 return true;
149}
150
151#ifdef USE_PCL_VIEWER
152pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
153bool cancelled = false, update_pointcloud = false;
154
155class ViewerWorker
156{
157public:
158 explicit ViewerWorker(std::mutex &mutex) : m_mutex(mutex) { }
159
160 void run()
161 {
162 pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
163
164 bool local_update = false, local_cancelled = false;
165 pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
166 viewer->setBackgroundColor(0, 0, 0);
167 viewer->initCameraParameters();
168 viewer->setPosition(640 + 80, 480 + 80);
169 viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
170 viewer->setSize(640, 480);
171
172 bool first_init = true;
173 while (!local_cancelled) {
174 {
175 std::unique_lock<std::mutex> lock(m_mutex, std::try_to_lock);
176
177 if (lock.owns_lock()) {
178 local_update = update_pointcloud;
179 update_pointcloud = false;
180 local_cancelled = cancelled;
181 local_pointcloud = pointcloud->makeShared();
182 }
183 }
184
185 if (local_update && !local_cancelled) {
186 local_update = false;
187
188 if (first_init) {
189 viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
190 viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
191 first_init = false;
192 }
193 else {
194 viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
195 }
196 }
197
198 viewer->spinOnce(10);
199 }
200
201 std::cout << "End of point cloud display thread" << std::endl;
202 }
203
204private:
205 std::mutex &m_mutex;
206};
207#endif
208
209bool readData(int cpt, const std::string &input_directory, vpImage<vpRGBa> &I_color, vpImage<uint16_t> &I_depth_raw,
210 bool pointcloud_binary_format
211#ifdef USE_PCL_VIEWER
212 ,
213 pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud
214#endif
215)
216{
217 char buffer[FILENAME_MAX];
218 std::stringstream ss;
219 ss << input_directory << "/color_image_%04d.jpg";
220 snprintf(buffer, FILENAME_MAX, ss.str().c_str(), cpt);
221 std::string filename_color = buffer;
222
223 ss.str("");
224 ss << input_directory << "/depth_image_%04d.bin";
225 snprintf(buffer, FILENAME_MAX, ss.str().c_str(), cpt);
226 std::string filename_depth = buffer;
227
228 ss.str("");
229 ss << input_directory << "/point_cloud_%04d" << (pointcloud_binary_format ? ".bin" : ".pcd");
230 snprintf(buffer, FILENAME_MAX, ss.str().c_str(), cpt);
231 std::string filename_pointcloud = buffer;
232
233 if (!vpIoTools::checkFilename(filename_color) && !vpIoTools::checkFilename(filename_depth) &&
234 !vpIoTools::checkFilename(filename_pointcloud)) {
235 std::cerr << "End of sequence." << std::endl;
236 return false;
237 }
238
239 // Read color
240 if (vpIoTools::checkFilename(filename_color))
241 vpImageIo::read(I_color, filename_color);
242
243 // Read raw depth
244 std::ifstream file_depth(filename_depth.c_str(), std::ios::in | std::ios::binary);
245 if (file_depth.is_open()) {
246 unsigned int height = 0, width = 0;
247 vpIoTools::readBinaryValueLE(file_depth, height);
248 vpIoTools::readBinaryValueLE(file_depth, width);
249 I_depth_raw.resize(height, width);
250
251 uint16_t depth_value = 0;
252 for (unsigned int i = 0; i < height; i++) {
253 for (unsigned int j = 0; j < width; j++) {
254 vpIoTools::readBinaryValueLE(file_depth, depth_value);
255 I_depth_raw[i][j] = depth_value;
256 }
257 }
258 }
259
260 // Read pointcloud
261#ifdef USE_PCL_VIEWER
262 if (pointcloud_binary_format) {
263 std::ifstream file_pointcloud(filename_pointcloud.c_str(), std::ios::in | std::ios::binary);
264 if (!file_pointcloud.is_open()) {
265 std::cerr << "Cannot read pointcloud file: " << filename_pointcloud << std::endl;
266 }
267
268 uint32_t height = 0, width = 0;
269 char is_dense = 1;
270 vpIoTools::readBinaryValueLE(file_pointcloud, height);
271 vpIoTools::readBinaryValueLE(file_pointcloud, width);
272 file_pointcloud.read((char *)(&is_dense), sizeof(is_dense));
273
274 point_cloud->width = width;
275 point_cloud->height = height;
276 point_cloud->is_dense = (is_dense != 0);
277 point_cloud->resize((size_t)width * height);
278
279 float x = 0.0f, y = 0.0f, z = 0.0f;
280 for (uint32_t i = 0; i < height; i++) {
281 for (uint32_t j = 0; j < width; j++) {
282 vpIoTools::readBinaryValueLE(file_pointcloud, x);
283 vpIoTools::readBinaryValueLE(file_pointcloud, y);
284 vpIoTools::readBinaryValueLE(file_pointcloud, z);
285
286 point_cloud->points[(size_t)(i * width + j)].x = x;
287 point_cloud->points[(size_t)(i * width + j)].y = y;
288 point_cloud->points[(size_t)(i * width + j)].z = z;
289 }
290 }
291 }
292 else {
293 if (pcl::io::loadPCDFile<pcl::PointXYZ>(filename_pointcloud, *point_cloud) == -1) {
294 std::cerr << "Cannot read PCD: " << filename_pointcloud << std::endl;
295 }
296 }
297#endif
298
299 return true;
300}
301} // Namespace
302
303int main(int argc, char *argv[])
304{
305 std::string input_directory = "";
306 bool click = false;
307 bool pointcloud_binary_format = false;
308 bool save_video = false;
309 bool color_depth = false;
310
311 // Read the command line options
312 if (!getOptions(argc, argv, input_directory, click, pointcloud_binary_format, save_video, color_depth)) {
313 return EXIT_FAILURE;
314 }
315
316 vpImage<vpRGBa> I_color(480, 640), I_depth_color(480, 640);
317 vpImage<uint16_t> I_depth_raw(480, 640);
318 vpImage<unsigned char> I_depth(480, 640);
319
320#ifdef VISP_HAVE_X11
321 vpDisplayX d1, d2;
322#else
323 vpDisplayGDI d1, d2;
324#endif
325 bool init_display = false;
326
327#ifdef USE_PCL_VIEWER
328 std::mutex mutex;
329 ViewerWorker viewer(mutex);
330 std::thread viewer_thread(&ViewerWorker::run, &viewer);
331#endif
332
333 vpVideoWriter writer;
335 if (save_video) {
336 std::string output_directory = vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
337 vpIoTools::makeDirectory(output_directory);
338 writer.setFileName(output_directory + "/%04d.png");
339 }
340
341 int cpt_frame = 0;
342 bool quit = false;
343 while (!quit) {
344 double t = vpTime::measureTimeMs();
345
346#ifdef USE_PCL_VIEWER
347 {
348 std::lock_guard<std::mutex> lock(mutex);
349 update_pointcloud = true;
350 quit = !readData(cpt_frame, input_directory, I_color, I_depth_raw, pointcloud_binary_format, pointcloud);
351 }
352#else
353 quit = !readData(cpt_frame, input_directory, I_color, I_depth_raw, pointcloud_binary_format);
354#endif
355
356 vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
357 if (color_depth)
358 vpImageConvert::createDepthHistogram(I_depth_raw, I_depth_color);
359
360 if (!init_display) {
361 init_display = true;
362 d1.init(I_color, 0, 0, "Color image");
363 if (color_depth)
364 d2.init(I_depth_color, I_color.getWidth() + 10, 0, "Depth image");
365 else
366 d2.init(I_depth, I_color.getWidth() + 10, 0, "Depth image");
367 }
368
369 vpDisplay::display(I_color);
370 if (color_depth)
371 vpDisplay::display(I_depth_color);
372 else
373 vpDisplay::display(I_depth);
374
375 std::stringstream ss;
376 ss << "Frame: " << cpt_frame;
377 vpDisplay::displayText(I_color, 20, 20, ss.str(), vpColor::red);
378 if (color_depth)
379 vpDisplay::displayText(I_depth_color, 20, 20, ss.str(), vpColor::red);
380 else
381 vpDisplay::displayText(I_depth, 20, 20, ss.str(), vpColor::red);
382
383 vpDisplay::flush(I_color);
384 if (color_depth)
385 vpDisplay::flush(I_depth_color);
386 else
387 vpDisplay::flush(I_depth);
388
389 if (save_video) {
390 if (O.getSize() == 0) {
391 O.resize(I_color.getHeight(), I_color.getWidth() + I_depth_color.getWidth());
392 writer.open(O);
393 }
394
395 O.insert(I_color, vpImagePoint());
396 if (!color_depth)
397 vpImageConvert::convert(I_depth, I_depth_color);
398 O.insert(I_depth_color, vpImagePoint(0, I_color.getWidth()));
399 writer.saveFrame(O);
400 }
401
403 if (vpDisplay::getClick(I_color, button, click)) {
404 switch (button) {
406 if (!quit)
407 quit = !click;
408 break;
409
411 click = !click;
412 break;
413
414 default:
415 break;
416 }
417 }
418
419 vpTime::wait(t, 30);
420 cpt_frame++;
421 }
422
423#ifdef USE_PCL_VIEWER
424 {
425 std::lock_guard<std::mutex> lock(mutex);
426 cancelled = true;
427 }
428 viewer_thread.join();
429#endif
430
431 return EXIT_SUCCESS;
432}
433#else
434int main()
435{
436 std::cerr << "Enable C++11 or higher (cmake -DUSE_CXX_STANDARD=11) and install X11 or GDI!" << std::endl;
437 return EXIT_SUCCESS;
438}
439#endif
static const vpColor red
Definition vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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)
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)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition vpImage.h:795
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition vpImage.h:1361
unsigned int getSize() const
Definition vpImage.h:223
unsigned int getHeight() const
Definition vpImage.h:184
static bool checkFilename(const std::string &filename)
static void readBinaryValueLE(std::ifstream &file, int16_t &short_value)
static void makeDirectory(const std::string &dirname)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
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 double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")