Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
grabRealSense2.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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/vpImageConvert.h>
45 #include <visp3/gui/vpDisplayGDI.h>
46 #include <visp3/gui/vpDisplayX.h>
47 #include <visp3/sensor/vpRealSense2.h>
48 
49 #if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_CPP11_COMPATIBILITY) && \
50  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
51 
52 #ifdef VISP_HAVE_PCL
53 #include <pcl/visualization/cloud_viewer.h>
54 #include <pcl/visualization/pcl_visualizer.h>
55 #include <thread>
56 #include <mutex>
57 
58 namespace
59 {
60 // Global variables
61 pcl::PointCloud<pcl::PointXYZ>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
62 pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud_color(new pcl::PointCloud<pcl::PointXYZRGB>());
63 bool cancelled = false, update_pointcloud = false;
64 
65 class ViewerWorker
66 {
67 public:
68  explicit ViewerWorker(const bool color_mode, std::mutex &mutex) : m_colorMode(color_mode), m_mutex(mutex) {}
69 
70  void run()
71  {
72  std::string date = vpTime::getDateTime();
73  pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer " + date));
74  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud_color);
75  pcl::PointCloud<pcl::PointXYZ>::Ptr local_pointcloud(new pcl::PointCloud<pcl::PointXYZ>());
76  pcl::PointCloud<pcl::PointXYZRGB>::Ptr local_pointcloud_color(new pcl::PointCloud<pcl::PointXYZRGB>());
77 
78  viewer->setBackgroundColor(0, 0, 0);
79  viewer->initCameraParameters();
80  viewer->setPosition(640 + 80, 480 + 80);
81  viewer->setCameraPosition(0, 0, -0.25, 0, -1, 0);
82  viewer->setSize(640, 480);
83 
84  bool init = true;
85  bool local_update = false, local_cancelled = false;
86  while (!local_cancelled) {
87  {
88  std::unique_lock<std::mutex> lock(m_mutex, std::try_to_lock);
89 
90  if (lock.owns_lock()) {
91  local_update = update_pointcloud;
92  update_pointcloud = false;
93  local_cancelled = cancelled;
94 
95  if (local_update) {
96  if (m_colorMode) {
97  local_pointcloud_color = pointcloud_color->makeShared();
98  } else {
99  local_pointcloud = pointcloud->makeShared();
100  }
101  }
102  }
103  }
104 
105  if (local_update && !local_cancelled) {
106  local_update = false;
107 
108  if (init) {
109  if (m_colorMode) {
110  viewer->addPointCloud<pcl::PointXYZRGB>(local_pointcloud_color, rgb, "RGB sample cloud");
111  viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1,
112  "RGB sample cloud");
113  } else {
114  viewer->addPointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
115  viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
116  }
117  init = false;
118  } else {
119  if (m_colorMode) {
120  viewer->updatePointCloud<pcl::PointXYZRGB>(local_pointcloud_color, rgb, "RGB sample cloud");
121  } else {
122  viewer->updatePointCloud<pcl::PointXYZ>(local_pointcloud, "sample cloud");
123  }
124  }
125  }
126 
127  viewer->spinOnce(5);
128  }
129 
130  std::cout << "End of point cloud display thread" << std::endl;
131  }
132 
133 private:
134  bool m_colorMode;
135  std::mutex &m_mutex;
136 };
137 }
138 #endif
139 
140 int main()
141 {
142  try {
143  vpRealSense2 rs;
144  rs2::config config;
145  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
146  config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
147  config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);
148  rs.open(config);
149 
151  << std::endl;
153  << std::endl;
154  std::cout << "Extrinsics cMd: \n" << rs.getTransformation(RS2_STREAM_COLOR, RS2_STREAM_DEPTH) << std::endl;
155  std::cout << "Extrinsics dMc: \n" << rs.getTransformation(RS2_STREAM_DEPTH, RS2_STREAM_COLOR) << std::endl;
156  std::cout << "Extrinsics cMi: \n" << rs.getTransformation(RS2_STREAM_COLOR, RS2_STREAM_INFRARED) << std::endl;
157  std::cout << "Extrinsics dMi: \n" << rs.getTransformation(RS2_STREAM_DEPTH, RS2_STREAM_INFRARED) << std::endl;
158 
159  vpImage<vpRGBa> color((unsigned int)rs.getIntrinsics(RS2_STREAM_COLOR).height,
160  (unsigned int)rs.getIntrinsics(RS2_STREAM_COLOR).width);
161  vpImage<unsigned char> infrared((unsigned int)rs.getIntrinsics(RS2_STREAM_INFRARED).height,
162  (unsigned int)rs.getIntrinsics(RS2_STREAM_INFRARED).width);
163  vpImage<vpRGBa> depth_display((unsigned int)rs.getIntrinsics(RS2_STREAM_DEPTH).height,
164  (unsigned int)rs.getIntrinsics(RS2_STREAM_DEPTH).width);
165  vpImage<uint16_t> depth(depth_display.getHeight(), depth_display.getWidth());
166 
167 #ifdef VISP_HAVE_PCL
168  std::mutex mutex;
169  ViewerWorker viewer(true, mutex);
170  std::thread viewer_thread(&ViewerWorker::run, &viewer);
171 #endif
172 
173 #if defined(VISP_HAVE_X11)
174  vpDisplayX dc(color, 10, 10, "Color image");
175  vpDisplayX di(infrared, (int)color.getWidth() + 80, 10, "Infrared image");
176  vpDisplayX dd(depth_display, 10, (int)color.getHeight() + 80, "Depth image");
177 #elif defined(VISP_HAVE_GDI)
178  vpDisplayGDI dc(color, 10, 10, "Color image");
179  vpDisplayGDI di(infrared, color.getWidth() + 80, 10, "Infrared image");
180  vpDisplayGDI dd(depth_display, 10, color.getHeight() + 80, "Depth image");
181 #endif
182 
183  while (true) {
184  double t = vpTime::measureTimeMs();
185 
186 #ifdef VISP_HAVE_PCL
187  {
188  std::lock_guard<std::mutex> lock(mutex);
189  rs.acquire((unsigned char *)color.bitmap, (unsigned char *)depth.bitmap, NULL, pointcloud_color,
190  (unsigned char *)infrared.bitmap);
191  update_pointcloud = true;
192  }
193 #else
194  rs.acquire((unsigned char *)color.bitmap, (unsigned char *)depth.bitmap, NULL, (unsigned char *)infrared.bitmap);
195 #endif
196 
197  vpImageConvert::createDepthHistogram(depth, depth_display);
198 
199  vpDisplay::display(color);
200  vpDisplay::display(infrared);
201  vpDisplay::display(depth_display);
202 
203  vpDisplay::displayText(color, 15, 15, "Click to quit", vpColor::red);
204  if (vpDisplay::getClick(color, false) || vpDisplay::getClick(infrared, false) ||
205  vpDisplay::getClick(depth_display, false)) {
206  break;
207  }
208  vpDisplay::flush(color);
209  vpDisplay::flush(infrared);
210  vpDisplay::flush(depth_display);
211 
212  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << std::endl;
213  }
214 
215  std::cout << "RealSense sensor characteristics: \n" << rs << std::endl;
216 
217 #ifdef VISP_HAVE_PCL
218  {
219  std::lock_guard<std::mutex> lock(mutex);
220  cancelled = true;
221  }
222 
223  viewer_thread.join();
224 #endif
225 
226  } catch (const vpException &e) {
227  std::cerr << "RealSense error " << e.what() << std::endl;
228  } catch (const std::exception &e) {
229  std::cerr << e.what() << std::endl;
230  }
231 
232  return EXIT_SUCCESS;
233 }
234 #else
235 int main()
236 {
237 #if !defined(VISP_HAVE_REALSENSE2)
238  std::cout << "You do not realsense2 SDK functionality enabled..." << std::endl;
239  std::cout << "Tip:" << std::endl;
240  std::cout << "- Install librealsense2, configure again ViSP using cmake and build again this example" << std::endl;
241  return EXIT_SUCCESS;
242 #endif
243 #if !defined(VISP_HAVE_CPP11_COMPATIBILITY)
244  std::cout << "You do not build ViSP with C++11 compiler flag" << std::endl;
245  std::cout << "Tip:" << std::endl;
246  std::cout << "- Configure ViSP again using cmake -DUSE_CPP11=ON, and build again this example" << std::endl;
247 #endif
248  return EXIT_SUCCESS;
249 }
250 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
void open(const rs2::config &cfg=rs2::config())
static void flush(const vpImage< unsigned char > &I)
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion) const
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
Definition: vpTime.cpp:345
static const vpColor red
Definition: vpColor.h:180
const char * what() const
static void display(const vpImage< unsigned char > &I)
void acquire(vpImage< unsigned char > &grey)
vpHomogeneousMatrix getTransformation(const rs2_stream &from, const rs2_stream &to) const
rs2_intrinsics getIntrinsics(const rs2_stream &stream) const
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)