Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testRealSense2_D435_pcl.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Test Intel RealSense acquisition with librealsense2 (PCL demo).
32 */
37
38#include <iostream>
39
40#include <visp3/core/vpConfig.h>
41
42#if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_THREADS) \
43 && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_PCL_VISUALIZATION)
44
45#include <visp3/core/vpImage.h>
46#include <visp3/core/vpImageConvert.h>
47#include <visp3/core/vpTime.h>
48#include <visp3/gui/vpDisplayGDI.h>
49#include <visp3/gui/vpDisplayX.h>
50#include <visp3/gui/vpDisplayPCL.h>
51#include <visp3/sensor/vpRealSense2.h>
52
53int main(int argc, char *argv[])
54{
55#ifdef ENABLE_VISP_NAMESPACE
56 using namespace VISP_NAMESPACE_NAME;
57#endif
58 bool opt_pcl_color = false;
59 bool opt_show_infrared2 = false;
60 bool display_helper = false;
61
62 for (int i = 1; i < argc; i++) {
63 if (std::string(argv[i]) == "--pcl-color") {
64 opt_pcl_color = true;
65 }
66 else if (std::string(argv[i]) == "--show-infrared2") {
67 opt_show_infrared2 = true;
68 }
69 else if ((std::string(argv[i]) == "--help") || (std::string(argv[i]) == "-h")) {
70 display_helper = true;
71 }
72 else {
73 display_helper = true;
74 std::cout << "\nERROR" << std::endl;
75 std::cout << " Wrong command line option." << std::endl;
76 }
77 if (display_helper) {
78 std::cout << "\nSYNOPSIS " << std::endl
79 << " " << argv[0]
80 << " [--pcl-color]"
81 << " [--show-infrared2]"
82 << " [--help,-h]"
83 << std::endl;
84 std::cout << "\nOPTIONS " << std::endl
85 << " --pcl-color" << std::endl
86 << " Enable textured point cloud visualization." << std::endl
87 << std::endl
88 << " --show-infrared2" << std::endl
89 << " Display also the infrared2 stream." << std::endl
90 << std::endl
91 << " --help, -h" << std::endl
92 << " Display this helper message." << std::endl
93 << std::endl;
94 return EXIT_SUCCESS;
95 }
96 }
97
98 const int width = 640, height = 480, fps = 30;
99 vpRealSense2 rs;
100 rs2::config config;
101 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
102 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
103 config.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);
104 config.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);
105 rs.open(config);
106
107 vpImage<vpRGBa> color(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
108 vpImage<vpRGBa> depth_color(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
109 vpImage<uint16_t> depth_raw(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
110 vpImage<unsigned char> infrared1(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
111 vpImage<unsigned char> infrared2(static_cast<unsigned int>(height), static_cast<unsigned int>(width));
112
113#ifdef VISP_HAVE_X11
114 vpDisplayX d1, d2, d3, d4;
115#else
116 vpDisplayGDI d1, d2, d3, d4;
117#endif
118 d1.init(color, 0, 0, "Color");
119 d2.init(depth_color, color.getWidth() + 80, 0, "Depth");
120 d3.init(infrared1, 0, color.getHeight() + 70, "Infrared left");
121 if (opt_show_infrared2) {
122 d4.init(infrared2, color.getWidth(), color.getHeight() + 100, "Infrared right");
123 }
124
125 std::mutex mutex;
126 pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
127 pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(new pcl::PointCloud<pcl::PointXYZRGB>());
128 vpDisplayPCL pcl_viewer(color.getWidth() + 80, color.getHeight() + 70, "3D viewer " + vpTime::getDateTime());
129 if (opt_pcl_color) {
130 pcl_viewer.startThread(std::ref(mutex), pointcloud_color);
131 }
132 else {
133 pcl_viewer.startThread(std::ref(mutex), pointcloud);
134 }
135 std::vector<double> time_vector;
136 vpChrono chrono;
137 while (true) {
138 chrono.start();
139 {
140 std::lock_guard<std::mutex> lock(mutex);
141
142 if (opt_pcl_color) {
143 rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth_raw.bitmap),
144 nullptr, pointcloud_color, reinterpret_cast<unsigned char *>(infrared1.bitmap),
145 opt_show_infrared2 ? reinterpret_cast<unsigned char *>(infrared2.bitmap) : nullptr, nullptr);
146 }
147 else {
148 rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth_raw.bitmap),
149 nullptr, pointcloud, reinterpret_cast<unsigned char *>(infrared1.bitmap),
150 opt_show_infrared2 ? reinterpret_cast<unsigned char *>(infrared2.bitmap) : nullptr, nullptr);
151 }
152 }
153
154 vpImageConvert::createDepthHistogram(depth_raw, depth_color);
155
156 vpDisplay::display(color);
157 vpDisplay::display(depth_color);
158 vpDisplay::display(infrared1);
159 vpDisplay::display(infrared2);
160
161 vpDisplay::displayText(color, 20, 20, "Click to quit.", vpColor::red);
162 vpDisplay::displayText(depth_color, 20, 20, "Click to quit.", vpColor::red);
163 vpDisplay::displayText(infrared1, 20, 20, "Click to quit.", vpColor::red);
164 vpDisplay::displayText(infrared2, 20, 20, "Click to quit.", vpColor::red);
165
166 vpDisplay::flush(color);
167 vpDisplay::flush(depth_color);
168 vpDisplay::flush(infrared1);
169 vpDisplay::flush(infrared2);
170
171 chrono.stop();
172 time_vector.push_back(chrono.getDurationMs());
173 if (vpDisplay::getClick(color, false) || vpDisplay::getClick(depth_color, false) ||
174 vpDisplay::getClick(infrared1, false) || vpDisplay::getClick(infrared2, false)) {
175 break;
176 }
177 }
178
179 std::cout << "Acquisition - Mean time: " << vpMath::getMean(time_vector)
180 << " ms ; Median time: " << vpMath::getMedian(time_vector) << " ms" << std::endl;
181
182 return EXIT_SUCCESS;
183}
184
185#else
186int main()
187{
188#if !defined(VISP_HAVE_REALSENSE2)
189 std::cout << "Install librealsense2 to make this test work." << std::endl;
190#endif
191#if !defined(VISP_HAVE_X11) && !defined(VISP_HAVE_GDI)
192 std::cout << "X11 or GDI are needed." << std::endl;
193#endif
194#if !defined(VISP_HAVE_PCL)
195 std::cout << "Install PCL to make this test work." << std::endl;
196#endif
197 return EXIT_SUCCESS;
198}
199#endif
void start(bool reset=true)
Definition vpTime.cpp:411
void stop()
Definition vpTime.cpp:426
double getDurationMs()
Definition vpTime.cpp:400
static const vpColor red
Definition vpColor.h:198
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:135
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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double getMedian(const std::vector< double > &v)
Definition vpMath.cpp:343
static double getMean(const std::vector< double > &v)
Definition vpMath.cpp:323
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")