-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvhelpers.hpp
56 lines (48 loc) · 1.56 KB
/
cvhelpers.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef CVHELPERS_HPP
#define CVHELPERS_HPP
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include "opencv.hpp" // Include OpenCV API
#include <exception>
// Convert rs2::frame to cv::Mat
cv::Mat frame_to_mat(const rs2::frame& f)
{
using namespace cv;
using namespace rs2;
auto vf = f.as<video_frame>();
const int w = vf.get_width();
const int h = vf.get_height();
if (f.get_profile().format() == RS2_FORMAT_BGR8)
{
return Mat(Size(w, h), CV_8UC3, (void*)f.get_data(), Mat::AUTO_STEP);
}
else if (f.get_profile().format() == RS2_FORMAT_RGB8)
{
auto r = Mat(Size(w, h), CV_8UC3, (void*)f.get_data(), Mat::AUTO_STEP);
cv::cvtColor(r, r, CV_BGR2RGB);
return r;
}
else if (f.get_profile().format() == RS2_FORMAT_Z16)
{
return Mat(Size(w, h), CV_16UC1, (void*)f.get_data(), Mat::AUTO_STEP);
}
else if (f.get_profile().format() == RS2_FORMAT_Y8)
{
return Mat(Size(w, h), CV_8UC1, (void*)f.get_data(), Mat::AUTO_STEP);;
}
throw std::runtime_error("Frame format is not supported yet!");
}
// Converts depth frame to a matrix of doubles with distances in meters
cv::Mat depth_frame_to_meters(const rs2::pipeline& pipe, const rs2::depth_frame& f)
{
using namespace cv;
using namespace rs2;
Mat dm = frame_to_mat(f);
dm.convertTo(dm, CV_64F);
auto depth_scale = pipe.get_active_profile()
.get_device()
.first<depth_sensor>()
.get_depth_scale();
dm = dm * depth_scale;
return dm;
}
#endif // CVHELPERS_HPP