NDEVR
API Documentation
OpenCVHelperFunctions.h
1#pragma once
2#include "Base/Headers/Matrix.hpp"
3#include <Eigen/src/Core/Matrix.h>
4#include <opencv2/core/mat.hpp>
5#include <librealsense2/rs.hpp>
6
7namespace NDEVR
8{
13static inline Matrix<fltp04> Convert(cv::Mat mat)
14{
15 if (!mat.empty())
16 {
18 for (uint01 i = 0; i < 4; i++)
19 {
20 for (uint01 x = 0; x < 4; x++)
21 {
22 transform[i][x] = mat.at<fltp04>(x, i);
23 }
24 }
25 return transform;
26 }
27 else
28 {
29 return Constant<Matrix<fltp04>>::Invalid;
30 }
31}
32
36static inline cv::Mat Convert(const Matrix<fltp04>& transform)
37{
39 return cv::Mat();
40 cv::Mat mat(4, 4, CV_32F);
41 for (uint01 i = 0; i < 4; i++)
42 {
43 for (uint01 x = 0; x < 4; x++)
44 {
45 mat.at<fltp04>(x, i) = transform[i][x];
46 }
47 }
48 return mat;
49}
50
55static cv::Mat ConvertImgToColor(rs2::video_frame a_frame)
56{
57 cv::Mat im_RGB;
58 cv::Size size(a_frame.get_width(), a_frame.get_height());
59 rs2_format format = a_frame.get_profile().format();
60 switch (format)
61 {
62 case rs2_format::RS2_FORMAT_YUYV:
63 im_RGB = cv::Mat(size, CV_8UC2, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
64 cvtColor(im_RGB, im_RGB, cv::COLOR_YUV2RGB_YUY2);
65 break;
66 case rs2_format::RS2_FORMAT_RGB8:
67 im_RGB = cv::Mat(size, CV_8UC3, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
68 break;
69 case rs2_format::RS2_FORMAT_RGBA8:
70 im_RGB = cv::Mat(size, CV_8UC4, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
71 cvtColor(im_RGB, im_RGB, cv::COLOR_RGBA2RGB);
72 break;
73 case rs2_format::RS2_FORMAT_BGR8:
74 im_RGB = cv::Mat(size, CV_8UC3, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
75 cvtColor(im_RGB, im_RGB, cv::COLOR_BGR2RGB);
76 break;
77 case rs2_format::RS2_FORMAT_BGRA8:
78 im_RGB = cv::Mat(size, CV_8UC4, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
79 cvtColor(im_RGB, im_RGB, cv::COLOR_BGRA2RGB);
80 break;
81 case rs2_format::RS2_FORMAT_Y8:
82 im_RGB = cv::Mat(size, CV_8UC1, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
83 break;
84 case rs2_format::RS2_FORMAT_Y16:
85 case rs2_format::RS2_FORMAT_Z16:
86 im_RGB = cv::Mat(size, CV_16UC1, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
87 break;
88 default:
89 lib_assert(false, "unknown format");
90 }
91 return im_RGB;
92}
93
98static cv::Mat ConvertImgToGray(rs2::video_frame a_frame)
99{
100 cv::Mat im_RGB;
101 cv::Size size(a_frame.get_width(), a_frame.get_height());
102 switch (a_frame.get_profile().format())
103 {
104 case rs2_format::RS2_FORMAT_YUYV:
105 im_RGB = cv::Mat(size, CV_8UC2, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
106 cvtColor(im_RGB, im_RGB, cv::COLOR_YUV2GRAY_YUY2);
107 break;
108 case rs2_format::RS2_FORMAT_BGR8:
109 im_RGB = cv::Mat(size, CV_8UC3, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
110 cvtColor(im_RGB, im_RGB, cv::COLOR_BGR2GRAY);
111 break;
112 case rs2_format::RS2_FORMAT_BGRA8:
113 im_RGB = cv::Mat(size, CV_8UC4, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
114 cvtColor(im_RGB, im_RGB, cv::COLOR_BGRA2GRAY);
115 break;
116 case rs2_format::RS2_FORMAT_RGB8:
117 im_RGB = cv::Mat(size, CV_8UC3, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
118 cvtColor(im_RGB, im_RGB, cv::COLOR_RGB2GRAY);
119 break;
120 case rs2_format::RS2_FORMAT_Y8:
121 im_RGB = cv::Mat(size, CV_8UC1, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
122 break;
123 case rs2_format::RS2_FORMAT_Z16:
124 case rs2_format::RS2_FORMAT_Y16:
125 im_RGB = cv::Mat(size, CV_16UC1, (void*)a_frame.get_data(), cv::Mat::AUTO_STEP);
126 break;
127 default:
128 lib_assert(false, "unknown format");
129 }
130 return im_RGB;
131}
132}
Templated logic for doing matrix multiplication.
Definition Matrix.hpp:182
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
static cv::Mat ConvertImgToGray(rs2::video_frame a_frame)
Converts a RealSense video frame to an OpenCV grayscale matrix.
static cv::Mat ConvertImgToColor(rs2::video_frame a_frame)
Converts a RealSense video frame to an OpenCV RGB color matrix.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
@ transform
A 4x4 transform matrix that maps local coordinates into global space.
static Matrix< fltp04 > Convert(Eigen::Matrix4f mat)
Converts an Eigen 4x4 float matrix to an NDEVR Matrix.
Definition System.h:205
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...