// License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2017 Intel Corporation. All Rights Reserved. #pragma once #include // Include RealSense Cross Platform API #include // Include OpenCV API #include // Convert rs2::frame to cv::Mat static cv::Mat frame_to_mat(const rs2::frame& f) { using namespace cv; using namespace rs2; auto vf = f.as(); 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_rgb = Mat(Size(w, h), CV_8UC3, (void*)f.get_data(), Mat::AUTO_STEP); Mat r_bgr; cvtColor(r_rgb, r_bgr, COLOR_RGB2BGR); return r_bgr; } 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); } else if (f.get_profile().format() == RS2_FORMAT_DISPARITY32) { return Mat(Size(w, h), CV_32FC1, (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 static cv::Mat depth_frame_to_meters( const rs2::depth_frame & f ) { cv::Mat dm = frame_to_mat(f); dm.convertTo( dm, CV_64F ); dm = dm * f.get_units(); return dm; }