-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathk4a2mat.hpp
More file actions
119 lines (105 loc) · 3.88 KB
/
k4a2mat.hpp
File metadata and controls
119 lines (105 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
This is utility to that provides converter to convert k4a::image to cv::Mat.
cv::Mat mat = k4a::get_mat( image );
Copyright (c) 2019 Tsukasa Sugiura <t.sugiura0204@gmail.com>
Licensed under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <vector>
#include <limits>
#include <k4a/k4a.h>
#include <k4a/k4a.hpp>
#include <opencv2/opencv.hpp>
namespace k4a
{
static cv::Mat get_mat(k4a::image& src, bool deep_copy = true)
{
if (src.get_size() == 0)
return cv::Mat();
cv::Mat mat;
const int32_t width = src.get_width_pixels();
const int32_t height = src.get_height_pixels();
const k4a_image_format_t format = src.get_format();
switch (format)
{
case k4a_image_format_t::K4A_IMAGE_FORMAT_COLOR_MJPG:
{
// NOTE: this is slower than other formats.
std::vector<uint8_t> buffer(src.get_buffer(), src.get_buffer() + src.get_size());
mat = cv::imdecode(buffer, cv::IMREAD_ANYCOLOR);
if (mat.rows == 0 || mat.cols == 0)
break;
cv::cvtColor(mat, mat, cv::COLOR_BGR2BGRA);
break;
}
case k4a_image_format_t::K4A_IMAGE_FORMAT_COLOR_NV12:
{
cv::Mat nv12 = cv::Mat(height + height / 2, width, CV_8UC1, src.get_buffer()).clone();
cv::cvtColor(nv12, mat, cv::COLOR_YUV2BGRA_NV12);
break;
}
case k4a_image_format_t::K4A_IMAGE_FORMAT_COLOR_YUY2:
{
cv::Mat yuy2 = cv::Mat(height, width, CV_8UC2, src.get_buffer()).clone();
cv::cvtColor(yuy2, mat, cv::COLOR_YUV2BGRA_YUY2);
break;
}
case k4a_image_format_t::K4A_IMAGE_FORMAT_COLOR_BGRA32:
{
mat = deep_copy ? cv::Mat(height, width, CV_8UC4, src.get_buffer()).clone()
: cv::Mat(height, width, CV_8UC4, src.get_buffer());
break;
}
case k4a_image_format_t::K4A_IMAGE_FORMAT_DEPTH16:
case k4a_image_format_t::K4A_IMAGE_FORMAT_IR16:
{
mat = deep_copy ? cv::Mat(height, width, CV_16UC1, reinterpret_cast<uint16_t*>(src.get_buffer())).clone()
: cv::Mat(height, width, CV_16UC1, reinterpret_cast<uint16_t*>(src.get_buffer()));
break;
}
case k4a_image_format_t::K4A_IMAGE_FORMAT_CUSTOM8:
{
mat = cv::Mat(height, width, CV_8UC1, src.get_buffer()).clone();
break;
}
case k4a_image_format_t::K4A_IMAGE_FORMAT_CUSTOM:
{
// NOTE: This is opencv_viz module format (cv::viz::WCloud).
const int16_t* buffer = reinterpret_cast<int16_t*>(src.get_buffer());
mat = cv::Mat(height, width, CV_32FC3, cv::Vec3f::all(std::numeric_limits<float>::quiet_NaN()));
mat.forEach<cv::Vec3f>(
[&](cv::Vec3f& point, const int32_t* position) {
const int32_t index = (position[0] * width + position[1]) * 3;
point = cv::Vec3f(buffer[index + 0], buffer[index + 1], buffer[index + 2]);
}
);
break;
}
default:
throw k4a::error("Failed to convert this format!");
break;
}
return mat;
}
static cv::Mat k4a_get_mat(k4a_image_t& src, bool deep_copy = true)
{
k4a_image_reference(src);
k4a::image img = k4a::image(src);
return k4a::get_mat(img, deep_copy);
}
}