【问题标题】:Mapping skeleton joints to create a skeleton映射骨架关节以创建骨架
【发布时间】:2014-12-28 23:32:16
【问题描述】:

我已经研究了好几天了,但没有成功,我认为其他问题是堆栈溢出是我的来源之一。 我正在使用 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

【问题讨论】:

    标签: c# kinect


    【解决方案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);
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

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

      jointTo 也是如此……

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

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

      【讨论】:

        【解决方案3】:

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

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

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

        这里是“骨架基础”示例的MainWindow.xaml.cs。记下 DrawBonesAndJoints' andDrawBone' 函数来演示您想要做什么。

        【讨论】:

          【解决方案4】:

          也许我没有理解正确,但是你为什么要做已经做过的事情呢?这是来自 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));
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-05
            • 1970-01-01
            • 2011-08-04
            • 2015-07-27
            相关资源
            最近更新 更多