-
Notifications
You must be signed in to change notification settings - Fork 0
/
KinectV2.hpp
145 lines (108 loc) · 3.73 KB
/
KinectV2.hpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef KINECT_V2_HPP
#define KINECT_V2_HPP
#include <Kinect.h>
#include "Sensor.hpp"
class KinectV2 : public Sensor {
private:
IKinectSensor* m_pKinectSensor = nullptr;
IDepthFrameSource* pDepthFrameSource = nullptr;
IDepthFrameReader* m_pDepthFrameReader = nullptr;
IColorFrameSource* pColorFrameSource = nullptr;
IColorFrameReader* m_pColorFrameReader = nullptr;
IDepthFrame* pDepthFrame = NULL;
IColorFrame* pColorFrame = NULL;
bool depthFrameSizeChanged = true;
bool colorFrameSizeChanged = true;
cv::Mat depthMat;
cv::Mat colorMat;
CameraIntrinsics depthIntrinsics;
public:
KinectV2() {
depthIntrinsics = { 0 };
if (FAILED(GetDefaultKinectSensor(&m_pKinectSensor))) {
throw std::runtime_error("Failed to get default sensor!");
}
if (FAILED(m_pKinectSensor->Open())) {
throw std::runtime_error("Failed to open default sensor!");
}
std::this_thread::sleep_for(std::chrono::seconds(2));
BOOLEAN isAvailable = FALSE;
do {
if (FAILED(m_pKinectSensor->get_IsAvailable(&isAvailable))) {
isAvailable = FALSE;
}
} while (isAvailable == FALSE);
if (FAILED(m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource))) {
throw std::runtime_error("Failed to get depth sensor!");
}
if (FAILED(pDepthFrameSource->OpenReader(&m_pDepthFrameReader))) {
throw std::runtime_error("Failed to open depth sensor!");
}
if (FAILED(m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource))) {
throw std::runtime_error("Failed to get color sensor!");
}
if (FAILED(pColorFrameSource->OpenReader(&m_pColorFrameReader))) {
throw std::runtime_error("Failed to open color sensor!");
}
ICoordinateMapper* cm;
if (FAILED(m_pKinectSensor->get_CoordinateMapper(&cm))) {
throw std::runtime_error("Failed to get coordinate mapper.");
}
if (FAILED(cm->GetDepthCameraIntrinsics(&depthIntrinsics))) {
throw std::runtime_error("Failed to read Kinect v2.0 intrinsics.");
}
}
virtual bool getColorFrame(cv::Mat& colorMat) override {
HRESULT hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);
if (SUCCEEDED(hr)) {
if (colorFrameSizeChanged) {
IFrameDescription* pFrameDescription = nullptr;
int nWidth = 0;
int nHeight = 0;
pColorFrame->get_FrameDescription(&pFrameDescription);
pFrameDescription->get_Width(&nWidth);
pFrameDescription->get_Height(&nHeight);
colorMat.create(nHeight, nWidth, CV_8UC4);
colorFrameSizeChanged = false;
pFrameDescription->Release();
pFrameDescription = nullptr;
}
pColorFrame->CopyConvertedFrameDataToArray(colorMat.rows * colorMat.cols * 4, (BYTE*)colorMat.data, ColorImageFormat::ColorImageFormat_Bgra);
pColorFrame->Release();
pColorFrame = nullptr;
return true;
}
return false;
}
virtual bool getDepthFrame(cv::Mat& depthMat) override {
HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
if (SUCCEEDED(hr)) {
IFrameDescription* pFrameDescription = nullptr;
int nWidth = 0;
int nHeight = 0;
pDepthFrame->get_FrameDescription(&pFrameDescription);
pFrameDescription->get_Width(&nWidth);
pFrameDescription->get_Height(&nHeight);
depthMat.release();
UINT16* data = new UINT16[nWidth * nHeight];
if (FAILED(pDepthFrame->CopyFrameDataToArray(nWidth * nHeight, data))) {
pDepthFrame->Release();
return false;
}
depthMat = cv::Mat(cv::Size(nWidth, nHeight), CV_16UC1, (void*)data, cv::Mat::AUTO_STEP);
pDepthFrame->Release();
pDepthFrame = NULL;
return true;
}
return false;
}
virtual void getIntrinsics(float& fx, float& fy, float& ppx, float& ppy) override {
fx = depthIntrinsics.FocalLengthX;
fy = depthIntrinsics.FocalLengthY;
ppx = depthIntrinsics.PrincipalPointX;
ppy = depthIntrinsics.PrincipalPointY;
}
~KinectV2() {
}
};
#endif