Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testRealSense2_D435_opencv.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 (OpenCV demo).
32  */
38 #include <iostream>
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_REALSENSE2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && \
43  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
44 
45 #include <visp3/core/vpImage.h>
46 #include <visp3/core/vpImageConvert.h>
47 #include <visp3/core/vpMeterPixelConversion.h>
48 #include <visp3/sensor/vpRealSense2.h>
49 
50 #include <opencv2/core.hpp>
51 #include <opencv2/highgui.hpp>
52 
53 namespace
54 {
55 struct float3
56 {
57  float x, y, z;
58  float3() : x(0), y(0), z(0) { }
59  float3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
60 };
61 
62 void getPointcloud(const rs2::depth_frame &depth_frame, std::vector<float3> &pointcloud)
63 {
64  auto vf = depth_frame.as<rs2::video_frame>();
65  const int width = vf.get_width();
66  const int height = vf.get_height();
67  pointcloud.resize((size_t)(width * height));
68 
69  rs2::pointcloud pc;
70  rs2::points points = pc.calculate(depth_frame);
71  auto vertices = points.get_vertices();
72  for (size_t i = 0; i < points.size(); i++) {
73  float3 pcl;
74  if (vertices[i].z > std::numeric_limits<float>::epsilon()) {
75  pcl.x = vertices[i].x;
76  pcl.y = vertices[i].y;
77  pcl.z = vertices[i].z;
78  }
79 
80  pointcloud[i] = pcl;
81  }
82 }
83 
84 void createDepthHist(std::vector<uint32_t> &histogram, const std::vector<float3> &pointcloud, float depth_scale)
85 {
86  std::fill(histogram.begin(), histogram.end(), 0);
87 
88  for (size_t i = 0; i < pointcloud.size(); i++) {
89  const float3 &pt = pointcloud[i];
90  ++histogram[static_cast<uint32_t>(pt.z * depth_scale)];
91  }
92 
93  for (int i = 2; i < 0x10000; i++)
94  histogram[i] += histogram[i - 1]; // Build a cumulative histogram for
95  // the indices in [1,0xFFFF]
96 }
97 
98 unsigned char getDepthColor(const std::vector<uint32_t> &histogram, float z, float depth_scale)
99 {
100  // 0-255 based on histogram location
101  return static_cast<unsigned char>(histogram[static_cast<uint32_t>(z * depth_scale)] * 255 / histogram[0xFFFF]);
102 }
103 
104 void frame_to_mat(const rs2::frame &f, cv::Mat &img)
105 {
106  auto vf = f.as<rs2::video_frame>();
107  const int w = vf.get_width();
108  const int h = vf.get_height();
109  const int size = w * h;
110 
111  if (f.get_profile().format() == RS2_FORMAT_BGR8) {
112  memcpy(static_cast<void *>(img.ptr<cv::Vec3b>()), f.get_data(), size * 3);
113  }
114  else if (f.get_profile().format() == RS2_FORMAT_RGB8) {
115  cv::Mat tmp(h, w, CV_8UC3, const_cast<void *>(f.get_data()), cv::Mat::AUTO_STEP);
116  cv::cvtColor(tmp, img, cv::COLOR_RGB2BGR);
117  }
118  else if (f.get_profile().format() == RS2_FORMAT_Y8) {
119  memcpy(img.ptr<uchar>(), f.get_data(), size);
120  }
121 }
122 } // namespace
123 
124 int main()
125 {
126 #ifdef ENABLE_VISP_NAMESPACE
127  using namespace VISP_NAMESPACE_NAME;
128 #endif
129  const int width = 640, height = 480, fps = 60;
130  vpRealSense2 rs;
131  rs2::config config;
132  config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_BGR8, fps);
133  config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
134  config.enable_stream(RS2_STREAM_INFRARED, 1, width, height, RS2_FORMAT_Y8, fps);
135  config.enable_stream(RS2_STREAM_INFRARED, 2, width, height, RS2_FORMAT_Y8, fps);
136  rs.open(config);
137 
138  rs2::pipeline_profile &profile = rs.getPipelineProfile();
139  rs2::pipeline &pipe = rs.getPipeline();
140  float depth_scale = 1 / rs.getDepthScale();
141 
142  // initialize the image sizes
143  // width and height can also be used instead
144  auto color_profile = profile.get_stream(RS2_STREAM_COLOR).as<rs2::video_stream_profile>();
145  cv::Mat mat_color(color_profile.height(), color_profile.width(), CV_8UC3);
146 
147  auto depth_profile = profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
148  cv::Mat mat_depth(depth_profile.height(), depth_profile.width(), CV_8UC3);
149  rs2::colorizer color_map;
150 
151  auto infrared_profile = profile.get_stream(RS2_STREAM_INFRARED).as<rs2::video_stream_profile>();
152  cv::Mat mat_infrared1(infrared_profile.height(), infrared_profile.width(), CV_8UC1);
153  cv::Mat mat_infrared2(infrared_profile.height(), infrared_profile.width(), CV_8UC1);
154 
155  std::vector<float3> pointcloud;
156  cv::Mat mat_pointcloud(depth_profile.height(), depth_profile.width(), CV_8UC1);
157  std::vector<uint32_t> histogram(0x10000);
158 
159  vpCameraParameters cam_projection = rs.getCameraParameters(RS2_STREAM_DEPTH);
160 
161  std::vector<double> time_vector;
162  vpChrono chrono;
163  while (true) {
164  chrono.start();
165 
166  auto data = pipe.wait_for_frames();
167  frame_to_mat(data.get_color_frame(), mat_color);
168 #if (RS2_API_VERSION >= ((2 * 10000) + (16 * 100) + 0))
169  frame_to_mat(data.get_depth_frame().apply_filter(color_map), mat_depth);
170 #else
171  frame_to_mat(color_map(data.get_depth_frame()), mat_depth);
172 #endif
173 
174  cv::imshow("OpenCV color", mat_color);
175  cv::imshow("OpenCV depth", mat_depth);
176 
177 #if (RS2_API_VERSION >= ((2 * 10000) + (10 * 100) + 0))
178  // rs2::frameset::get_infrared_frame() introduced in librealsense 2.10.0
179  frame_to_mat(data.get_infrared_frame(1), mat_infrared1);
180  frame_to_mat(data.get_infrared_frame(2), mat_infrared2);
181 
182  cv::imshow("OpenCV infrared left", mat_infrared1);
183  cv::imshow("OpenCV infrared right", mat_infrared2);
184 #endif
185 
186  getPointcloud(data.get_depth_frame(), pointcloud);
187  createDepthHist(histogram, pointcloud, depth_scale);
188 
189  mat_pointcloud = 0;
190  for (size_t i = 0; i < pointcloud.size(); i++) {
191  const float3 &pt = pointcloud[i];
192  float Z = pt.z;
193  if (Z > 1e-2) {
194  double x = pt.x / Z;
195  double y = pt.y / Z;
196 
197  vpImagePoint imPt;
198  vpMeterPixelConversion::convertPoint(cam_projection, x, y, imPt);
199  int u = std::min<int>(static_cast<int>(width - 1), static_cast<int>(std::max<double>(0.0, imPt.get_u())));
200  int v = std::min<int>(static_cast<int>(height - 1), static_cast<int>(std::max<double>(0.0, imPt.get_v())));
201  unsigned char depth_viz = getDepthColor(histogram, Z, depth_scale);
202  mat_pointcloud.at<uchar>(v, u) = depth_viz;
203  }
204  }
205  cv::imshow("OpenCV projected pointcloud", mat_pointcloud);
206 
207  chrono.stop();
208  time_vector.push_back(chrono.getDurationMs());
209  if (cv::waitKey(5) == 27 || cv::waitKey(5) == 113) { // Esc or q
210  break;
211  }
212  }
213 
214  std::cout << "Acquisition - Mean time: " << vpMath::getMean(time_vector)
215  << " ms ; Median time: " << vpMath::getMedian(time_vector) << " ms" << std::endl;
216 
217  return EXIT_SUCCESS;
218 }
219 #else
220 int main()
221 {
222 #if !defined(VISP_HAVE_REALSENSE2)
223  std::cout << "Install librealsense2 to make this test work." << std::endl;
224 #endif
225 #if !(VISP_HAVE_OPENCV_VERSION >= 0x030000)
226  std::cout << "Install OpenCV version >= 3 to make this test work." << std::endl;
227 #endif
228  return EXIT_SUCCESS;
229 }
230 #endif
Generic class defining intrinsic camera parameters.
void start(bool reset=true)
Definition: vpTime.cpp:401
void stop()
Definition: vpTime.cpp:416
double getDurationMs()
Definition: vpTime.cpp:390
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:322
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:302
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
rs2::pipeline_profile & getPipelineProfile()
Get a reference to rs2::pipeline_profile.
Definition: vpRealSense2.h:386
bool open(const rs2::config &cfg=rs2::config())
float getDepthScale()
rs2::pipeline & getPipeline()
Get a reference to rs2::pipeline.
Definition: vpRealSense2.h:383