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 
stream_profile get_profile() const
Definition: rs_frame.hpp:557
float get_units() const
Definition: rs_frame.hpp:845
const void * get_data() const
Definition: rs_frame.hpp:545
Definition: cah-model.h:10
GLdouble GLdouble GLdouble w
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
static cv::Mat depth_frame_to_meters(const rs2::depth_frame &f)
Definition: cv-helpers.hpp:48
GLdouble f
rs2_format format() const
Definition: rs_frame.hpp:44
static cv::Mat frame_to_mat(const rs2::frame &f)
Definition: cv-helpers.hpp:11
int get_width() const
Definition: rs_frame.hpp:659
T as() const
Definition: rs_frame.hpp:580


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:12