2014-02-21 2 views
0

Вот мой код. Это очень просто, поскольку я просто изучаю код с Kinect.Kinect Sensor (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», который более прощает, когда соединение не отслеживается.

Смежные вопросы