From 09b3dcb6db8d7aa27f8e4aa7daec8fafeb6fab1f Mon Sep 17 00:00:00 2001 From: Tsukasa Sugiura Date: Thu, 6 Dec 2018 02:12:25 +0900 Subject: [PATCH] Merge pull request #13341 from UnaNancyOwen:fix_librealsense * videoio(librealsense): fix pipeline start with config fix to apply pipeline settings by passing config to start. * videoio(librealsense): add support get props add support get some props. --- modules/videoio/include/opencv2/videoio.hpp | 3 +- modules/videoio/src/cap_librealsense.cpp | 142 +++++++++++++++++++++++++++- modules/videoio/src/cap_librealsense.hpp | 5 + 3 files changed, 144 insertions(+), 6 deletions(-) diff --git a/modules/videoio/include/opencv2/videoio.hpp b/modules/videoio/include/opencv2/videoio.hpp index f96dcd7..408f9e8 100644 --- a/modules/videoio/include/opencv2/videoio.hpp +++ b/modules/videoio/include/opencv2/videoio.hpp @@ -526,7 +526,8 @@ enum { CAP_PROP_INTELPERC_PROFILE_COUNT = 11001, //! Intel Perceptual Streams enum { CAP_INTELPERC_DEPTH_GENERATOR = 1 << 29, CAP_INTELPERC_IMAGE_GENERATOR = 1 << 28, - CAP_INTELPERC_GENERATORS_MASK = CAP_INTELPERC_DEPTH_GENERATOR + CAP_INTELPERC_IMAGE_GENERATOR + CAP_INTELPERC_IR_GENERATOR = 1 << 27, + CAP_INTELPERC_GENERATORS_MASK = CAP_INTELPERC_DEPTH_GENERATOR + CAP_INTELPERC_IMAGE_GENERATOR + CAP_INTELPERC_IR_GENERATOR }; enum { CAP_INTELPERC_DEPTH_MAP = 0, //!< Each pixel is a 16-bit integer. The value indicates the distance from an object to the camera's XY plane or the Cartesian depth. diff --git a/modules/videoio/src/cap_librealsense.cpp b/modules/videoio/src/cap_librealsense.cpp index 4ef1c76..30fc66c 100644 --- a/modules/videoio/src/cap_librealsense.cpp +++ b/modules/videoio/src/cap_librealsense.cpp @@ -19,7 +19,7 @@ VideoCapture_LibRealsense::VideoCapture_LibRealsense(int) : mAlign(RS2_STREAM_CO config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16); config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8); config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8); - mPipe.start(); + mPipe.start(config); } catch (const rs2::error&) { @@ -27,15 +27,147 @@ VideoCapture_LibRealsense::VideoCapture_LibRealsense(int) : mAlign(RS2_STREAM_CO } VideoCapture_LibRealsense::~VideoCapture_LibRealsense(){} -double VideoCapture_LibRealsense::getProperty(int prop) const +double VideoCapture_LibRealsense::getProperty(int propIdx) const { - double propValue = 0; + double propValue = 0.0; - if (prop == CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE) - return mPipe.get_active_profile().get_device().first().get_depth_scale(); + const int purePropIdx = propIdx & ~CAP_INTELPERC_GENERATORS_MASK; + if((propIdx & CAP_INTELPERC_GENERATORS_MASK) == CAP_INTELPERC_IMAGE_GENERATOR) + { + propValue = getImageGeneratorProperty(purePropIdx); + } + else if((propIdx & CAP_INTELPERC_GENERATORS_MASK) == CAP_INTELPERC_DEPTH_GENERATOR) + { + propValue = getDepthGeneratorProperty(purePropIdx); + } + else if((propIdx & CAP_INTELPERC_GENERATORS_MASK) == CAP_INTELPERC_IR_GENERATOR) + { + propValue = getIrGeneratorProperty(purePropIdx); + } + else + { + propValue = getCommonProperty(purePropIdx); + } return propValue; } + +double VideoCapture_LibRealsense::getImageGeneratorProperty(int propIdx) const +{ + double propValue = 0.0; + const rs2::video_stream_profile profile = mPipe.get_active_profile().get_stream(RS2_STREAM_COLOR).as(); + if(!profile) + { + return propValue; + } + + switch(propIdx) + { + case CAP_PROP_FRAME_WIDTH: + propValue = static_cast(profile.width()); + break; + case CAP_PROP_FRAME_HEIGHT: + propValue = static_cast(profile.height()); + break; + case CAP_PROP_FPS: + propValue = static_cast(profile.fps()); + break; + } + + return propValue; +} + +double VideoCapture_LibRealsense::getDepthGeneratorProperty(int propIdx) const +{ + double propValue = 0.0; + const rs2::video_stream_profile profile = mPipe.get_active_profile().get_stream(RS2_STREAM_DEPTH).as(); + const rs2::depth_sensor sensor = mPipe.get_active_profile().get_device().first(); + if(!profile || !sensor) + { + return propValue; + } + + switch(propIdx) + { + case CAP_PROP_FRAME_WIDTH: + propValue = static_cast(profile.width()); + break; + case CAP_PROP_FRAME_HEIGHT: + propValue = static_cast(profile.height()); + break; + case CAP_PROP_FPS: + propValue = static_cast(profile.fps()); + break; + case CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE: + propValue = static_cast(sensor.get_depth_scale()); + break; + case CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_HORZ: + propValue = static_cast(profile.get_intrinsics().fx); + break; + case CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_VERT: + propValue = static_cast(profile.get_intrinsics().fy); + break; + } + + return propValue; +} + +double VideoCapture_LibRealsense::getIrGeneratorProperty(int propIdx) const +{ + double propValue = 0.0; + const rs2::video_stream_profile profile = mPipe.get_active_profile().get_stream(RS2_STREAM_INFRARED).as(); + if(!profile) + { + return propValue; + } + + switch(propIdx) + { + case CAP_PROP_FRAME_WIDTH: + propValue = static_cast(profile.width()); + break; + case CAP_PROP_FRAME_HEIGHT: + propValue = static_cast(profile.height()); + break; + case CAP_PROP_FPS: + propValue = static_cast(profile.fps()); + break; + } + + return propValue; +} + +double VideoCapture_LibRealsense::getCommonProperty(int propIdx) const +{ + double propValue = 0.0; + const rs2::video_stream_profile profile = mPipe.get_active_profile().get_stream(RS2_STREAM_DEPTH).as(); + const rs2::depth_sensor sensor = mPipe.get_active_profile().get_device().first(); + if(!profile || !sensor) + { + return propValue; + } + + switch(propIdx) + { + case CAP_PROP_FRAME_WIDTH: + case CAP_PROP_FRAME_HEIGHT: + case CAP_PROP_FPS: + propValue = getDepthGeneratorProperty(propIdx); + break; + case CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE: + propValue = static_cast(sensor.get_depth_scale()); + break; + case CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_HORZ: + propValue = static_cast(profile.get_intrinsics().fx); + break; + case CAP_PROP_INTELPERC_DEPTH_FOCAL_LENGTH_VERT: + propValue = static_cast(profile.get_intrinsics().fy); + break; + } + + return propValue; +} + bool VideoCapture_LibRealsense::setProperty(int, double) { bool isSet = false; diff --git a/modules/videoio/src/cap_librealsense.hpp b/modules/videoio/src/cap_librealsense.hpp index 81d17e2..1f59668 100644 --- a/modules/videoio/src/cap_librealsense.hpp +++ b/modules/videoio/src/cap_librealsense.hpp @@ -29,6 +29,11 @@ protected: rs2::pipeline mPipe; rs2::frameset mData; rs2::align mAlign; + + double getDepthGeneratorProperty(int propIdx) const; + double getImageGeneratorProperty(int propIdx) const; + double getIrGeneratorProperty(int propIdx) const; + double getCommonProperty(int propIdx) const; }; } -- 2.7.4