Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/src/cap_librealsense.cpp
16354 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
5
#include "precomp.hpp"
6
7
#ifdef HAVE_LIBREALSENSE
8
#include "cap_librealsense.hpp"
9
10
namespace cv
11
{
12
13
VideoCapture_LibRealsense::VideoCapture_LibRealsense(int) : mAlign(RS2_STREAM_COLOR)
14
{
15
try
16
{
17
rs2::config config;
18
// Configure all streams to run at VGA resolution at default fps
19
config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16);
20
config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8);
21
config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8);
22
mPipe.start();
23
}
24
catch (const rs2::error&)
25
{
26
}
27
}
28
VideoCapture_LibRealsense::~VideoCapture_LibRealsense(){}
29
30
double VideoCapture_LibRealsense::getProperty(int prop) const
31
{
32
double propValue = 0;
33
34
if (prop == CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE)
35
return mPipe.get_active_profile().get_device().first<rs2::depth_sensor>().get_depth_scale();
36
37
return propValue;
38
}
39
bool VideoCapture_LibRealsense::setProperty(int, double)
40
{
41
bool isSet = false;
42
return isSet;
43
}
44
45
bool VideoCapture_LibRealsense::grabFrame()
46
{
47
if (!isOpened())
48
return false;
49
50
try
51
{
52
mData = mAlign.process(mPipe.wait_for_frames());
53
}
54
catch (const rs2::error&)
55
{
56
return false;
57
}
58
59
return true;
60
}
61
bool VideoCapture_LibRealsense::retrieveFrame(int outputType, cv::OutputArray frame)
62
{
63
rs2::video_frame _frame(nullptr);
64
int type;
65
switch (outputType)
66
{
67
case CAP_INTELPERC_DEPTH_MAP:
68
_frame = mData.get_depth_frame().as<rs2::video_frame>();
69
type = CV_16UC1;
70
break;
71
case CAP_INTELPERC_IR_MAP:
72
_frame = mData.get_infrared_frame();
73
type = CV_8UC1;
74
break;
75
case CAP_INTELPERC_IMAGE:
76
_frame = mData.get_color_frame();
77
type = CV_8UC3;
78
break;
79
default:
80
return false;
81
}
82
83
try
84
{
85
// we copy the data straight away, so const_cast should be fine
86
void* data = const_cast<void*>(_frame.get_data());
87
Mat(_frame.get_height(), _frame.get_width(), type, data, _frame.get_stride_in_bytes()).copyTo(frame);
88
89
if(_frame.get_profile().format() == RS2_FORMAT_RGB8)
90
cvtColor(frame, frame, COLOR_RGB2BGR);
91
}
92
catch (const rs2::error&)
93
{
94
return false;
95
}
96
97
return true;
98
}
99
int VideoCapture_LibRealsense::getCaptureDomain()
100
{
101
return CAP_INTELPERC;
102
}
103
104
bool VideoCapture_LibRealsense::isOpened() const
105
{
106
return bool(std::shared_ptr<rs2_pipeline>(mPipe));
107
}
108
109
}
110
111
#endif
112
113