Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
grabRealSense2.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  * Acquisition with RealSense RGB-D sensor and librealsense2.
33  *
34 *****************************************************************************/
35 
42 #include <iostream>
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_HAVE_REALSENSE2) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
47 
48 #include <visp3/core/vpTime.h>
49 #include <visp3/core/vpImageConvert.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()
56 {
57 #ifdef ENABLE_VISP_NAMESPACE
58  using namespace VISP_NAMESPACE_NAME;
59 #endif
60 
61  try {
62  vpRealSense2 rs;
63 
64  std::string product_line = rs.getProductLine();
65  std::cout << "Product line: " << product_line << std::endl;
66 
67  if (product_line == "T200") {
68  std::cout << "This example doesn't support T200 product line family !" << std::endl;
69  return EXIT_SUCCESS;
70  }
71  rs2::config config;
72  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
73  config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
74  config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
75  rs.open(config);
76 
78  << std::endl;
80  << std::endl;
81  std::cout << "Extrinsics cMd: \n" << rs.getTransformation(RS2_STREAM_COLOR, RS2_STREAM_DEPTH) << std::endl;
82  std::cout << "Extrinsics dMc: \n" << rs.getTransformation(RS2_STREAM_DEPTH, RS2_STREAM_COLOR) << std::endl;
83  std::cout << "Extrinsics cMi: \n" << rs.getTransformation(RS2_STREAM_COLOR, RS2_STREAM_INFRARED) << std::endl;
84  std::cout << "Extrinsics dMi: \n" << rs.getTransformation(RS2_STREAM_DEPTH, RS2_STREAM_INFRARED) << std::endl;
85 
86  vpImage<vpRGBa> color((unsigned int)rs.getIntrinsics(RS2_STREAM_COLOR).height,
87  (unsigned int)rs.getIntrinsics(RS2_STREAM_COLOR).width);
88  vpImage<unsigned char> infrared((unsigned int)rs.getIntrinsics(RS2_STREAM_INFRARED).height,
89  (unsigned int)rs.getIntrinsics(RS2_STREAM_INFRARED).width);
90  vpImage<vpRGBa> depth_display((unsigned int)rs.getIntrinsics(RS2_STREAM_DEPTH).height,
91  (unsigned int)rs.getIntrinsics(RS2_STREAM_DEPTH).width);
92  vpImage<uint16_t> depth(depth_display.getHeight(), depth_display.getWidth());
93 
94 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_THREADS)
95  std::mutex mutex;
96  pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(new pcl::PointCloud<pcl::PointXYZRGB>());
97 #if defined(VISP_HAVE_PCL_VISUALIZATION)
98  vpDisplayPCL pcl_viewer(color.getWidth() + 80, color.getHeight() + 70, "3D viewer " + vpTime::getDateTime());
99  pcl_viewer.startThread(std::ref(mutex), pointcloud_color);
100 #endif
101 #endif
102 
103 #if defined(VISP_HAVE_X11)
104  vpDisplayX dc(color, 10, 10, "Color image");
105  vpDisplayX di(infrared, static_cast<int>(color.getWidth()) + 80, 10, "Infrared image");
106  vpDisplayX dd(depth_display, 10, static_cast<int>(color.getHeight()) + 70, "Depth image");
107 #elif defined(VISP_HAVE_GDI)
108  vpDisplayGDI dc(color, 10, 10, "Color image");
109  vpDisplayGDI di(infrared, color.getWidth() + 80, 10, "Infrared image");
110  vpDisplayGDI dd(depth_display, 10, color.getHeight() + 70, "Depth image");
111 #endif
112 
113  while (true) {
114  double t = vpTime::measureTimeMs();
115 
116 #if defined(VISP_HAVE_PCL) && defined(VISP_HAVE_PCL_COMMON) && defined(VISP_HAVE_THREADS)
117  {
118  std::lock_guard<std::mutex> lock(mutex);
119  rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth.bitmap), nullptr, pointcloud_color,
120  reinterpret_cast<unsigned char *>(infrared.bitmap));
121  }
122 #else
123  rs.acquire(reinterpret_cast<unsigned char *>(color.bitmap), reinterpret_cast<unsigned char *>(depth.bitmap), nullptr, reinterpret_cast<unsigned char *>(infrared.bitmap));
124 #endif
125 
126  vpImageConvert::createDepthHistogram(depth, depth_display);
127 
128  vpDisplay::display(color);
129  vpDisplay::display(infrared);
130  vpDisplay::display(depth_display);
131 
132  vpDisplay::displayText(color, 15, 15, "Click to quit", vpColor::red);
133  if (vpDisplay::getClick(color, false) || vpDisplay::getClick(infrared, false) ||
134  vpDisplay::getClick(depth_display, false)) {
135  break;
136  }
137  vpDisplay::flush(color);
138  vpDisplay::flush(infrared);
139  vpDisplay::flush(depth_display);
140 
141  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
142  }
143 
144  std::cout << "RealSense sensor characteristics: \n" << rs << std::endl;
145  }
146  catch (const vpException &e) {
147  std::cerr << "RealSense error " << e.what() << std::endl;
148  }
149  catch (const std::exception &e) {
150  std::cerr << e.what() << std::endl;
151  }
152 
153  return EXIT_SUCCESS;
154 }
155 #else
156 int main()
157 {
158 #if !defined(VISP_HAVE_REALSENSE2)
159  std::cout << "You do not have realsense2 SDK functionality enabled..." << std::endl;
160  std::cout << "Tip:" << std::endl;
161  std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
162  return EXIT_SUCCESS;
163 #endif
164  return EXIT_SUCCESS;
165 }
166 #endif
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
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
const char * what() const
Definition: vpException.cpp:71
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
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
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
VISP_EXPORT double measureTimeMs()