Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
grabRealSense2.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Acquisition with RealSense RGB-D sensor and librealsense2.
32 */
33
39
40#include <iostream>
41
42#include <visp3/core/vpConfig.h>
43
44#if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_DISPLAY)
45
46#include <visp3/core/vpTime.h>
47#include <visp3/core/vpImageConvert.h>
48#include <visp3/gui/vpDisplayFactory.h>
49#include <visp3/gui/vpDisplayPCL.h>
50#include <visp3/sensor/vpRealSense2.h>
51
52int main()
53{
54#ifdef ENABLE_VISP_NAMESPACE
55 using namespace VISP_NAMESPACE_NAME;
56#endif
57
58#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
59 vpDisplay *dc = nullptr;
60 vpDisplay *di = nullptr;
61 vpDisplay *dd = nullptr;
62#endif
63
64 try {
65 vpRealSense2 rs;
66
67 std::string product_line = rs.getProductLine();
68 std::cout << "Product line: " << product_line << std::endl;
69
70 if (product_line == "T200") {
71 std::cout << "This example doesn't support T200 product line family !" << std::endl;
72 return EXIT_SUCCESS;
73 }
74 rs2::config config;
75 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
76 config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
77 config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
78 rs.open(config);
79
81 << std::endl;
83 << std::endl;
84 std::cout << "Extrinsics cMd: \n" << rs.getTransformation(RS2_STREAM_COLOR, RS2_STREAM_DEPTH) << std::endl;
85 std::cout << "Extrinsics dMc: \n" << rs.getTransformation(RS2_STREAM_DEPTH, RS2_STREAM_COLOR) << std::endl;
86 std::cout << "Extrinsics cMi: \n" << rs.getTransformation(RS2_STREAM_COLOR, RS2_STREAM_INFRARED) << std::endl;
87 std::cout << "Extrinsics dMi: \n" << rs.getTransformation(RS2_STREAM_DEPTH, RS2_STREAM_INFRARED) << std::endl;
88
89 vpImage<vpRGBa> color(static_cast<unsigned int>(rs.getIntrinsics(RS2_STREAM_COLOR).height),
90 static_cast<unsigned int>(rs.getIntrinsics(RS2_STREAM_COLOR).width));
91 vpImage<unsigned char> infrared(static_cast<unsigned int>(rs.getIntrinsics(RS2_STREAM_INFRARED).height),
92 static_cast<unsigned int>(rs.getIntrinsics(RS2_STREAM_INFRARED).width));
93 vpImage<vpRGBa> depth_display(static_cast<unsigned int>(rs.getIntrinsics(RS2_STREAM_DEPTH).height),
94 static_cast<unsigned int>(rs.getIntrinsics(RS2_STREAM_DEPTH).width));
95 vpImage<uint16_t> depth(depth_display.getHeight(), depth_display.getWidth());
96
97#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_THREADS)
98 std::mutex mutex;
99 pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(new pcl::PointCloud<pcl::PointXYZRGB>());
100#if defined(VISP_HAVE_PCL_VISUALIZATION)
101 vpDisplayPCL pcl_viewer(color.getWidth() + 80, color.getHeight() + 70, "3D viewer " + vpTime::getDateTime());
102 pcl_viewer.startThread(std::ref(mutex), pointcloud_color);
103#endif
104#endif
105
106#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
107 std::shared_ptr<vpDisplay> dc = vpDisplayFactory::createDisplay(color, 10, 10, "Color image");
108 std::shared_ptr<vpDisplay> di = vpDisplayFactory::createDisplay(infrared, static_cast<int>(color.getWidth()) + 80, 10, "Infrared image");
109 std::shared_ptr<vpDisplay> dd = vpDisplayFactory::createDisplay(depth_display, 10, static_cast<int>(color.getHeight()) + 70, "Depth image");
110#else
111 dc = vpDisplayFactory::allocateDisplay(color, 10, 10, "Color image");
112 di = vpDisplayFactory::allocateDisplay(infrared, static_cast<int>(color.getWidth()) + 80, 10, "Infrared image");
113 dd = vpDisplayFactory::allocateDisplay(depth_display, 10, static_cast<int>(color.getHeight()) + 70, "Depth image");
114#endif
115
116 while (true) {
117 double t = vpTime::measureTimeMs();
118
119#if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_THREADS)
120 {
121 std::lock_guard<std::mutex> lock(mutex);
122 rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth.bitmap), nullptr, pointcloud_color,
123 reinterpret_cast<unsigned char *>(infrared.bitmap));
124 }
125#else
126 rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth.bitmap), nullptr, reinterpret_cast<unsigned char *>(infrared.bitmap));
127#endif
128
129 vpImageConvert::createDepthHistogram(depth, depth_display);
130
131 vpDisplay::display(color);
132 vpDisplay::display(infrared);
133 vpDisplay::display(depth_display);
134
135 vpDisplay::displayText(color, 15, 15, "Click to quit", vpColor::red);
136 if (vpDisplay::getClick(color, false) || vpDisplay::getClick(infrared, false) ||
137 vpDisplay::getClick(depth_display, false)) {
138 break;
139 }
140 vpDisplay::flush(color);
141 vpDisplay::flush(infrared);
142 vpDisplay::flush(depth_display);
143
144 std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
145 }
146
147 std::cout << "RealSense sensor characteristics: \n" << rs << std::endl;
148 }
149 catch (const vpException &e) {
150 std::cerr << "RealSense error " << e.what() << std::endl;
151 }
152 catch (const std::exception &e) {
153 std::cerr << e.what() << std::endl;
154 }
155
156#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
157 delete dc;
158 delete di;
159 delete dd;
160#endif
161
162 return EXIT_SUCCESS;
163}
164#else
165int main()
166{
167#if !defined(VISP_HAVE_REALSENSE2)
168 std::cout << "You do not have realsense2 SDK functionality enabled..." << std::endl;
169 std::cout << "Tip:" << std::endl;
170 std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
171 return EXIT_SUCCESS;
172#endif
173 return EXIT_SUCCESS;
174}
175#endif
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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)
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
Definition of the vpImage class member functions.
Definition vpImage.h:131
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
rs2_intrinsics getIntrinsics(const rs2_stream &stream, int index=-1) const
std::string getProductLine()
vpHomogeneousMatrix getTransformation(const rs2_stream &from, const rs2_stream &to, int from_index=-1) const
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")