kinect2.0的骨骼追踪相比较上一代算是有了大幅度提升,最多可以同时追踪6个人身上的25个骨骼节点(图为v1和v2的对比)
其实单纯的获取骨骼节点的坐标还是很简单的,我就讲一下在控制台程序中实时的输出骨骼坐标的方法吧
当然 只要需要调用kinect 第一步肯定是添加引用
因为所声明的变量在获取骨骼的监听事件中还要调用 ,所以需要声明为全局静态变量,首先还是要声明个kinectsensor
public static KinectSensor kinect; public static BodyFrameReader bodyframereader; public static Body[] bodies = null;变量声明完,下一步就应该获取并打开传感器了
kinect = KinectSensor.GetDefault(); kinect.Open();然后开始读入数据
bodyframereader = kinect.BodyFrameSource.OpenReader(); if (bodyframereader != null) { bodyframereader.FrameArrived += Bodyframereader_FrameArrived; }
private static void Bodyframereader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) { bool data = false; using (BodyFrame bodyframe = e.FrameReference.AcquireFrame()) { if (bodyframe != null) { if (bodies == null) { bodies = new Body[bodyframe.BodyCount]; } } bodyframe.GetAndRefreshBodyData(bodies); data = true; } if (data) { foreach (Body body in bodies) { if (body.IsTracked) { IReadOnlyDictionary<JointType, Joint> joints = body.Joints; CameraSpacePoint position = joints[JointType.HandRight].Position; Console.WriteLine(position.X.ToString() + "," + position.Y.ToString() + "," + position.Z.ToString()); CameraSpacePoint position2 = joints[JointType.ShoulderRight].Position; Console.WriteLine(position2.X.ToString() + "," + position2.Y.ToString() + "," + position2.Z.ToString()); Console.WriteLine(); } } } }
插上kinect运行就可以看到输出了
在此基础上想要得到handstate也就是手势 也就是几行代码了
类似 在
if (body.IsTracked)的大括号下添加
if (body.HandRightState == HandState.Closed) Console.WriteLine("close\n\n"); if (body.HandRightState == HandState.Open) Console.WriteLine("open\n\n"); if (body.HandRightState == HandState.Lasso) Console.WriteLine("lesso\n\n");
t同时把上面的输出给注释掉(全选后ctrl+k+c可以快速注释 ,ctrl+k+u取消注释)
运行后对着kinect把右手合上张开就可以看到输出了,还有那个lesso ,完全合上是close 完全张开是open 其他情况就都是lesso了
附上全部handstate