Kinect SDK 2.0:如何追踪一个body
Kinect SDK 2.0: how to track one body
我正在使用 Kinect SDK 2.0,我只想跟踪一个骨骼。
如何实现?
我已经在 HD Face Basics 示例中尝试了 FindClosestSkeleton
方法,但是那阻止了我的程序更新每一帧,我并不完全理解它。
有人可以解释并告诉我怎么做吗?
以下代码基于 BodyBasics-WPF 示例。
public class KinectManager
{
// Active Kinect sensor
private KinectSensor kinectSensor = null;
// Reader for body frames
private BodyFrameReader bodyFrameReader = null;
// Array for the bodies
private Body[] bodies = null;
// index for the currently tracked body
private int bodyIndex;
// flag to asses if a body is currently tracked
private bool bodyTracked = false;
public KinectManager()
{
this.kinectSensor = KinectSensor.GetDefault();
// open the reader for the body frames
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
// open the sensor
this.kinectSensor.Open();
this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived;
}
// Handles the body frame data arriving from the sensor
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
bodyFrame.GetAndRefreshBodyData(this.bodies);
dataReceived = true;
}
}
if (dataReceived)
{
Body body = null;
if(this.bodyTracked) {
if(this.bodies[this.bodyIndex].IsTracked) {
body = this.bodies[this.bodyIndex];
} else {
bodyTracked = false;
}
}
if(!bodyTracked) {
for (int i=0; i<this.bodies.Length; ++i)
{
if(this.bodies[i].IsTracked) {
this.bodyIndex = i;
this.bodyTracked = true;
break;
}
}
}
if (body != null && this.bodyTracked && body.IsTracked)
{
// body represents your single tracked skeleton
}
}
}
}
我正在使用 Kinect SDK 2.0,我只想跟踪一个骨骼。
如何实现?
我已经在 HD Face Basics 示例中尝试了 FindClosestSkeleton
方法,但是那阻止了我的程序更新每一帧,我并不完全理解它。
有人可以解释并告诉我怎么做吗?
以下代码基于 BodyBasics-WPF 示例。
public class KinectManager
{
// Active Kinect sensor
private KinectSensor kinectSensor = null;
// Reader for body frames
private BodyFrameReader bodyFrameReader = null;
// Array for the bodies
private Body[] bodies = null;
// index for the currently tracked body
private int bodyIndex;
// flag to asses if a body is currently tracked
private bool bodyTracked = false;
public KinectManager()
{
this.kinectSensor = KinectSensor.GetDefault();
// open the reader for the body frames
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
// open the sensor
this.kinectSensor.Open();
this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived;
}
// Handles the body frame data arriving from the sensor
private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
bool dataReceived = false;
using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
if (this.bodies == null)
{
this.bodies = new Body[bodyFrame.BodyCount];
}
bodyFrame.GetAndRefreshBodyData(this.bodies);
dataReceived = true;
}
}
if (dataReceived)
{
Body body = null;
if(this.bodyTracked) {
if(this.bodies[this.bodyIndex].IsTracked) {
body = this.bodies[this.bodyIndex];
} else {
bodyTracked = false;
}
}
if(!bodyTracked) {
for (int i=0; i<this.bodies.Length; ++i)
{
if(this.bodies[i].IsTracked) {
this.bodyIndex = i;
this.bodyTracked = true;
break;
}
}
}
if (body != null && this.bodyTracked && body.IsTracked)
{
// body represents your single tracked skeleton
}
}
}
}