-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
455 lines (390 loc) · 10.4 KB
/
widget.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <QDebug>
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
if (pInterfaceToRelease != NULL)
{
pInterfaceToRelease->Release();
pInterfaceToRelease = NULL;
}
}
static const int cColorWidth = 1920;
static const int cColorHeight = 1080;
IKinectSensor* m_pKinectSensor;// Current Kinect
IColorFrameReader* m_pColorFrameReader;// Color reader
RGBQUAD* m_pColorRGBX;
void SetIdentityMatrix(Matrix4 &mat)
{
mat.M11 = 1.f; mat.M12 = 0.f; mat.M13 = 0.f; mat.M14 = 0.f;
mat.M21 = 0.f; mat.M22 = 1.f; mat.M23 = 0.f; mat.M24 = 0.f;
mat.M31 = 0.f; mat.M32 = 0.f; mat.M33 = 1.f; mat.M34 = 0.f;
mat.M41 = 0.f; mat.M42 = 0.f; mat.M43 = 0.f; mat.M44 = 1.f;
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_processorType = NUI_FUSION_RECONSTRUCTION_PROCESSOR_TYPE_AMP;
m_deviceIndex = -1;
SetIdentityMatrix(m_worldToCameraTransform);
SetIdentityMatrix(m_defaultWorldToVolumeTransform);
m_cDepthWidth = NUI_DEPTH_RAW_WIDTH;
m_cDepthHeight = NUI_DEPTH_RAW_HEIGHT;
m_reconstructionParams.voxelsPerMeter = 256.f;
m_reconstructionParams.voxelCountX = 384;
m_reconstructionParams.voxelCountY = 384;
m_reconstructionParams.voxelCountZ = 384;
m_pKinectSensor = NULL;
m_pColorFrameReader = NULL;
m_pColorRGBX = new RGBQUAD[cColorWidth * cColorHeight];// create heap storage for color pixel data in RGBX format
init();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateKinectData()));
connect(this->ui->pushButton, SIGNAL(clicked()), this, SLOT(debugButton()));
timer->start(33);
}
Widget::~Widget()
{
delete ui;
if (m_pColorRGBX)
{
delete [] m_pColorRGBX;
m_pColorRGBX = NULL;
}
SafeRelease(m_pColorFrameReader);// done with color frame reader
if (m_pKinectSensor)
{
m_pKinectSensor->Close();// close the Kinect Sensor
}
SafeRelease(m_pKinectSensor);
}
void Widget::updateKinectData()
{
//this->colorcam();
//this->depthcam();
this->check_depth_frame();
}
void Widget::debugButton()
{
this->check_depth_frame();
//this->depthcam();
}
void Widget::colorcam()
{
if (!m_pColorFrameReader)
{
return;
}
IColorFrame* pColorFrame = NULL;
HRESULT hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);
if (SUCCEEDED(hr))
{
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
ColorImageFormat imageFormat = ColorImageFormat_None;
UINT nBufferSize = 0;
RGBQUAD *pBuffer = NULL;
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_FrameDescription(&pFrameDescription);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
}
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
//qDebug() << "imageformat is" << imageFormat;
}
if (SUCCEEDED(hr))
{
if (imageFormat == ColorImageFormat_Bgra)
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer));
qDebug() << "buffer size is:" << nBufferSize;
}
else if (m_pColorRGBX)
{
pBuffer = m_pColorRGBX;
nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra);
}
else
{
hr = E_FAIL;
}
}
if (SUCCEEDED(hr))
{
//ui->label->setPixmap(QPixmap::fromImage(QImage((uchar*)pBuffer, nWidth , nHeight, QImage::Format_RGB32)).scaledToWidth(800));
ui->label->setPixmap(QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(pBuffer), nWidth, nHeight, QImage::Format_RGB32)).scaledToWidth(800));
//ProcessColor(pBuffer, nWidth, nHeight);
}
SafeRelease(pFrameDescription);
}
SafeRelease(pColorFrame);
}
void Widget::init()
{
HRESULT hr;
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr))
{
return ;
}
if (m_pKinectSensor)
{
// Initialize the Kinect and get the color reader
hr = m_pKinectSensor->Open();
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource);
hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pColorFrameSource->OpenReader(&m_pColorFrameReader);
}
if (SUCCEEDED(hr)) {
hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
}
if (SUCCEEDED(hr)) {
hr = m_pKinectSensor->get_CoordinateMapper(&m_pMapper);
}
if (SUCCEEDED(hr)) {
hr = NuiFusionCreateReconstruction(
// 重建参数
&m_reconstructionParams,
//cpu模式还是gpu模式
m_processorType,
//用了哪一个设备 默认-1
m_deviceIndex,
// 世界到相机的转换
&m_worldToCameraTransform,
// 容器输出
&m_pReconstruction
);
}
if (FAILED(hr)) { qDebug() << "error!"; }
if (SUCCEEDED(hr)) {
hr = m_pReconstruction->GetCurrentWorldToVolumeTransform(&m_defaultWorldToVolumeTransform);
}
// 创建浮点深度帧
if (SUCCEEDED(hr)) {
hr = NuiFusionCreateImageFrame(
NUI_FUSION_IMAGE_TYPE_FLOAT,
m_cDepthWidth,
m_cDepthHeight,
nullptr,
&m_pDepthFloatImage
);
}
// 创建平滑浮点深度帧
if (SUCCEEDED(hr)) {
hr = NuiFusionCreateImageFrame(
NUI_FUSION_IMAGE_TYPE_FLOAT,
m_cDepthWidth,
m_cDepthHeight,
nullptr,
&m_pSmoothDepthFloatImage
);
}
// 创建点云帧
if (SUCCEEDED(hr)) {
hr = NuiFusionCreateImageFrame(
NUI_FUSION_IMAGE_TYPE_POINT_CLOUD,
m_cDepthWidth,
m_cDepthHeight,
nullptr,
&m_pPointCloud
);
}
// 创建Fusion图像帧
if (SUCCEEDED(hr)) {
hr = NuiFusionCreateImageFrame(
NUI_FUSION_IMAGE_TYPE_COLOR,
m_cDepthWidth,
m_cDepthHeight,
nullptr,
&m_pSurfaceImageFrame
);
}
// 创建Fusion法线帧
if (SUCCEEDED(hr)) {
hr = NuiFusionCreateImageFrame(
NUI_FUSION_IMAGE_TYPE_COLOR,
m_cDepthWidth,
m_cDepthHeight,
nullptr,
&m_pNormalImageFrame
);
}
SafeRelease(pDepthFrameSource);
SafeRelease(pColorFrameSource);
}
if (!m_pKinectSensor || FAILED(hr))
{
printf("No ready Kinect found! \n");
return;
}
}
void Widget::check_depth_frame() {
if (!m_pDepthFrameReader)
{
qDebug() << "dont have pepth frame reader!";
return;
}
// 深度帧
IDepthFrame* pDepthFrame = nullptr;
// 帧描述
IFrameDescription* pFrameDescription = nullptr;
// 深度帧宽度数据
int width = 0;
// 深度帧高度数据
int height = 0;
// 最近有效值
USHORT depth_min_reliable_distance = 0;
// 最远有效值
USHORT depth_max_reliable_distance = 0;
// 帧缓存大小
UINT nBufferSize = 0;
// 深度缓存
UINT16 *pBuffer = nullptr;
// 获取深度帧
HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
// 获取帧描述
if (SUCCEEDED(hr)) {
hr = pDepthFrame->get_FrameDescription(&pFrameDescription);
}
// 获取帧宽度
if (SUCCEEDED(hr)) {
hr = pFrameDescription->get_Width(&width);
}
// 获取帧高度
if (SUCCEEDED(hr)) {
hr = pFrameDescription->get_Height(&height);
}
// 获取最近有效距离值
if (SUCCEEDED(hr)) {
hr = pDepthFrame->get_DepthMinReliableDistance(&depth_min_reliable_distance);
}
// 获取最远有效距离值
if (SUCCEEDED(hr)) {
hr = pDepthFrame->get_DepthMaxReliableDistance(&depth_max_reliable_distance);
}
// 获取深度数据
if (SUCCEEDED(hr)) {
hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
qDebug() << "buffer size is:" << nBufferSize;
}
// ----------- Fusion 处理开始
HRESULT hr4f = hr;
// 由原深度数据构造浮点数据
if (SUCCEEDED(hr4f)) {
hr4f = m_pReconstruction->DepthToDepthFloatFrame(
pBuffer,
nBufferSize * sizeof(UINT16),
m_pDepthFloatImage,
m_fMinDepthThreshold,
m_fMaxDepthThreshold,
true
);
}
// 平滑数据
if (SUCCEEDED(hr4f)) {
hr4f = m_pReconstruction->SmoothDepthFloatFrame(
m_pDepthFloatImage,
m_pSmoothDepthFloatImage,
1,
0.03f
);
}
// 处理当前帧
if (SUCCEEDED(hr4f)) {
hr4f = m_pReconstruction->ProcessFrame(
m_pSmoothDepthFloatImage,
/*
*参数为配准过程中的迭代次数,
*该参数用来表示对齐相机追踪算法的迭代次数,
*最小值为1,值越小的计算速度更快,
*但是设置过小会导致配准过程不收敛,从而得不到正确的转换。*/
NUI_FUSION_DEFAULT_ALIGN_ITERATION_COUNT,
/*
*参数用来控制深度影像融合的平滑参数,
*值过小会使得的图像具有更多的噪点,
*但是物体的移动显示的更快,消失的也更快,因此比较适合动态场景建模。
*大的值使得物体融合的更慢,但是会保有更多的细节,噪点更少。
*/
m_cMaxIntegrationWeight,
nullptr,
&m_worldToCameraTransform
);
}
// 检查错误
if (hr4f == E_NUI_FUSION_TRACKING_ERROR) {
qDebug()<< "Fusion tracking fail";
}
else if (SUCCEEDED(hr)) {
qDebug() << "Fusion ture";
}
else {
qDebug() << "Fusion tracking fail";
}
// 获取当前矩阵
if (SUCCEEDED(hr4f)) {
Matrix4 calculatedCameraPose;
hr4f = m_pReconstruction->GetCurrentWorldToCameraTransform(&calculatedCameraPose);
if (SUCCEEDED(hr4f)) {
m_worldToCameraTransform = calculatedCameraPose;
}
}
// 计算点云
if (SUCCEEDED(hr4f)) {
hr4f = m_pReconstruction->CalculatePointCloud(
m_pPointCloud,
&m_worldToCameraTransform
);
}
// 生成图像帧
if (SUCCEEDED(hr4f)) {
// Shading Point Clouid
Matrix4 worldToBGRTransform = { 0.0f };
worldToBGRTransform.M11 = m_reconstructionParams.voxelsPerMeter / m_reconstructionParams.voxelCountX;
worldToBGRTransform.M22 = m_reconstructionParams.voxelsPerMeter / m_reconstructionParams.voxelCountY;
worldToBGRTransform.M33 = m_reconstructionParams.voxelsPerMeter / m_reconstructionParams.voxelCountZ;
worldToBGRTransform.M41 = 0.5f;
worldToBGRTransform.M42 = 0.5f;
worldToBGRTransform.M43 = 0.0f;
worldToBGRTransform.M44 = 1.0f;
//SetIdentityMatrix(worldToBGRTransform);
//
hr = NuiFusionShadePointCloud(
m_pPointCloud,
&m_worldToCameraTransform,
&worldToBGRTransform,
m_pSurfaceImageFrame,
m_pNormalImageFrame
);
}
if (SUCCEEDED(hr4f))
{
RGBQUAD *pBuffer = NULL;
pBuffer = m_pColorRGBX;
pBuffer=reinterpret_cast<RGBQUAD*>(m_pSurfaceImageFrame->pFrameBuffer->pBits);
//pBuffer = reinterpret_cast<RGBQUAD*>(m_pNormalImageFrame->pFrameBuffer->pBits);
ui->label->setPixmap(QPixmap::fromImage(QImage(reinterpret_cast<uchar*>(pBuffer), width, height, QImage::Format_RGB32)).scaledToWidth(800));
}
//SafeRelease(pBuffer);
SafeRelease(pFrameDescription);
SafeRelease(pDepthFrame);
}