-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmyKinect.cpp
More file actions
332 lines (279 loc) · 10.1 KB
/
Copy pathmyKinect.cpp
File metadata and controls
332 lines (279 loc) · 10.1 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
#include "myKinect.h"
#include <iostream>
/// Initializes the default Kinect sensor
HRESULT CBodyBasics::InitializeDefaultSensor()
{
//用于判断每次读取操作的成功与否
HRESULT hr;
//搜索kinect
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr)){
return hr;
}
//找到kinect设备
if (m_pKinectSensor)
{
// Initialize the Kinect and get coordinate mapper and the body reader
IBodyFrameSource* pBodyFrameSource = NULL;//读取骨架
IDepthFrameSource* pDepthFrameSource = NULL;//读取深度信息
IBodyIndexFrameSource* pBodyIndexFrameSource = NULL;//读取背景二值图
//打开kinect
hr = m_pKinectSensor->Open();
//coordinatemapper
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
}
//bodyframe
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_BodyFrameSource(&pBodyFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pBodyFrameSource->OpenReader(&m_pBodyFrameReader);
}
//depth frame
if (SUCCEEDED(hr)){
hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
}
if (SUCCEEDED(hr)){
hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
}
//body index frame
if (SUCCEEDED(hr)){
hr = m_pKinectSensor->get_BodyIndexFrameSource(&pBodyIndexFrameSource);
}
if (SUCCEEDED(hr)){
hr = pBodyIndexFrameSource->OpenReader(&m_pBodyIndexFrameReader);
}
SafeRelease(pBodyFrameSource);
SafeRelease(pDepthFrameSource);
SafeRelease(pBodyIndexFrameSource);
}
if (!m_pKinectSensor || FAILED(hr))
{
std::cout << "Kinect initialization failed!" << std::endl;
return E_FAIL;
}
//skeletonImg,用于画骨架、背景二值图的MAT
skeletonImg.create(cDepthHeight, cDepthWidth, CV_8UC3);
skeletonImg.setTo(0);
//depthImg,用于画深度信息的MAT
depthImg.create(cDepthHeight, cDepthWidth, CV_8UC1);
depthImg.setTo(0);
return hr;
}
/// Main processing function
void CBodyBasics::Update()
{
//每次先清空skeletonImg
skeletonImg.setTo(0);
//如果丢失了kinect,则不继续操作
if (!m_pBodyFrameReader)
{
return;
}
IBodyFrame* pBodyFrame = NULL;//骨架信息
IDepthFrame* pDepthFrame = NULL;//深度信息
IBodyIndexFrame* pBodyIndexFrame = NULL;//背景二值图
//记录每次操作的成功与否
HRESULT hr = S_OK;
//---------------------------------------获取背景二值图并显示---------------------------------
if (SUCCEEDED(hr)){
hr = m_pBodyIndexFrameReader->AcquireLatestFrame(&pBodyIndexFrame);//获得背景二值图信息
}
if (SUCCEEDED(hr)){
BYTE *bodyIndexArray = new BYTE[cDepthHeight * cDepthWidth];//背景二值图是8为uchar,有人是黑色,没人是白色
pBodyIndexFrame->CopyFrameDataToArray(cDepthHeight * cDepthWidth, bodyIndexArray);
//把背景二值图画到MAT里
uchar* skeletonData = (uchar*)skeletonImg.data;
for (int j = 0; j < cDepthHeight * cDepthWidth; ++j){
*skeletonData = bodyIndexArray[j]; ++skeletonData;
*skeletonData = bodyIndexArray[j]; ++skeletonData;
*skeletonData = bodyIndexArray[j]; ++skeletonData;
}
delete[] bodyIndexArray;
}
SafeRelease(pBodyIndexFrame);//必须要释放,否则之后无法获得新的frame数据
//-----------------------获取深度数据并显示--------------------------
if (SUCCEEDED(hr)){
hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);//获得深度数据
}
if (SUCCEEDED(hr)){
UINT16 *depthArray = new UINT16[cDepthHeight * cDepthWidth];//深度数据是16位unsigned int
pDepthFrame->CopyFrameDataToArray(cDepthHeight * cDepthWidth, depthArray);
//把深度数据画到MAT中
uchar* depthData = (uchar*)depthImg.data;
for (int j = 0; j < cDepthHeight * cDepthWidth; ++j){
*depthData = depthArray[j];
++depthData;
}
delete[] depthArray;
}
SafeRelease(pDepthFrame);//必须要释放,否则之后无法获得新的frame数据
imshow("depthImg", depthImg);
cv::waitKey(5);
//-----------------------------获取骨架并显示----------------------------
if (SUCCEEDED(hr)){
hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame);//获取骨架信息
}
if (SUCCEEDED(hr))
{
IBody* ppBodies[BODY_COUNT] = { 0 };//每一个IBody可以追踪一个人,总共可以追踪六个人
if (SUCCEEDED(hr))
{
//把kinect追踪到的人的信息,分别存到每一个IBody中
hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies);
}
if (SUCCEEDED(hr))
{
//对每一个IBody,我们找到他的骨架信息,并且画出来
ProcessBody(BODY_COUNT, ppBodies);
}
for (int i = 0; i < _countof(ppBodies); ++i)
{
SafeRelease(ppBodies[i]);//释放所有
}
}
SafeRelease(pBodyFrame);//必须要释放,否则之后无法获得新的frame数据
}
/// Handle new body data
void CBodyBasics::ProcessBody(int nBodyCount, IBody** ppBodies)
{
//记录操作结果是否成功
HRESULT hr;
//对于每一个IBody
for (int i = 0; i < nBodyCount; ++i)
{
IBody* pBody = ppBodies[i];
if (pBody)//还没有搞明白这里pBody和下面的bTracked有什么区别
{
BOOLEAN bTracked = false;
hr = pBody->get_IsTracked(&bTracked);
if (SUCCEEDED(hr) && bTracked)
{
Joint joints[JointType_Count];//存储关节点类
HandState leftHandState = HandState_Unknown;//左手状态
HandState rightHandState = HandState_Unknown;//右手状态
//获取左右手状态
pBody->get_HandLeftState(&leftHandState);
pBody->get_HandRightState(&rightHandState);
//存储深度坐标系中的关节点位置
DepthSpacePoint *depthSpacePosition = new DepthSpacePoint[_countof(joints)];
//获得关节点类
hr = pBody->GetJoints(_countof(joints), joints);
if (SUCCEEDED(hr))
{
for (int j = 0; j < _countof(joints); ++j)
{
//将关节点坐标从摄像机坐标系(-1~1)转到深度坐标系(424*512)
m_pCoordinateMapper->MapCameraPointToDepthSpace(joints[j].Position, &depthSpacePosition[j]);
}
//------------------------hand state left-------------------------------
DrawHandState(depthSpacePosition[JointType_HandLeft], leftHandState);
DrawHandState(depthSpacePosition[JointType_HandRight], rightHandState);
//---------------------------body-------------------------------
DrawBone(joints, depthSpacePosition, JointType_Head, JointType_Neck);
DrawBone(joints, depthSpacePosition, JointType_Neck, JointType_SpineShoulder);
DrawBone(joints, depthSpacePosition, JointType_SpineShoulder, JointType_SpineMid);
DrawBone(joints, depthSpacePosition, JointType_SpineMid, JointType_SpineBase);
DrawBone(joints, depthSpacePosition, JointType_SpineShoulder, JointType_ShoulderRight);
DrawBone(joints, depthSpacePosition, JointType_SpineShoulder, JointType_ShoulderLeft);
DrawBone(joints, depthSpacePosition, JointType_SpineBase, JointType_HipRight);
DrawBone(joints, depthSpacePosition, JointType_SpineBase, JointType_HipLeft);
// -----------------------Right Arm ------------------------------------
DrawBone(joints, depthSpacePosition, JointType_ShoulderRight, JointType_ElbowRight);
DrawBone(joints, depthSpacePosition, JointType_ElbowRight, JointType_WristRight);
DrawBone(joints, depthSpacePosition, JointType_WristRight, JointType_HandRight);
DrawBone(joints, depthSpacePosition, JointType_HandRight, JointType_HandTipRight);
DrawBone(joints, depthSpacePosition, JointType_WristRight, JointType_ThumbRight);
//----------------------------------- Left Arm--------------------------
DrawBone(joints, depthSpacePosition, JointType_ShoulderLeft, JointType_ElbowLeft);
DrawBone(joints, depthSpacePosition, JointType_ElbowLeft, JointType_WristLeft);
DrawBone(joints, depthSpacePosition, JointType_WristLeft, JointType_HandLeft);
DrawBone(joints, depthSpacePosition, JointType_HandLeft, JointType_HandTipLeft);
DrawBone(joints, depthSpacePosition, JointType_WristLeft, JointType_ThumbLeft);
// ----------------------------------Right Leg--------------------------------
DrawBone(joints, depthSpacePosition, JointType_HipRight, JointType_KneeRight);
DrawBone(joints, depthSpacePosition, JointType_KneeRight, JointType_AnkleRight);
DrawBone(joints, depthSpacePosition, JointType_AnkleRight, JointType_FootRight);
// -----------------------------------Left Leg---------------------------------
DrawBone(joints, depthSpacePosition, JointType_HipLeft, JointType_KneeLeft);
DrawBone(joints, depthSpacePosition, JointType_KneeLeft, JointType_AnkleLeft);
DrawBone(joints, depthSpacePosition, JointType_AnkleLeft, JointType_FootLeft);
}
delete[] depthSpacePosition;
}
}
}
cv::imshow("skeletonImg", skeletonImg);
cv::waitKey(5);
}
//画手的状态
void CBodyBasics::DrawHandState(const DepthSpacePoint depthSpacePosition, HandState handState)
{
//给不同的手势分配不同颜色
CvScalar color;
switch (handState){
case HandState_Open:
color = cvScalar(255, 0, 0);
break;
case HandState_Closed:
color = cvScalar(0, 255, 0);
break;
case HandState_Lasso:
color = cvScalar(0, 0, 255);
break;
default://如果没有确定的手势,就不要画
return;
}
circle(skeletonImg,
cvPoint(depthSpacePosition.X, depthSpacePosition.Y),
20, color, -1);
}
/// Draws one bone of a body (joint to joint)
void CBodyBasics::DrawBone(const Joint* pJoints, const DepthSpacePoint* depthSpacePosition, JointType joint0, JointType joint1)
{
TrackingState joint0State = pJoints[joint0].TrackingState;
TrackingState joint1State = pJoints[joint1].TrackingState;
// If we can't find either of these joints, exit
if ((joint0State == TrackingState_NotTracked) || (joint1State == TrackingState_NotTracked))
{
return;
}
// Don't draw if both points are inferred
if ((joint0State == TrackingState_Inferred) && (joint1State == TrackingState_Inferred))
{
return;
}
CvPoint p1 = cvPoint(depthSpacePosition[joint0].X, depthSpacePosition[joint0].Y),
p2 = cvPoint(depthSpacePosition[joint1].X, depthSpacePosition[joint1].Y);
// We assume all drawn bones are inferred unless BOTH joints are tracked
if ((joint0State == TrackingState_Tracked) && (joint1State == TrackingState_Tracked))
{
//非常确定的骨架,用白色直线
line(skeletonImg, p1, p2, cvScalar(255, 255, 255));
}
else
{
//不确定的骨架,用红色直线
line(skeletonImg, p1, p2, cvScalar(0, 0, 255));
}
}
/// Constructor
CBodyBasics::CBodyBasics() :
m_pKinectSensor(NULL),
m_pCoordinateMapper(NULL),
m_pBodyFrameReader(NULL){}
/// Destructor
CBodyBasics::~CBodyBasics()
{
SafeRelease(m_pBodyFrameReader);
SafeRelease(m_pCoordinateMapper);
if (m_pKinectSensor)
{
m_pKinectSensor->Close();
}
SafeRelease(m_pKinectSensor);
}