0

I want my SkeletonFrames avaible in my click event. Is there anyway to make it possible?

I was planning to create a Kinect to place image on skeleton joints. Here's my coding.

private void button1_Click(object sender, RoutedEventArgs e)
{
    foreach (SkeletonData data in e.SkeletonFrame.Skeletons)
    {
        foreach (Joint joint in data.Joints)
        {
            if (joint.Position.W < 0.6f) return;
            switch (joint.ID)
            {
                case JointID.Head:
                    var heanp = getDisplayPosition(joint);

                    Canvas.SetLeft(imgHead, heanp.X);
                    Canvas.SetTop(imgHead, heanp.Y);
                    break;
            }
        }
    }
}

If there's any mistakes, please do correct me.

4

1 回答 1

1

您可以捕获 SkeletonFrameReady 事件并将最后捕获的骨架保存到全局变量中,您可以从按钮单击事件中访问该变量。

例如:

private Skeleton[] skeletons = new Skeleton[0];

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);
        }
    }
}

现在您有了最后一个 SkeletonFrameReady 事件的数组(骨架)。

根据下面的评论,您需要升级到 SDK 的 v1.6 并查看 Microsoft 提供的示例。尽管他们可能不会完全按照您的当前项目所需做,但他们会教您使用 Microsoft Kinect for Windows 的基本基础知识,并让您了解如何使用传感器。

对于您的当前情况,您最想查看的示例是SkeletonBasics-WPF示例。这将向您展示如何跟踪骨架并从中提取数据。它还向您展示了如何在跟踪点上以及在跟踪点之间绘制项目。

对于您的情况,最相关的部分是DrawBonesAndJoints函数:

private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
{
    // Render Torso
    this.DrawBone(skeleton, drawingContext, JointType.Head, JointType.ShoulderCenter);
    this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderLeft);
    this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderRight);
    this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.Spine);
    this.DrawBone(skeleton, drawingContext, JointType.Spine, JointType.HipCenter);
    this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipLeft);
    this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipRight);

    // Left Arm
    this.DrawBone(skeleton, drawingContext, JointType.ShoulderLeft, JointType.ElbowLeft);
    this.DrawBone(skeleton, drawingContext, JointType.ElbowLeft, JointType.WristLeft);
    this.DrawBone(skeleton, drawingContext, JointType.WristLeft, JointType.HandLeft);

    // Right Arm
    this.DrawBone(skeleton, drawingContext, JointType.ShoulderRight, JointType.ElbowRight);
    this.DrawBone(skeleton, drawingContext, JointType.ElbowRight, JointType.WristRight);
    this.DrawBone(skeleton, drawingContext, JointType.WristRight, JointType.HandRight);

    // Left Leg
    this.DrawBone(skeleton, drawingContext, JointType.HipLeft, JointType.KneeLeft);
    this.DrawBone(skeleton, drawingContext, JointType.KneeLeft, JointType.AnkleLeft);
    this.DrawBone(skeleton, drawingContext, JointType.AnkleLeft, JointType.FootLeft);

    // Right Leg
    this.DrawBone(skeleton, drawingContext, JointType.HipRight, JointType.KneeRight);
    this.DrawBone(skeleton, drawingContext, JointType.KneeRight, JointType.AnkleRight);
    this.DrawBone(skeleton, drawingContext, JointType.AnkleRight, JointType.FootRight);

    // Render Joints
    foreach (Joint joint in skeleton.Joints)
    {
        Brush drawBrush = null;

        if (joint.TrackingState == JointTrackingState.Tracked)
        {
            drawBrush = this.trackedJointBrush;                    
        }
        else if (joint.TrackingState == JointTrackingState.Inferred)
        {
            drawBrush = this.inferredJointBrush;                    
        }

        if (drawBrush != null)
        {
            drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);
        }
    }
}

因为您不关心骨骼,所以可以删除绘制它们的函数调用:

private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
{
    // Render Joints
    foreach (Joint joint in skeleton.Joints)
    {
        Brush drawBrush = null;

        if (joint.TrackingState == JointTrackingState.Tracked)
        {
            drawBrush = this.trackedJointBrush;                    
        }
        else if (joint.TrackingState == JointTrackingState.Inferred)
        {
            drawBrush = this.inferredJointBrush;                    
        }

        if (drawBrush != null)
        {
            drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);
        }
    }
}

现在你有一个程序可以在所有关节上画点。如果你想要的只是双手...

private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
{
    drawingContext.DrawEllipse(trackedJointBrush, null, SkeletonPointToScreen(skeleton.Joints[JointType.HandLeft].Position), JointThickness, JointThickness);
    drawingContext.DrawEllipse(trackedJointBrush, null, SkeletonPointToScreen(skeleton.Joints[JointType.HandRight].Position), JointThickness, JointThickness);
}

现在你的手周围会有两个小点。

于 2012-10-18T17:43:51.307 回答