Repository navigation
Expand file tree
/
Copy pathkinect.cpp
More file actions
338 lines (322 loc) · 11.3 KB
/
Copy pathkinect.cpp
File metadata and controls
338 lines (322 loc) · 11.3 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
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
#include <windows.h>
#include <iostream>
#include <NuiApi.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
//Kinect录制并同时存储深度和彩色图像
//kinect
int main()
{
cout << "input 1 to store video , input 0 is not"<<endl;
int store = 0;
cin >> store;
string colorImgPath="F:\\kinect_vedio\\color\\";
string depthImgPath="F:\\kinect_vedio\\depth\\";
Mat image_rgb, image_depth;
image_rgb.create(480, 640, CV_8UC3); //初始化3通道的彩色图像
image_depth.create(480, 640, CV_8UC1);//初始化单通道的深度图像
//1、初始化NUI 使用彩色数据和不带游戏者ID的深度数据
HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH);
if (FAILED(hr))
{
cout<<"NuiInitialize failed"<<endl;
return hr;
}
//2、定义事件句柄
//创建读取下一帧的信号事件句柄,控制KINECT是否可以开始读取下一帧数据
HANDLE nextColorFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
HANDLE colorStreamHandle = NULL; //保存彩色图像数据流的句柄,用以提取数据
HANDLE nextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
HANDLE depthStreamHandle = NULL; //保存深度图像数据流的句柄,用以提取数据
//3、打开KINECT设备的彩色图信息通道,并用colorStreamHandle保存该流的句柄,以便于以后读取
hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
0, 2, nextColorFrameEvent, &colorStreamHandle);
if( FAILED( hr ) )//判断是否提取正确
{
cout<<"Could not open color image stream video"<<endl;
NuiShutdown();
return hr;
}
hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480,
0, 2, nextDepthFrameEvent, &depthStreamHandle);
if( FAILED( hr ) )//判断是否提取正确
{
cout<<"Could not open depth image stream video"<<endl;
NuiShutdown();
return hr;
}
namedWindow("colorImage", CV_WINDOW_AUTOSIZE);
namedWindow("depthImage", CV_WINDOW_AUTOSIZE);
//4、开始读取彩色图数据和深度图数据
int num_colorImg=0;
int num_depthImg=0;
while(1)
{
const NUI_IMAGE_FRAME * pImageFrame_rgb = NULL;
const NUI_IMAGE_FRAME * pImageFrame_depth = NULL;
//4.1、无限等待新的彩色数据,等到后返回
if (WaitForSingleObject(nextColorFrameEvent, INFINITE)==0)
{
//4.2、从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于pImageFrame
hr = NuiImageStreamGetNextFrame(colorStreamHandle, 0, &pImageFrame_rgb);
if (FAILED(hr))
{
cout<<"Could not get color image"<<endl;
NuiShutdown();
return -1;
}
INuiFrameTexture * pTexture = pImageFrame_rgb->pFrameTexture;
NUI_LOCKED_RECT LockedRect;
//4.3、提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址
//并锁定数据,这样当我们读数据的时候,kinect就不会去修改它
pTexture->LockRect(0, &LockedRect, NULL, 0);
//4.4、确认获得的数据是否有效
if( LockedRect.Pitch != 0 )
{
//4.5、将数据转换为OpenCV的Mat格式
for (int i=0; i<image_rgb.rows; i++)
{
uchar *ptr = image_rgb.ptr(i); //第i行的指针
//每个字节代表一个颜色信息,直接使用uchar
uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
for (int j=0; j<image_rgb.cols; j++)
{
ptr[3*j] = pBuffer[4*j]; //内部数据是4个字节,0-1-2是BGR,第4个现在未使用
ptr[3*j+1] = pBuffer[4*j+1];
ptr[3*j+2] = pBuffer[4*j+2];
}
}
imshow("colorImage", image_rgb); //显示图像
if (store == 1){
imwrite(colorImgPath + to_string(num_colorImg) + ".jpg", image_rgb);
}
num_colorImg++;
}
else
{
cout<<"Buffer length of received texture is bogus\r\n"<<endl;
}
//5、这帧已经处理完了,所以将其解锁
pTexture->UnlockRect(0);
//6、释放本帧数据,准备迎接下一帧
NuiImageStreamReleaseFrame(colorStreamHandle, pImageFrame_rgb );
}
//7.1、无限等待新的深度数据,等到后返回
if (WaitForSingleObject(nextDepthFrameEvent, INFINITE)==0)
{
//7.2、从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于pImageFrame
hr = NuiImageStreamGetNextFrame(depthStreamHandle, 0, &pImageFrame_depth);
if (FAILED(hr))
{
cout<<"Could not get color image"<<endl;
NuiShutdown();
return -1;
}
INuiFrameTexture * pTexture = pImageFrame_depth->pFrameTexture;
NUI_LOCKED_RECT LockedRect;
//7.3、提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址
//并锁定数据,这样当我们读数据的时候,kinect就不会去修改它
pTexture->LockRect(0, &LockedRect, NULL, 0);
//7.4、确认获得的数据是否有效
if( LockedRect.Pitch != 0 )
{
//7.5、将数据转换为OpenCV的Mat格式
for (int i=0; i<image_depth.rows; i++)
{
uchar *ptr = image_depth.ptr(i); //第i行的指针
//每个字节代表一个颜色信息,直接使用uchar
uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
USHORT *pBufferRun = (USHORT *)pBuffer;//这里需要转换,因为每个深度数据是2个字节,应将BYTE转成USHORT
for (int j=0; j<image_depth.cols; j++)
{
ptr[j] = 255-(BYTE)(256*pBufferRun[j]/0x1fff); //将数据归一化处理*
}
}
imshow("depthImage", image_depth); //显示图像
if (store == 1){
imwrite(depthImgPath + to_string(num_depthImg) + ".jpg", image_depth);
}
num_depthImg++;
}
else
{
cout<<"Buffer length of received texture is bogus\r\n"<<endl;
}
//8、这帧已经处理完了,所以将其解锁
pTexture->UnlockRect(0);
//9、释放本帧数据,准备迎接下一帧
NuiImageStreamReleaseFrame(depthStreamHandle, pImageFrame_depth );
}
if (cvWaitKey(20) == 27)
break;
}
//10、关闭NUI链接
NuiShutdown();
return 0;
}
//int main(int argc, char *argv[])
//{
// Mat colorImage;
// Mat depthImage;
// colorImage.create(480, 640, CV_8UC3);
// depthImage.create(480,640,CV_8UC1);
//
// //1、初始化NUI
// HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR|NUI_INITIALIZE_FLAG_USES_DEPTH);
// if (FAILED(hr))
// {
// cout<<"NuiInitialize failed"<<endl;
// return hr;
// }
//
// //2、定义事件句柄
// //创建读取下一帧的信号事件句柄,控制KINECT是否可以开始读取下一帧数据
// HANDLE nextColorFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
// HANDLE colorStreamHandle = NULL; //保存彩色图像数据流的句柄,用以提取数据
//
// HANDLE nextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
// HANDLE depthStreamHandle = NULL; //保存深度图像数据流的句柄,用以提取数据
// //3、打开KINECT设备的彩色图信息通道,并用colorStreamHandle保存该流的句柄,以便于以后读取
// hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
// 0, 2, nextColorFrameEvent, &colorStreamHandle);
// if( FAILED( hr ) )//判断是否提取正确
// {
// cout<<"Could not open color image stream video"<<endl;
// NuiShutdown();
// return hr;
// }
// namedWindow("colorImage", CV_WINDOW_AUTOSIZE);
//
// hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
// 0, 2, nextDepthFrameEvent, &depthStreamHandle);
// if( FAILED( hr ) )//判断是否提取正确
// {
// cout<<"Could not open color image stream video"<<endl;
// NuiShutdown();
// return hr;
// }
// namedWindow("depthImage", CV_WINDOW_AUTOSIZE);
// //4、开始读取彩色图数据
// while(1)
// {
// const NUI_IMAGE_FRAME * pColorImageFrame = NULL;
// const NUI_IMAGE_FRAME * pDepthImageFrame = NULL;
//
// //////4.1、无限等待新的数据,等到后返回
// // if (WaitForSingleObject(nextColorFrameEvent, INFINITE)==0)
// // {
// // //4.2、从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于pImageFrame
// // hr = NuiImageStreamGetNextFrame(colorStreamHandle, 0, &pColorImageFrame);
// // if (FAILED(hr))
// // {
// // cout<<"Could not get color image"<<endl;
// // NuiShutdown();
// // return -1;
// // }
//
// // INuiFrameTexture * pTexture = pColorImageFrame->pFrameTexture;
// // NUI_LOCKED_RECT LockedRect;
//
// // //4.3、提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址
// // //并锁定数据,这样当我们读数据的时候,kinect就不会去修改它
// // pTexture->LockRect(0, &LockedRect, NULL, 0);
// // //4.4、确认获得的数据是否有效
// // if( LockedRect.Pitch != 0 )
// // {
// // //4.5、将数据转换为OpenCV的Mat格式
// // for (int i=0; i<colorImage.rows; i++)
// // {
// // uchar *ptr = colorImage.ptr<uchar>(i); //第i行的指针
// //
// // //每个字节代表一个颜色信息,直接使用uchar
// // uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
// // for (int j=0; j<colorImage.cols; j++)
// // {
// // ptr[3*j] = pBuffer[4*j]; //内部数据是4个字节,0-1-2是BGR,第4个现在未使用
// // ptr[3*j+1] = pBuffer[4*j+1];
// // ptr[3*j+2] = pBuffer[4*j+2];
// // }
// // }
// // imshow("colorImage", colorImage); //显示图像
// // }
// // else
// // {
// // cout<<"Buffer length of received texture is bogus\r\n"<<endl;
// // }
//
// // //5、这帧已经处理完了,所以将其解锁
// // pTexture->UnlockRect(0);
// // //6、释放本帧数据,准备迎接下一帧
// // NuiImageStreamReleaseFrame(colorStreamHandle, pColorImageFrame );
// // }
// //7.1、无限等待新的数据,等到后返回
// cout<<6<<endl;
//
// if (WaitForSingleObject(nextDepthFrameEvent, INFINITE)==0)
// {
// cout<<9<<endl;
//
// //7.2、从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于pImageFrame
// hr = NuiImageStreamGetNextFrame(depthStreamHandle, 0, &pDepthImageFrame);
// if (FAILED(hr))
// {
// cout<<"Could not get color image"<<endl;
// NuiShutdown();
// return -1;
// }
// cout<<0<<endl;
//
// INuiFrameTexture * pTexture = pDepthImageFrame->pFrameTexture;
// NUI_LOCKED_RECT LockedRect;
// cout<<1<<endl;
// //7.3、提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址
// //并锁定数据,这样当我们读数据的时候,kinect就不会去修改它
// pTexture->LockRect(0, &LockedRect, NULL, 0);
// //7.4、确认获得的数据是否有效
// cout<<2<<endl;
//
// if( LockedRect.Pitch != 0 )
// {
// //7.5、将数据转换为OpenCV的Mat格式
// for (int i=0; i<depthImage.rows; i++)
// {
// cout<<3<<endl;
//
// uchar *ptr = depthImage.ptr<uchar>(i); //第i行的指针
//
// //每个字节代表一个颜色信息,直接使用uchar
// uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
// USHORT *pBufferRun = (USHORT *)pBuffer;
// for (int j=0; j<depthImage.cols; j++)
// {
// cout<<4<<endl;
//
// ptr[j] = 255-(BYTE)(256*pBufferRun[j]/0x1fff); //将数据归一化处理*
// }
// cout<<5<<endl;
//
// }
// imshow("depthImage", depthImage); //显示图像
// }
// else
// {
// cout<<"Buffer length of received texture is bogus\r\n"<<endl;
// }
//
// //5、这帧已经处理完了,所以将其解锁
// pTexture->UnlockRect(0);
// //6、释放本帧数据,准备迎接下一帧
// NuiImageStreamReleaseFrame(depthStreamHandle, pDepthImageFrame );
// }
// cout<<"a"<<endl;
// if (cvWaitKey(20) == 27)
// break;
// }
// //7、关闭NUI链接
// NuiShutdown();
// return 0;
//}