骨骼数据不像前两个数据,转成字节数组,加载到图片控件上。
一个Kinect for windows设备最多识别6个人,其中只有两个人能识别完整,一个完整的人Kinect for windows中提供了全身20个关节的点,分别为:1、头(Head),2、肩中央(ShoulderCenter),3、左肩(ShoulderLeft),4、右肩(ShoulderRight),5、左肘(ElbowLeft),6、右肘(ElbowRight),7、左腕(WristLeft),8、右腕(WristRight),9、左手(HandLeft),10、右手(HandRight),11、脊柱(Spine),12、 髋中央(HipCenter),13、左髋(HipLeft),14、右髋(HipRight),15、左膝(KneeLeft),16、右膝(KneeRight),17、左髁(AnkleLeft),18、右髁(AnkleRight),19、左脚(FootLeft),20、右脚(FootRight)。
本例可以从骨骼数据中获取这6个人的点,再找到被完整跟踪的人(最多两个人)的20个关节点显示在屏幕上。
新建一个 Winform项目。
然后写如下代码:
1. KinectSensor kinectsensor = null;
2. private void Form1_Shown(object
3. {
4. //从Kinect集合中找到连接上的Kinect
5. foreach (KinectSensor ks in
6. {
7. //找到连接的Kinect
8. if
9. {
10. kinectsensor = ks;
11. //平滑参数
12. new
13. //调用骨骼流
14. kinectsensor.SkeletonStream.Enable(smoothingParam);
15. //设置更近模
16. kinectsensor.DepthStream.Range = DepthRange.Near;
17. //骨骼模式为更近模式
18. true;
19. //坐姿或站姿 坐姿中识别上半身的10个点,默认模式识别全身20个点
20. //kinectsensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
21. //订阅骨骼识别事件
22. kinectsensor.SkeletonFrameReady += kinectsensor_SkeletonFrameReady;
23. //开始工作,即可以采集摄像头和红外摄像头信息
24. this.Text = "Kinect开始工作……";
25. return;
26. }
27. }
28. }
29. //定义骨骼数组,因为一个Kinect最多可识别6个人,其中两个人的骨骼的20个点可以识别,另4个人只识别成4个点
30. Skeleton[] skeletonDataArr;
31. void kinectsensor_SkeletonFrameReady(object
32. {
33. this.Refresh();
34.
35. using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) // Open the Skeleton frame
36. {
37. //实例化骨骼数组
38. this.skeletonDataArr = new
39.
40. if (skeletonFrame != null && this.skeletonDataArr != null)
41. {
42. //复制数据到骨骼数组中
43. this.skeletonDataArr);
44. //设定人与人间的距离间隔
45. int
46. //遍历识别最多识别的6个人
47. foreach (var skeletondata in
48. {
49. this.CreateGraphics();
50. //设定放大步长为200
51. int
52. //绘制6个人的位置
53. new
54. #region 绘制被跟踪到的完整的人
55. if
56. {
57. if
58. {
59. //定义红色的笔
60. new
61.
62. //得到头部联合点
63. Joint HeadJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.Head);
64. //得到肩中联合点
65. Joint ShoulderCenterJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ShoulderCenter);
66. //画头和肩中的连线
67. gra.DrawLine(pen, (HeadJoint.Position.X + 1) * step + Distance, (1 - HeadJoint.Position.Y) * step,
68. (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step);
69.
70. //得到左肩联合点
71. Joint ShoulderLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ShoulderLeft);
72. //画左肩和肩中的连线
73. gra.DrawLine(pen, (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step,
74. (ShoulderLeftJoint.Position.X + 1) * step + Distance, (1 - ShoulderLeftJoint.Position.Y) * step);
75.
76. //得到右肩联合点
77. Joint ShoulderRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ShoulderRight);
78. //画右肩和肩中的连线
79. gra.DrawLine(pen, (ShoulderRightJoint.Position.X + 1) * step + Distance, (1 - ShoulderRightJoint.Position.Y) * step,
80. (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step);
81.
82. //得到左肘联合点
83. Joint ElbowLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ElbowLeft);
84. //画左肩和左肘的连线
85. gra.DrawLine(pen, (ShoulderLeftJoint.Position.X + 1) * step + Distance, (1 - ShoulderLeftJoint.Position.Y) * step,
86. (ElbowLeftJoint.Position.X + 1) * step + Distance, (1 - ElbowLeftJoint.Position.Y) * step);
87. //得到右肘联合点
88. Joint ElbowRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.ElbowRight);
89. //画右肩和右肘的连线
90. gra.DrawLine(pen, (ShoulderRightJoint.Position.X + 1) * step + Distance, (1 - ShoulderRightJoint.Position.Y) * step,
91. (ElbowRightJoint.Position.X + 1) * step + Distance, (1 - ElbowRightJoint.Position.Y) * step);
92.
93. //得到左腕联合点
94. Joint WristLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.WristLeft);
95. //画左肘和左腕的连线
96. gra.DrawLine(pen, (WristLeftJoint.Position.X + 1) * step + Distance, (1 - WristLeftJoint.Position.Y) * step,
97. (ElbowLeftJoint.Position.X + 1) * step + Distance, (1 - ElbowLeftJoint.Position.Y) * step);
98.
99. //得到右腕联合点
100. Joint WristRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.WristRight);
101. //画右肘和右腕的连线
102. gra.DrawLine(pen, (WristRightJoint.Position.X + 1) * step + Distance, (1 - WristRightJoint.Position.Y) * step,
103. (ElbowRightJoint.Position.X + 1) * step + Distance, (1 - ElbowRightJoint.Position.Y) * step);
104.
105. //得到左手联合点
106. Joint HandLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HandLeft);
107. //画左手和左腕的连线
108. gra.DrawLine(pen, (WristLeftJoint.Position.X + 1) * step + Distance, (1 - WristLeftJoint.Position.Y) * step,
109. (HandLeftJoint.Position.X + 1) * step + Distance, (1 - HandLeftJoint.Position.Y) * step);
110.
111. //得到右手联合点
112. Joint HandRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HandRight);
113. //画右手和右腕的连线
114. gra.DrawLine(pen, (WristRightJoint.Position.X + 1) * step + Distance, (1 - WristRightJoint.Position.Y) * step,
115. (HandRightJoint.Position.X + 1) * step + Distance, (1 - HandRightJoint.Position.Y) * step);
116.
117. //得到脊柱联合点
118. Joint SpineJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.Spine);
119. //画脊柱和肩中的连线
120. gra.DrawLine(pen, (SpineJoint.Position.X + 1) * step + Distance, (1 - SpineJoint.Position.Y) * step,
121. (ShoulderCenterJoint.Position.X + 1) * step + Distance, (1 - ShoulderCenterJoint.Position.Y) * step);
122.
123. //得到髋中联合点
124. Joint HipCenterJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HipCenter);
125. //画脊柱和髋中的连线
126. gra.DrawLine(pen, (SpineJoint.Position.X + 1) * step + Distance, (1 - SpineJoint.Position.Y) * step,
127. (HipCenterJoint.Position.X + 1) * step + Distance, (1 - HipCenterJoint.Position.Y) * step);
128.
129. //得到左髋联合点
130. Joint HipLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HipLeft);
131. //画左髋和髋中的连线
132. gra.DrawLine(pen, (HipLeftJoint.Position.X + 1) * step + Distance, (1 - HipLeftJoint.Position.Y) * step,
133. (HipCenterJoint.Position.X + 1) * step + Distance, (1 - HipCenterJoint.Position.Y) * step);
134.
135. //得到右髋联合点
136. Joint HipRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.HipRight);
137. //画右髋和髋中的连线
138. gra.DrawLine(pen, (HipRightJoint.Position.X + 1) * step + Distance, (1 - HipRightJoint.Position.Y) * step,
139. (HipCenterJoint.Position.X + 1) * step + Distance, (1 - HipCenterJoint.Position.Y) * step);
140.
141. //得到左膝联合点
142. Joint KneeLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.KneeLeft);
143. //画左髋和左膝的连线
144. gra.DrawLine(pen, (HipLeftJoint.Position.X + 1) * step + Distance, (1 - HipLeftJoint.Position.Y) * step,
145. (KneeLeftJoint.Position.X + 1) * step + Distance, (1 - KneeLeftJoint.Position.Y) * step);
146.
147. //得到右膝联合点
148. Joint KneeRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.KneeRight);
149. //画右髋和右膝的连线
150. gra.DrawLine(pen, (HipRightJoint.Position.X + 1) * step + Distance, (1 - HipRightJoint.Position.Y) * step,
151. (KneeRightJoint.Position.X + 1) * step + Distance, (1 - KneeRightJoint.Position.Y) * step);
152.
153. //得到左踝联合点
154. Joint AnkleLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.AnkleLeft);
155. //画左踝和左膝的连线
156. gra.DrawLine(pen, (AnkleLeftJoint.Position.X + 1) * step + Distance, (1 - AnkleLeftJoint.Position.Y) * step,
157. (KneeLeftJoint.Position.X + 1) * step + Distance, (1 - KneeLeftJoint.Position.Y) * step);
158.
159. //得到右踝联合点
160. Joint AnkleRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.AnkleRight);
161. //画右踝和右膝的连线
162. gra.DrawLine(pen, (AnkleRightJoint.Position.X + 1) * step + Distance, (1 - AnkleRightJoint.Position.Y) * step,
163. (KneeRightJoint.Position.X + 1) * step + Distance, (1 - KneeRightJoint.Position.Y) * step);
164.
165.
166. //得到左脚联合点
167. Joint FootLeftJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.FootLeft);
168. //画左踝和左脚的连线
169. gra.DrawLine(pen, (AnkleLeftJoint.Position.X + 1) * step + Distance, (1 - AnkleLeftJoint.Position.Y) * step,
170. (FootLeftJoint.Position.X + 1) * step + Distance, (1 - FootLeftJoint.Position.Y) * step);
171.
172. //得到右脚联合点
173. Joint FootRightJoint = skeletondata.Joints.SingleOrDefault(joi => joi.JointType == JointType.FootRight);
174. //画右踝和右脚的连线
175. gra.DrawLine(pen, (AnkleRightJoint.Position.X + 1) * step + Distance, (1 - AnkleRightJoint.Position.Y) * step,
176. (FootRightJoint.Position.X + 1) * step + Distance, (1 - FootRightJoint.Position.Y) * step);
177. }
178. }
179. #endregion
180. //下一个人的位置往右偏200个像素
181. Distance += 200;
182. }
183. }
184. }
185. }
186. private void Form1_FormClosing(object
187. {
188. if
189. {
190. //结束Kinect采集工作
191. "Kinect结束工作!");
192. }
193. }
效果如下:
我们看到屏幕上有6个蓝色的点,代表可识别6个人,其中有两个完整的人体骨骼,每个完整的人是有20个关节点的坐标。