Skip to content

Commit d1a598c

Browse files
committedJun 8, 2018
Merge pull request opencv#11730 from paroj:realsensev2
2 parents 45dd575 + bfc227b commit d1a598c

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed
 

‎cmake/OpenCVFindLibRealsense.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# LIBREALSENSE_LIBRARIES and LIBREALSENSE_INCLUDE to link Intel librealsense modules
33
# HAVE_LIBREALSENSE for conditional compilation OpenCV with/without librealsense
44

5-
find_path(LIBREALSENSE_INCLUDE_DIR "librealsense/rs.hpp" PATHS "$ENV{LIBREALSENSE_INCLUDE}" DOC "Path to librealsense interface headers")
6-
find_library(LIBREALSENSE_LIBRARIES "realsense" PATHS "$ENV{LIBREALSENSE_LIB}" DOC "Path to librealsense interface libraries")
5+
find_path(LIBREALSENSE_INCLUDE_DIR "librealsense2/rs.hpp" PATHS "$ENV{LIBREALSENSE_INCLUDE}" DOC "Path to librealsense interface headers")
6+
find_library(LIBREALSENSE_LIBRARIES "realsense2" PATHS "$ENV{LIBREALSENSE_LIB}" DOC "Path to librealsense interface libraries")
77

88
if(LIBREALSENSE_INCLUDE_DIR AND LIBREALSENSE_LIBRARIES)
99
set(HAVE_LIBREALSENSE TRUE)

‎modules/videoio/src/cap_librealsense.cpp

+20-21
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@
1010
namespace cv
1111
{
1212

13-
VideoCapture_LibRealsense::VideoCapture_LibRealsense(int index)
13+
VideoCapture_LibRealsense::VideoCapture_LibRealsense(int) : mAlign(RS2_STREAM_COLOR)
1414
{
1515
try
1616
{
17-
mDev = mContext.get_device(index);
18-
// Configure all streams to run at VGA resolution at 60 frames per second
19-
mDev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
20-
mDev->enable_stream(rs::stream::color, 640, 480, rs::format::bgr8, 60);
21-
mDev->enable_stream(rs::stream::infrared, 640, 480, rs::format::y8, 60);
22-
mDev->start();
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();
2323
}
24-
catch (rs::error&)
24+
catch (const rs2::error&)
2525
{
26-
mDev = nullptr;
2726
}
2827
}
2928
VideoCapture_LibRealsense::~VideoCapture_LibRealsense(){}
@@ -32,8 +31,8 @@ double VideoCapture_LibRealsense::getProperty(int prop) const
3231
{
3332
double propValue = 0;
3433

35-
if(prop == CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE)
36-
return mDev->get_depth_scale();
34+
if (prop == CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE)
35+
return mPipe.get_active_profile().get_device().first<rs2::depth_sensor>().get_depth_scale();
3736

3837
return propValue;
3938
}
@@ -50,9 +49,9 @@ bool VideoCapture_LibRealsense::grabFrame()
5049

5150
try
5251
{
53-
mDev->wait_for_frames();
52+
mData = mAlign.process(mPipe.wait_for_frames());
5453
}
55-
catch (rs::error&)
54+
catch (const rs2::error&)
5655
{
5756
return false;
5857
}
@@ -61,20 +60,20 @@ bool VideoCapture_LibRealsense::grabFrame()
6160
}
6261
bool VideoCapture_LibRealsense::retrieveFrame(int outputType, cv::OutputArray frame)
6362
{
64-
rs::stream stream;
63+
rs2::video_frame _frame(nullptr);
6564
int type;
6665
switch (outputType)
6766
{
6867
case CAP_INTELPERC_DEPTH_MAP:
69-
stream = rs::stream::depth_aligned_to_color;
68+
_frame = mData.get_depth_frame().as<rs2::video_frame>();
7069
type = CV_16UC1;
7170
break;
7271
case CAP_INTELPERC_IR_MAP:
73-
stream = rs::stream::infrared;
72+
_frame = mData.get_infrared_frame();
7473
type = CV_8UC1;
7574
break;
7675
case CAP_INTELPERC_IMAGE:
77-
stream = rs::stream::color;
76+
_frame = mData.get_color_frame();
7877
type = CV_8UC3;
7978
break;
8079
default:
@@ -84,10 +83,10 @@ bool VideoCapture_LibRealsense::retrieveFrame(int outputType, cv::OutputArray fr
8483
try
8584
{
8685
// we copy the data straight away, so const_cast should be fine
87-
void* data = const_cast<void*>(mDev->get_frame_data(stream));
88-
Mat(mDev->get_stream_height(stream), mDev->get_stream_width(stream), type, data).copyTo(frame);
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);
8988
}
90-
catch (rs::error&)
89+
catch (const rs2::error&)
9190
{
9291
return false;
9392
}
@@ -101,7 +100,7 @@ int VideoCapture_LibRealsense::getCaptureDomain()
101100

102101
bool VideoCapture_LibRealsense::isOpened() const
103102
{
104-
return mDev && mDev->is_streaming();
103+
return bool(std::shared_ptr<rs2_pipeline>(mPipe));
105104
}
106105

107106
}

‎modules/videoio/src/cap_librealsense.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#ifdef HAVE_LIBREALSENSE
99

10-
#include <librealsense/rs.hpp>
10+
#include <librealsense2/rs.hpp>
1111

1212
namespace cv
1313
{
@@ -26,8 +26,9 @@ class VideoCapture_LibRealsense : public IVideoCapture
2626
virtual int getCaptureDomain() CV_OVERRIDE;
2727
virtual bool isOpened() const CV_OVERRIDE;
2828
protected:
29-
rs::context mContext;
30-
rs::device* mDev = nullptr;
29+
rs2::pipeline mPipe;
30+
rs2::frameset mData;
31+
rs2::align mAlign;
3132
};
3233

3334
}

0 commit comments

Comments
 (0)
Please sign in to comment.