c# – 为什么不使用Kinect SDK 1.8跟踪Kinect手部关节

我已经在这方面工作了很长一段时间,我找不到为什么(大多数时候)没有跟踪HandJoint.代码适用于避免发生这种情况时(这就是为什么所有的空值和空值都在那里).我使用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中获取你的骨架

SkeletonFrame.OpenSkeletonFrame()始终返回带有X对象的数组.
跟踪的人不一定是阵列中的第一个Skeleton.

这就是为什么你有时会得到你的手跟踪.有时是跟踪骨架是列表中的第一个(returnPosFromHand(skeleton [0]);)

迭代骨架数组并检查跟踪哪个骨架并使用该骨架.您可以按Z-index订购,也可以选择与传感器最接近的一个.然后将该骨架传递给您的函数.

点赞