Kinect传感器(Windows)无法跟踪骨架关节

时间:2014-02-21 15:09:08

标签: c# kinect kinect-sdk

到目前为止,这是我的代码。这是非常基础的,因为我正在学习如何使用Kinect进行编码。

    KinectSensor Sensor = null;

    private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        Skeleton[] skeletons = new Skeleton[0];

        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
        }

        // Draw the skeleton.
        if (skeletons.Length > 0)
        {
            drawSkeleton(skeletons[0]);
        }
    }

    private void drawSkeleton(Skeleton skeleton)
    {
        // Dispose of the current image if applicable.
        if (pctSkeleton.Image != null)
        {
            pctSkeleton.Image.Dispose();
        }

        Image image = Image.FromFile(CanvasPath);

        using (Graphics g = Graphics.FromImage(image))
        {
            // If any joints aren't tracked, return.
            foreach (Joint joint in skeleton.Joints)
            {
                if (joint.TrackingState == JointTrackingState.NotTracked)
                {
                    return;
                }
            }

            // Sort the 20 joints.
            Joint head = skeleton.Joints[JointType.Head];
            Joint hipCenter = skeleton.Joints[JointType.HipCenter];
            Joint spine = skeleton.Joints[JointType.Spine];
            Joint shoulderCenter = skeleton.Joints[JointType.ShoulderCenter];
            Joint shoulderLeft = skeleton.Joints[JointType.ShoulderLeft];
            Joint elbowLeft = skeleton.Joints[JointType.ElbowLeft];
            Joint wristLeft = skeleton.Joints[JointType.WristLeft];
            Joint handLeft = skeleton.Joints[JointType.HandLeft];
            Joint shoulderRight = skeleton.Joints[JointType.ShoulderRight];
            Joint elbowRight = skeleton.Joints[JointType.ElbowRight];
            Joint wristRight = skeleton.Joints[JointType.WristRight];
            Joint handRight = skeleton.Joints[JointType.HandRight];
            Joint hipLeft = skeleton.Joints[JointType.HipLeft];
            Joint kneeLeft = skeleton.Joints[JointType.KneeLeft];
            Joint ankleLeft = skeleton.Joints[JointType.AnkleLeft];
            Joint footLeft = skeleton.Joints[JointType.FootLeft];
            Joint hipRight = skeleton.Joints[JointType.HipRight];
            Joint kneeRight = skeleton.Joints[JointType.KneeRight];
            Joint ankleRight = skeleton.Joints[JointType.AnkleRight];
            Joint footRight = skeleton.Joints[JointType.FootRight];

            Pen inBoundPen = new Pen(Brushes.Green, 3);

            ///////////////////DRAW BONES////////////////////////
            // head => shoulder center.
            g.DrawLine(inBoundPen, convertX(head.Position.X), convertY(head.Position.Y), convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y));

            // shoulders => shoulder center.
            g.DrawLine(inBoundPen, convertX(shoulderRight.Position.X), convertY(shoulderRight.Position.Y), convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y));
            g.DrawLine(inBoundPen, convertX(shoulderLeft.Position.X), convertY(shoulderLeft.Position.Y), convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y));

            // shoulder center => spine.
            g.DrawLine(inBoundPen, convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y), convertX(spine.Position.X), convertY(spine.Position.Y));

            // shoulder right => elbow right
            g.DrawLine(inBoundPen, convertX(shoulderRight.Position.X), convertY(shoulderRight.Position.Y), convertX(elbowRight.Position.X), convertY(elbowRight.Position.Y));

            // elbow right => wrist right
            g.DrawLine(inBoundPen, convertX(elbowRight.Position.X), convertY(elbowRight.Position.Y), convertX(wristRight.Position.X), convertY(wristRight.Position.Y));

            // wrist right => hand right
            g.DrawLine(inBoundPen, convertX(wristRight.Position.X), convertY(wristRight.Position.Y), convertX(handRight.Position.X), convertY(handRight.Position.Y));

            // shoulder left => elbow left
            g.DrawLine(inBoundPen, convertX(shoulderLeft.Position.X), convertY(shoulderLeft.Position.Y), convertX(elbowLeft.Position.X), convertY(elbowLeft.Position.Y));

            // elbow left => wrist left
            g.DrawLine(inBoundPen, convertX(elbowLeft.Position.X), convertY(elbowLeft.Position.Y), convertX(wristLeft.Position.X), convertY(wristLeft.Position.Y));

            // wrist left => hand left
            g.DrawLine(inBoundPen, convertX(wristLeft.Position.X), convertY(wristLeft.Position.Y), convertX(handLeft.Position.X), convertY(handLeft.Position.Y));

            // spine => hip center.
            g.DrawLine(inBoundPen, convertX(spine.Position.X), convertY(spine.Position.Y), convertX(hipCenter.Position.X), convertY(hipCenter.Position.Y));

            // hips => hip center.
            g.DrawLine(inBoundPen, convertX(hipCenter.Position.X), convertY(hipCenter.Position.Y), convertX(hipLeft.Position.X), convertY(hipLeft.Position.Y));
            g.DrawLine(inBoundPen, convertX(hipCenter.Position.X), convertY(hipCenter.Position.Y), convertX(hipRight.Position.X), convertY(hipRight.Position.Y));

            // hip left => knee left.
            g.DrawLine(inBoundPen, convertX(hipLeft.Position.X), convertY(hipLeft.Position.Y), convertX(kneeLeft.Position.X), convertY(kneeLeft.Position.Y));

            // knee left => ankle left.
            g.DrawLine(inBoundPen, convertX(kneeLeft.Position.X), convertY(kneeLeft.Position.Y), convertX(ankleLeft.Position.X), convertY(ankleLeft.Position.Y));

            // ankle left => foot left.
            g.DrawLine(inBoundPen, convertX(ankleLeft.Position.X), convertY(ankleLeft.Position.Y), convertX(footLeft.Position.X), convertY(footLeft.Position.Y));

            // hip right => knee right.
            g.DrawLine(inBoundPen, convertX(hipRight.Position.X), convertY(hipRight.Position.Y), convertX(kneeRight.Position.X), convertY(kneeRight.Position.Y));

            // knee right => ankle right.
            g.DrawLine(inBoundPen, convertX(kneeRight.Position.X), convertY(kneeRight.Position.Y), convertX(ankleRight.Position.X), convertY(ankleRight.Position.Y));

            // ankle right => foot right.
            g.DrawLine(inBoundPen, convertX(ankleRight.Position.X), convertY(ankleRight.Position.Y), convertX(footRight.Position.X), convertY(footRight.Position.Y));
            ////////////////////////////////////////////////

            pctSkeleton.Image = image;
        }
    }

    private float convertX(float x)
    {
        return (pctSkeleton.Width / 2) + (x * 100);
    }

    private float convertY(float y)
    {
        return (pctSkeleton.Height / 2) - (y * 100);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // Get the first sensor
        Sensor = KinectSensor.KinectSensors[0];

        // Enable the skeleton and video streams.
        Sensor.SkeletonStream.Enable();
        Sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);

        // Set the event handlers
        Sensor.SkeletonFrameReady += SensorSkeletonFrameReady;
        Sensor.ColorFrameReady += SensorColorFrameReady;

        // Start the kinect.
        Sensor.Start();
    }

出于某种原因,传感器大多数时间都没有跟踪骨架。然而,它在大约20%的时间内起作用。设置断点时,它会显示未跟踪的关节。我是新手,所以任何关于我做错的信息都会非常感激。

1 个答案:

答案 0 :(得分:1)

当跟踪所有20个关节时,您的应用程序仅绘制骨架。如果没有跟踪单个关节(例如左脚踝),则您的应用程序不执行任何操作。传感器可以检测到每个关节是非常罕见的。

kinect developer toolkit中,您可以找到名为“SkeletonBasics-WPF”的样本,在未跟踪关节时更宽容。