The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

t/fm: Add simple FrameMat that wraps a cv::Mat

authored by

Jakob Bornecrantz and committed by
Moses Turner
b7b46908 6ba07514

+192
+2
doc/changes/auxiliary/mr.825.md
··· 1 + t/fm: Add simple FrameMat that wraps a cv::Mat, this allows us to easily pass 2 + cv::Mat's around without the C code needing to know about OpenCV.
+2
src/xrt/auxiliary/CMakeLists.txt
··· 97 97 tracking/t_debug_hsv_picker.cpp 98 98 tracking/t_debug_hsv_viewer.cpp 99 99 tracking/t_file.cpp 100 + tracking/t_frame_cv_mat_wrapper.cpp 101 + tracking/t_frame_cv_mat_wrapper.hpp 100 102 tracking/t_fusion.hpp 101 103 tracking/t_helper_debug_sink.hpp 102 104 tracking/t_hsv_filter.c
+2
src/xrt/auxiliary/meson.build
··· 205 205 'tracking/t_debug_hsv_picker.cpp', 206 206 'tracking/t_debug_hsv_viewer.cpp', 207 207 'tracking/t_file.cpp', 208 + 'tracking/t_frame_cv_mat_wrapper.cpp', 209 + 'tracking/t_frame_cv_mat_wrapper.hpp', 208 210 'tracking/t_fusion.hpp', 209 211 'tracking/t_helper_debug_sink.hpp', 210 212 'tracking/t_hsv_filter.c',
+116
src/xrt/auxiliary/tracking/t_frame_cv_mat_wrapper.cpp
··· 1 + // Copyright 2021, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Simple @ref xrt_frame wrapper around a @ref cv::Mat. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_tracking 8 + */ 9 + 10 + #include "util/u_format.h" 11 + 12 + #include "tracking/t_frame_cv_mat_wrapper.hpp" 13 + 14 + 15 + namespace xrt::auxiliary::tracking { 16 + 17 + 18 + /* 19 + * 20 + * C functions. 21 + * 22 + */ 23 + 24 + extern "C" void 25 + frame_mat_destroy(struct xrt_frame *xf) 26 + { 27 + FrameMat *fm = (FrameMat *)xf; 28 + delete fm; 29 + } 30 + 31 + 32 + /* 33 + * 34 + * Member functions 35 + * 36 + */ 37 + 38 + void 39 + FrameMat::fillInFields(cv::Mat mat, xrt_format format, const Params &params) 40 + { 41 + uint32_t width = (uint32_t)mat.cols; 42 + uint32_t height = (uint32_t)mat.rows; 43 + size_t stride = mat.step[0]; 44 + size_t size = stride * height; 45 + 46 + this->matrix = mat; 47 + 48 + // Main wrapping of cv::Mat by frame. 49 + xrt_frame &frame = this->frame; 50 + frame.reference.count = 1; 51 + frame.destroy = frame_mat_destroy; 52 + frame.data = mat.ptr<uint8_t>(); 53 + frame.format = format; 54 + frame.width = width; 55 + frame.height = height; 56 + frame.stride = stride; 57 + frame.size = size; 58 + 59 + // Params 60 + frame.timestamp = params.timestamp_ns; 61 + frame.stereo_format = params.stereo_format; 62 + } 63 + 64 + FrameMat::~FrameMat() 65 + { 66 + // Noop 67 + } 68 + 69 + FrameMat::FrameMat() 70 + { 71 + // Noop 72 + } 73 + 74 + 75 + /* 76 + * 77 + * Static functions. 78 + * 79 + */ 80 + 81 + void 82 + FrameMat::wrapR8G8B8(cv::Mat mat, xrt_frame **fm_out, const Params /*&&?*/ params) 83 + { 84 + assert(mat.channels() == 3); 85 + assert(mat.type() == CV_8UC3); 86 + 87 + 88 + FrameMat *fm = new FrameMat(); 89 + fm->fillInFields(mat, XRT_FORMAT_R8G8B8, params); 90 + 91 + // Unreference any old frames. 92 + xrt_frame_reference(fm_out, NULL); 93 + 94 + // Already has a ref count of one. 95 + *fm_out = &fm->frame; 96 + } 97 + 98 + void 99 + FrameMat::wrapL8(cv::Mat mat, xrt_frame **fm_out, const Params /*&&?*/ params) 100 + { 101 + assert(mat.channels() == 1); 102 + assert(mat.type() == CV_8UC1); 103 + 104 + 105 + FrameMat *fm = new FrameMat(); 106 + fm->fillInFields(mat, XRT_FORMAT_L8, params); 107 + 108 + // Unreference any old frames. 109 + xrt_frame_reference(fm_out, NULL); 110 + 111 + // Already has a ref count of one. 112 + *fm_out = &fm->frame; 113 + } 114 + 115 + 116 + } // namespace xrt::auxiliary::tracking
+70
src/xrt/auxiliary/tracking/t_frame_cv_mat_wrapper.hpp
··· 1 + // Copyright 2021, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Simple @ref xrt_frame wrapper around a @ref cv::Mat. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_tracking 8 + */ 9 + 10 + #include "xrt/xrt_frame.h" 11 + 12 + #include <opencv2/opencv.hpp> 13 + 14 + 15 + namespace xrt::auxiliary::tracking { 16 + 17 + 18 + class FrameMat 19 + { 20 + public: 21 + /*! 22 + * Additional optional parameters for frame creation. 23 + */ 24 + class Params 25 + { 26 + public: 27 + enum xrt_stereo_format stereo_format; 28 + uint64_t timestamp_ns; 29 + }; 30 + 31 + 32 + public: 33 + // Exposed to the C api. 34 + struct xrt_frame frame = {}; 35 + 36 + // The @ref cv::Mat that holds the data. 37 + cv::Mat matrix = cv::Mat(); 38 + 39 + 40 + public: 41 + /*! 42 + * Only public due to C needed to destroy it. 43 + */ 44 + ~FrameMat(); 45 + 46 + /*! 47 + * Wraps the given @ref cv::Mat assuming it's a 24bit RGB format matrix, the pointer pointed to by @ref xf_ptr 48 + * will have it's reference updated. 49 + */ 50 + static void 51 + wrapR8G8B8(cv::Mat mat, xrt_frame **xf_ptr, const Params /*&&?*/ params = {}); 52 + 53 + /*! 54 + * Wraps the given @ref cv::Mat assuming it's a 8bit format matrix, the pointer pointed to by @ref xf_ptr will 55 + * have it's reference updated. 56 + */ 57 + static void 58 + wrapL8(cv::Mat mat, xrt_frame **xf_ptr, const Params /*&&?*/ params = {}); 59 + 60 + 61 + private: 62 + FrameMat(); 63 + 64 + void 65 + fillInFields(cv::Mat mat, xrt_format format, const Params &params); 66 + }; 67 + 68 + 69 + 70 + } // namespace xrt::auxiliary::tracking