Visual Servoing Platform  version 3.6.1 under development (2024-05-02)
testRealSense2_D435_pcl.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  * Test Intel RealSense acquisition with librealsense2 (PCL demo).
33  *
34 *****************************************************************************/
40 #include <iostream>
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_THREADS) \
45  && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_VISUALIZATION)
46 
47 #include <visp3/core/vpImage.h>
48 #include <visp3/core/vpImageConvert.h>
49 #include <visp3/core/vpTime.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayX.h>
52 #include <visp3/gui/vpDisplayPCL.h>
53 #include <visp3/sensor/vpRealSense2.h>
54 
55 int main(int argc, char *argv[])
56 {
57  bool opt_pcl_color = false;
58  bool opt_show_infrared2 = false;
59  bool display_helper = false;
60 
61  for (int i = 1; i < argc; i++) {
62  if (std::string(argv[i]) == "--pcl-color") {
63  opt_pcl_color = true;
64  }
65  else if (std::string(argv[i]) == "--show-infrared2") {
66  opt_show_infrared2 = true;
67  }
68  else if ((std::string(argv[i]) == "--help") || (std::string(argv[i]) == "-h")) {
69  display_helper = true;
70  }
71  else {
72  display_helper = true;
73  std::cout << "\nERROR" << std::endl;
74  std::cout << " Wrong command line option." << std::endl;
75  }
76  if (display_helper) {
77  std::cout << "\nSYNOPSIS " << std::endl
78  << " " << argv[0]
79  << " [--pcl-color]"
80  << " [--show-infrared2]"
81  << " [--help,-h]"
82  << std::endl;
83  std::cout << "\nOPTIONS " << std::endl
84  << " --pcl-color" << std::endl
85  << " Enable textured point cloud visualization." << std::endl
86  << std::endl
87  << " --show-infrared2" << std::endl
88  << " Display also the infrared2 stream." << std::endl
89  << std::endl
90  << " --help, -h" << std::endl
91  << " Display this helper message." << std::endl
92  << std::endl;
93  return EXIT_SUCCESS;
94  }
95  }
96 
97  const int width = 640, height = 480, fps = 30;
98  vpRealSense2 rs;
99  rs2::config config;
100  config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
101  config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
102  config.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);
103  config.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);
104  rs.open(config);
105 
106  vpImage<vpRGBa> color(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
107  vpImage<vpRGBa> depth_color(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
108  vpImage<uint16_t> depth_raw(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
109  vpImage<unsigned char> infrared1(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
110  vpImage<unsigned char> infrared2(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
111 
112 #ifdef VISP_HAVE_X11
113  vpDisplayX d1, d2, d3, d4;
114 #else
115  vpDisplayGDI d1, d2, d3, d4;
116 #endif
117  d1.init(color, 0, 0, "Color");
118  d2.init(depth_color, color.getWidth() + 80, 0, "Depth");
119  d3.init(infrared1, 0, color.getHeight() + 70, "Infrared left");
120  if (opt_show_infrared2) {
121  d4.init(infrared2, color.getWidth(), color.getHeight() + 100, "Infrared right");
122  }
123 
124  std::mutex mutex;
125  pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
126  pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(new pcl::PointCloud<pcl::PointXYZRGB>());
127  vpDisplayPCL pcl_viewer(color.getWidth() + 80, color.getHeight() + 70, "3D viewer " + vpTime::getDateTime());
128  if (opt_pcl_color) {
129  pcl_viewer.startThread(std::ref(mutex), pointcloud_color);
130  }
131  else {
132  pcl_viewer.startThread(std::ref(mutex), pointcloud);
133  }
134  std::vector<double> time_vector;
135  vpChrono chrono;
136  while (true) {
137  chrono.start();
138  {
139  std::lock_guard<std::mutex> lock(mutex);
140 
141  if (opt_pcl_color) {
142  rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth_raw.bitmap),
143  nullptr, pointcloud_color, reinterpret_cast<unsigned char *>(infrared1.bitmap),
144  opt_show_infrared2 ? reinterpret_cast<unsigned char *>(infrared2.bitmap) : nullptr, nullptr);
145  }
146  else {
147  rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth_raw.bitmap),
148  nullptr, pointcloud, reinterpret_cast<unsigned char *>(infrared1.bitmap),
149  opt_show_infrared2 ? reinterpret_cast<unsigned char *>(infrared2.bitmap) : nullptr, nullptr);
150  }
151  }
152 
153  vpImageConvert::createDepthHistogram(depth_raw, depth_color);
154 
155  vpDisplay::display(color);
156  vpDisplay::display(depth_color);
157  vpDisplay::display(infrared1);
158  vpDisplay::display(infrared2);
159 
160  vpDisplay::displayText(color, 20, 20, "Click to quit.", vpColor::red);
161  vpDisplay::displayText(depth_color, 20, 20, "Click to quit.", vpColor::red);
162  vpDisplay::displayText(infrared1, 20, 20, "Click to quit.", vpColor::red);
163  vpDisplay::displayText(infrared2, 20, 20, "Click to quit.", vpColor::red);
164 
165  vpDisplay::flush(color);
166  vpDisplay::flush(depth_color);
167  vpDisplay::flush(infrared1);
168  vpDisplay::flush(infrared2);
169 
170  chrono.stop();
171  time_vector.push_back(chrono.getDurationMs());
172  if (vpDisplay::getClick(color, false) || vpDisplay::getClick(depth_color, false) ||
173  vpDisplay::getClick(infrared1, false) || vpDisplay::getClick(infrared2, false)) {
174  break;
175  }
176  }
177 
178  std::cout << "Acquisition - Mean time: " << vpMath::getMean(time_vector)
179  << " ms ; Median time: " << vpMath::getMedian(time_vector) << " ms" << std::endl;
180 
181  return EXIT_SUCCESS;
182 }
183 
184 #else
185 int main()
186 {
187 #if !defined(VISP_HAVE_REALSENSE2)
188  std::cout << "Install librealsense2 to make this test work." << std::endl;
189 #endif
190 #if !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_GDI)
191  std::cout << "X11 or GDI are needed." << std::endl;
192 #endif
193 #if !defined(VISP_HAVE_PCL)
194  std::cout << "Install PCL to make this test work." << std::endl;
195 #endif
196  return EXIT_SUCCESS;
197 }
198 #endif
void start(bool reset=true)
Definition: vpTime.cpp:399
void stop()
Definition: vpTime.cpp:414
double getDurationMs()
Definition: vpTime.cpp:388
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
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 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 double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:323
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:303
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")