为什么使用Kinect SDK 1.8不能跟踪Kinect的手部关节?
本文关键字:Kinect 手部 跟踪 SDK 为什么 不能 | 更新日期: 2023-09-27 18:08:05
我已经在这方面工作了很长一段时间了,我找不到为什么(大多数时候)手关节没有被跟踪。代码经过调整以避免发生这种情况(这就是为什么这里有所有的空值和可空值)。我使用c#和Kinect SDK v1.8。
我以前从来没有在这里发过,所以每条评论都很感谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;
using System.Windows;
namespace Arcade_Kinect.Kinect
{
class KinectAddons
{
public KinectSensor kinect;
public Point? punto;
public JointType hand;
public KinectAddons(bool leftHanded, KinectSensor sensor)
{
this.kinect = sensor;
if (kinect != null)
{
kinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinect_SkeletonFrameReady);
kinect.Start();
try
{
kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
kinect.SkeletonStream.Enable();
try
{
kinect.DepthStream.Range = DepthRange.Near;
kinect.SkeletonStream.EnableTrackingInNearRange = true;
}
catch (InvalidOperationException)
{
kinect.DepthStream.Range = DepthRange.Default;
kinect.SkeletonStream.EnableTrackingInNearRange = false;
}
}
catch (InvalidOperationException) { }
}
if (leftHanded)
this.hand = JointType.HandLeft;
else
this.hand = JointType.HandRight;
}
void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
Skeleton[] skeleton = new Skeleton[0];
using (SkeletonFrame sf = e.OpenSkeletonFrame())
{
if (sf != null)
{
skeleton = new Skeleton[sf.SkeletonArrayLength];
sf.CopySkeletonDataTo(skeleton);
punto = returnPosFromHand(skeleton[0]);
}
}
}
//saves x and y of the hand
public Point? returnPosFromHand(Skeleton sk)
{
DepthImagePoint depthPoint = new DepthImagePoint();
bool notWorking = true;
try
{
if (sk.Joints[this.hand].TrackingState == JointTrackingState.Tracked)
{
notWorking = false;
depthPoint = this.kinect.CoordinateMapper.MapSkeletonPointToDepthPoint(sk.Joints[this.hand].Position, DepthImageFormat.Resolution640x480Fps30);
}
}
catch (IndexOutOfRangeException)
{
System.Console.WriteLine("Hand capture was lost" + this.hand.ToString());
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
if (notWorking)
return null;
return new Point(depthPoint.X, depthPoint.Y);
}
//returns last know value
public double? readNext()
{
if (this.punto.HasValue)
return punto.Value.X;
return null;
}
}
}
问题
你的问题是获取骨架从骷髅框架
SkeletonFrame.OpenSkeletonFrame()
总是返回一个包含X-objects的数组。被跟踪的人不一定是数组中的第一个Skeleton。
这就是为什么你的手有时会被跟踪。有时是当跟踪骨架在列表中位于首位时(returnPosFromHand(skeleton[0]);
)
遍历骨架数组并检查跟踪的是哪个骨架并使用那个骨架。你可以按z指数排序,也许可以选择最接近传感器的。然后把这个骨架传递给你的函数。