2014-02-21 114 views
0

這是我的代碼到目前爲止。這是非常基本的,因爲我只是在學習如何使用Kinect進行編碼。Kinect傳感器(Windows)不跟蹤骨架關節

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

當所有20個關節都被跟蹤時,您的應用程序僅繪製骨架。如果只有一個關節沒有被跟蹤(例如左腳踝),你的應用程序什麼都不做。傳感器可以檢測到每個關節是非常罕見的。

kinect developer toolkit中,您可以找到一個名爲「SkeletonBasics-WPF」的示例,該示例在未跟蹤關節時更加寬容。