diff --git a/include/librealsense2/h/rs_frame.h b/include/librealsense2/h/rs_frame.h index 35a36db2ab..f507362796 100644 --- a/include/librealsense2/h/rs_frame.h +++ b/include/librealsense2/h/rs_frame.h @@ -150,6 +150,12 @@ int rs2_get_frame_width(const rs2_frame* frame, rs2_error** error); */ int rs2_get_frame_height(const rs2_frame* frame, rs2_error** error); +/** +* retrieve the scaling factor to use when converting a depth frame's get_data() units to meters +* \return float - depth, in meters, per 1 unit stored in the frame data +*/ +float rs2_depth_frame_get_units( const rs2_frame* frame, rs2_error** error ); + /** * retrieve frame stride in bytes (number of bytes from start of line N to start of line N+1) * \param[in] frame handle returned from a callback diff --git a/include/librealsense2/hpp/rs_frame.hpp b/include/librealsense2/hpp/rs_frame.hpp index 0ecf670af9..5bceab2a45 100644 --- a/include/librealsense2/hpp/rs_frame.hpp +++ b/include/librealsense2/hpp/rs_frame.hpp @@ -814,6 +814,18 @@ namespace rs2 error::handle(e); return r; } + + /** + * Provide the scaling factor to use when converting from get_data() units to meters + * \return float - depth, in meters, per 1 unit stored in the frame data + */ + float get_units() const + { + rs2_error * e = nullptr; + auto r = rs2_depth_frame_get_units( get(), &e ); + error::handle( e ); + return r; + } }; class disparity_frame : public depth_frame diff --git a/src/android/jni/frame.cpp b/src/android/jni/frame.cpp index 558420b7aa..fb8f059733 100644 --- a/src/android/jni/frame.cpp +++ b/src/android/jni/frame.cpp @@ -130,6 +130,14 @@ Java_com_intel_realsense_librealsense_DepthFrame_nGetDistance(JNIEnv *env, jclas return rv; } +extern "C" JNIEXPORT jfloat JNICALL +Java_com_intel_realsense_librealsense_DepthFrame_nGetUnits( JNIEnv *env, jclass type, jlong handle ) { + rs2_error *e = NULL; + float rv = rs2_depth_frame_get_units( reinterpret_cast(handle), &e ); + handle_error( env, e ); + return rv; +} + extern "C" JNIEXPORT jint JNICALL Java_com_intel_realsense_librealsense_Points_nGetCount(JNIEnv *env, jclass type, jlong handle) { rs2_error *e = NULL; diff --git a/src/realsense.def b/src/realsense.def index a386fa168c..166e289d9d 100644 --- a/src/realsense.def +++ b/src/realsense.def @@ -195,6 +195,7 @@ EXPORTS rs2_embedded_frames_count rs2_extract_frame rs2_depth_frame_get_distance + rs2_depth_frame_get_units rs2_depth_stereo_frame_get_baseline rs2_get_stereo_baseline diff --git a/src/rs.cpp b/src/rs.cpp index 0a027c5956..8213a859db 100644 --- a/src/rs.cpp +++ b/src/rs.cpp @@ -2160,6 +2160,14 @@ float rs2_depth_frame_get_distance(const rs2_frame* frame_ref, int x, int y, rs2 } HANDLE_EXCEPTIONS_AND_RETURN(0, frame_ref, x, y) +float rs2_depth_frame_get_units( const rs2_frame* frame_ref, rs2_error** error ) BEGIN_API_CALL +{ + VALIDATE_NOT_NULL( frame_ref ); + auto df = VALIDATE_INTERFACE((( frame_interface * ) frame_ref ), librealsense::depth_frame ); + return df->get_units(); +} +HANDLE_EXCEPTIONS_AND_RETURN( 0, frame_ref ) + float rs2_depth_stereo_frame_get_baseline(const rs2_frame* frame_ref, rs2_error** error) BEGIN_API_CALL { VALIDATE_NOT_NULL(frame_ref); diff --git a/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/DepthFrame.java b/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/DepthFrame.java index 527c649cab..081c161ebe 100644 --- a/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/DepthFrame.java +++ b/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/DepthFrame.java @@ -10,5 +10,10 @@ public float getDistance(int x, int y) { return nGetDistance(mHandle, x, y); } + public float getUnits() { + return nGetUnits(mHandle); + } + private static native float nGetDistance(long handle, int x, int y); + private static native float nGetUnits(long handle); } diff --git a/wrappers/csharp/Intel.RealSense/Frames/DepthFrame.cs b/wrappers/csharp/Intel.RealSense/Frames/DepthFrame.cs index 15d3837095..97fa7c2426 100644 --- a/wrappers/csharp/Intel.RealSense/Frames/DepthFrame.cs +++ b/wrappers/csharp/Intel.RealSense/Frames/DepthFrame.cs @@ -21,5 +21,12 @@ public float GetDistance(int x, int y) object error; return NativeMethods.rs2_depth_frame_get_distance(Handle, x, y, out error); } + /// Provide the scaling factor to use when converting from frame data units to meters + /// Depth, in meters, per 1 unit stored in the frame data + public float GetUnits() + { + object error; + return NativeMethods.rs2_depth_frame_get_units(Handle, out error); + } } } diff --git a/wrappers/csharp/Intel.RealSense/NativeMethods.cs b/wrappers/csharp/Intel.RealSense/NativeMethods.cs index 1e5864c6cb..e78885a765 100644 --- a/wrappers/csharp/Intel.RealSense/NativeMethods.cs +++ b/wrappers/csharp/Intel.RealSense/NativeMethods.cs @@ -568,6 +568,9 @@ internal static MemCpyDelegate GetMethod() [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)] internal static extern float rs2_depth_frame_get_distance(IntPtr frame_ref, int x, int y, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ErrorMarshaler))] out object error); + [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)] + internal static extern float rs2_depth_frame_get_units(IntPtr frame_ref, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ErrorMarshaler))] out object error); + [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)] internal static extern float rs2_depth_stereo_frame_get_baseline(IntPtr frame_ref, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ErrorMarshaler))] out object error); diff --git a/wrappers/opencv/cv-helpers.hpp b/wrappers/opencv/cv-helpers.hpp index 0a06259e7d..93a809b429 100644 --- a/wrappers/opencv/cv-helpers.hpp +++ b/wrappers/opencv/cv-helpers.hpp @@ -45,18 +45,11 @@ cv::Mat frame_to_mat(const rs2::frame& f) } // Converts depth frame to a matrix of doubles with distances in meters -cv::Mat depth_frame_to_meters(const rs2::pipeline& pipe, const rs2::depth_frame& f) +cv::Mat depth_frame_to_meters( const rs2::depth_frame & f ) { - using namespace cv; - using namespace rs2; - - Mat dm = frame_to_mat(f); - dm.convertTo(dm, CV_64F); - auto depth_scale = pipe.get_active_profile() - .get_device() - .first() - .get_depth_scale(); - dm = dm * depth_scale; + cv::Mat dm = frame_to_mat(f); + dm.convertTo( dm, CV_64F ); + dm = dm * f.get_units(); return dm; } diff --git a/wrappers/opencv/dnn/rs-dnn.cpp b/wrappers/opencv/dnn/rs-dnn.cpp index 0192939ad9..e9cdd76341 100644 --- a/wrappers/opencv/dnn/rs-dnn.cpp +++ b/wrappers/opencv/dnn/rs-dnn.cpp @@ -71,7 +71,7 @@ int main(int argc, char** argv) try // Convert RealSense frame to OpenCV matrix: auto color_mat = frame_to_mat(color_frame); - auto depth_mat = depth_frame_to_meters(pipe, depth_frame); + auto depth_mat = depth_frame_to_meters(depth_frame); Mat inputBlob = blobFromImage(color_mat, inScaleFactor, Size(inWidth, inHeight), meanVal, false); //Convert Mat to batch of images diff --git a/wrappers/python/pyrs_frame.cpp b/wrappers/python/pyrs_frame.cpp index 1021c4988f..7be2c171fa 100644 --- a/wrappers/python/pyrs_frame.cpp +++ b/wrappers/python/pyrs_frame.cpp @@ -207,7 +207,8 @@ void init_frame(py::module &m) { py::class_ depth_frame(m, "depth_frame", "Extends the video_frame class with additional depth related attributes and functions."); depth_frame.def(py::init()) - .def("get_distance", &rs2::depth_frame::get_distance, "x"_a, "y"_a, "Provide the depth in meters at the given pixel"); + .def("get_distance", &rs2::depth_frame::get_distance, "x"_a, "y"_a, "Provide the depth in meters at the given pixel") + .def("get_units", &rs2::depth_frame::get_units, "Provide the scaling factor to use when converting from get_data() units to meters"); // rs2::disparity_frame py::class_ disparity_frame(m, "disparity_frame", "Extends the depth_frame class with additional disparity related attributes and functions.");