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