0

我已经研究了好几天了,但没有成功,看看其他问题是堆栈溢出是我的来源之一。我正在使用 Kinect for Windows 创建一个程序,它将跟踪用户的关节并在单独的“屏幕”上绘制骨架,我已经映射了关节本身,并且我正在使用图像(红点)来演示位置映射的关节。

我的下一步是通过从一个关节到另一个关节画一条线来构建骨架本身,我遇到的问题是如何画线......我倾向于使事情过于复杂,所以我想知道我是否只是在制作对自己来说困难的事情,而不是仅仅找到最简单的解决方案。

我觉得我已经完成了 99% 的工作,但我仍然遇到一个我无法理解的错误,请在错误消息和相关的 C# 代码下方找到。

"cannot convert from 'Microsoft.Kinect.SkeletonPoint' to 'System.Windows.Point"

代码:

private void DrawBone (Joint jointFrom, Joint jointTo)
{
    Brush centerPointBrush;
    Pen trackedBonePen = new Pen(Brushes.White, TrackedBoneThickness);
    inferredBonePen = new Pen(Brushes.Gray, InferredBoneThickness);
    DrawingVisual visual = new DrawingVisual();
    DrawingContext context = visual.RenderOpen();

    centerPointBrush = Brushes.Red;

    if (jointFrom.TrackingState == JointTrackingState.NotTracked
        || jointTo.TrackingState == JointTrackingState.NotTracked)
    {
        return;
    }

    if (jointFrom.TrackingState == JointTrackingState.Inferred
        || jointTo.TrackingState == JointTrackingState.Inferred)
    {       
        context.DrawLine(inferredBonePen,jointFrom.Position, jointTo.Position);
    }

    if (jointFrom.TrackingState == JointTrackingState.Tracked
        || jointTo.TrackingState == JointTrackingState.Tracked)
    {
        context.DrawLine(trackedBonePen, jointFrom.Position, jointTo.Position);
    }
}

与开头的行相关的错误context.DrawLine,因为这些是DrawLine函数的不正确参数,我得到一个伴随的错误:

Error 4 The best overloaded method match for
'System.Windows.Media.DrawingContext.DrawLine(System.Windows.Media.Pen,
System.Windows.Point, System.Windows.Point)' has some invalid arguments
4

4 回答 4

1

不要重新发明轮子。微软的人在那里做得很好。也许这段代码可以帮助你:

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);
                }
            }
        }
于 2014-12-28T15:32:16.977 回答
0

也许我没有理解正确,但是你为什么要做已经做过的事情呢?这是来自 Kinect Developer Toolkit 1.8 的示例代码。

    private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
    {
        Joint joint0 = skeleton.Joints[jointType0];
        Joint joint1 = skeleton.Joints[jointType1];

        // If we can't find either of these joints, exit
        if (joint0.TrackingState == JointTrackingState.NotTracked || joint1.TrackingState == JointTrackingState.NotTracked)
        {
            return;
        }

        // Don't draw if both points are inferred
        if (joint0.TrackingState == JointTrackingState.Inferred && joint1.TrackingState == JointTrackingState.Inferred)
        {
            return;
        }

        // We assume all drawn bones are inferred unless BOTH joints are tracked
        Pen drawPen = this.inferredBonePen;

        if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
        {
            drawPen = this.trackedBonePen;
        }

        drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position), this.SkeletonPointToScreen(joint1.Position));
    }
于 2014-10-03T16:08:15.717 回答
0

正如 Miky Dinescu 指出的那样,您的直接问题是需要将 a 转换Microsoft.Kinect.Joint.Position为 a System.Windows.Point

您可能不关心Z值(深度),因此需要将X和标准化Y为您的窗口大小。根据您希望绘制的骨架的行为方式,这可能就像始终将 放置JointType.Spine在窗口的中心一样简单;或者您可能需要进行额外的数学运算,以便根据玩家在 Kinect 的 FOV 中的位置来转换骨架需要出现在窗口中的位置。

根据 Kinect 的输入绘制骨架在多个示例中进行了演示,这些示例可从Kinect for Windows Toolkit 获得。“Skeleton Basics”和“Shape Game”示例是立即浮现在脑海中的两个示例。您还可以仅在Kinect for Windows Samples CodePlex页面上找到代码(这些示例针对 SDK 1.6 进行编码,在 SDK 1.7 中都可以正常工作)。

这是“Skeleton Basics”示例的MainWindow.xaml.cs。记下DrawBonesAndJoints' andDrawBone 的函数以演示您想要做什么。

于 2013-03-28T15:12:00.553 回答
0

错误消息说明了一切。而不是jointFrom.Position直接使用,您需要创建一个System.Windows.Point来表示坐标jointFrom并将其传递给DrawLine

…… jointTo</p>

复杂之处在于骨架位置,因此fromJoint.Position表示 3D 坐标,而用于绘制线条的点是 2D 坐标。所以直接转换是不可能的。相反,您需要在 2D 中投影关节位置并使用投影点来绘制线。

您的另一个选择是在 WPF 中使用 3D 绘图,而不仅仅是绘制到视觉对象。

于 2013-03-28T12:11:01.437 回答