cv-helpers.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
7 #include <opencv2/opencv.hpp> // Include OpenCV API
8 #include <exception>
9 
10 // Convert rs2::frame to cv::Mat
11 static cv::Mat frame_to_mat(const rs2::frame& f)
12 {
13  using namespace cv;
14  using namespace rs2;
15 
16  auto vf = f.as<video_frame>();
17  const int w = vf.get_width();
18  const int h = vf.get_height();
19 
20  if (f.get_profile().format() == RS2_FORMAT_BGR8)
21  {
22  return Mat(Size(w, h), CV_8UC3, (void*)f.get_data(), Mat::AUTO_STEP);
23  }
24  else if (f.get_profile().format() == RS2_FORMAT_RGB8)
25  {
26  auto r_rgb = Mat(Size(w, h), CV_8UC3, (void*)f.get_data(), Mat::AUTO_STEP);
27  Mat r_bgr;
28  cvtColor(r_rgb, r_bgr, COLOR_RGB2BGR);
29  return r_bgr;
30  }
31  else if (f.get_profile().format() == RS2_FORMAT_Z16)
32  {
33  return Mat(Size(w, h), CV_16UC1, (void*)f.get_data(), Mat::AUTO_STEP);
34  }
35  else if (f.get_profile().format() == RS2_FORMAT_Y8)
36  {
37  return Mat(Size(w, h), CV_8UC1, (void*)f.get_data(), Mat::AUTO_STEP);
38  }
39  else if (f.get_profile().format() == RS2_FORMAT_DISPARITY32)
40  {
41  return Mat(Size(w, h), CV_32FC1, (void*)f.get_data(), Mat::AUTO_STEP);
42  }
43 
44  throw std::runtime_error("Frame format is not supported yet!");
45 }
46 
47 // Converts depth frame to a matrix of doubles with distances in meters
48 static cv::Mat depth_frame_to_meters( const rs2::depth_frame & f )
49 {
50  cv::Mat dm = frame_to_mat(f);
51  dm.convertTo( dm, CV_64F );
52  dm = dm * f.get_units();
53  return dm;
54 }
55 
rs2::frame
Definition: rs_frame.hpp:345
cv
static std::condition_variable cv
Definition: unit-tests-common.h:613
sw.h
int h
Definition: sw.py:17
RS2_FORMAT_RGB8
@ RS2_FORMAT_RGB8
Definition: rs_sensor.h:66
frame_to_mat
static cv::Mat frame_to_mat(const rs2::frame &f)
Definition: cv-helpers.hpp:11
RS2_FORMAT_Z16
@ RS2_FORMAT_Z16
Definition: rs_sensor.h:62
rs.hpp
f
GLdouble f
Definition: glad/glad/glad.h:1517
rs2
Definition: animated.h:9
w
GLdouble GLdouble GLdouble w
Definition: glad/glad/glad.h:1757
rs2::video_frame::get_width
int get_width() const
Definition: rs_frame.hpp:661
rs2::depth_frame
Definition: rs_frame.hpp:813
RS2_FORMAT_Y8
@ RS2_FORMAT_Y8
Definition: rs_sensor.h:70
rs2::video_frame
Definition: rs_frame.hpp:638
RS2_FORMAT_DISPARITY32
@ RS2_FORMAT_DISPARITY32
Definition: rs_sensor.h:80
RS2_FORMAT_BGR8
@ RS2_FORMAT_BGR8
Definition: rs_sensor.h:67
depth_frame_to_meters
static cv::Mat depth_frame_to_meters(const rs2::depth_frame &f)
Definition: cv-helpers.hpp:48


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Thu Dec 22 2022 03:13:15