Path: blob/master/modules/videoio/src/cap_librealsense.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34#include "precomp.hpp"56#ifdef HAVE_LIBREALSENSE7#include "cap_librealsense.hpp"89namespace cv10{1112VideoCapture_LibRealsense::VideoCapture_LibRealsense(int) : mAlign(RS2_STREAM_COLOR)13{14try15{16rs2::config config;17// Configure all streams to run at VGA resolution at default fps18config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16);19config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8);20config.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8);21mPipe.start();22}23catch (const rs2::error&)24{25}26}27VideoCapture_LibRealsense::~VideoCapture_LibRealsense(){}2829double VideoCapture_LibRealsense::getProperty(int prop) const30{31double propValue = 0;3233if (prop == CAP_PROP_INTELPERC_DEPTH_SATURATION_VALUE)34return mPipe.get_active_profile().get_device().first<rs2::depth_sensor>().get_depth_scale();3536return propValue;37}38bool VideoCapture_LibRealsense::setProperty(int, double)39{40bool isSet = false;41return isSet;42}4344bool VideoCapture_LibRealsense::grabFrame()45{46if (!isOpened())47return false;4849try50{51mData = mAlign.process(mPipe.wait_for_frames());52}53catch (const rs2::error&)54{55return false;56}5758return true;59}60bool VideoCapture_LibRealsense::retrieveFrame(int outputType, cv::OutputArray frame)61{62rs2::video_frame _frame(nullptr);63int type;64switch (outputType)65{66case CAP_INTELPERC_DEPTH_MAP:67_frame = mData.get_depth_frame().as<rs2::video_frame>();68type = CV_16UC1;69break;70case CAP_INTELPERC_IR_MAP:71_frame = mData.get_infrared_frame();72type = CV_8UC1;73break;74case CAP_INTELPERC_IMAGE:75_frame = mData.get_color_frame();76type = CV_8UC3;77break;78default:79return false;80}8182try83{84// we copy the data straight away, so const_cast should be fine85void* data = const_cast<void*>(_frame.get_data());86Mat(_frame.get_height(), _frame.get_width(), type, data, _frame.get_stride_in_bytes()).copyTo(frame);8788if(_frame.get_profile().format() == RS2_FORMAT_RGB8)89cvtColor(frame, frame, COLOR_RGB2BGR);90}91catch (const rs2::error&)92{93return false;94}9596return true;97}98int VideoCapture_LibRealsense::getCaptureDomain()99{100return CAP_INTELPERC;101}102103bool VideoCapture_LibRealsense::isOpened() const104{105return bool(std::shared_ptr<rs2_pipeline>(mPipe));106}107108}109110#endif111112113