2014-09-03 36 views
0

我使用的是kinect。我的目標是存儲這些值給我身體位置(頭部,手部等)的動力。我寫了一些代碼,但我不明白應該保存什麼值,以及如何存儲。我想要在db或txt文件中存儲頭,手和腳的位置。我希望數據能夠理解例如,如果有人移動她的手,kinect會發送一個值。我必須將其存儲並理解並理解所採取的行動。請輸入以下信息:從kinect骨架軌跡保存的關節

這是我的代碼:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using Microsoft.Kinect; 
using System.Linq; 
using System.IO; 

namespace KinectSkeletonApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     //Instantiate the Kinect runtime. Required to initialize the device. 
     //IMPORTANT NOTE: You can pass the device ID here, in case more than one Kinect device is connected. 
     KinectSensor sensor = KinectSensor.KinectSensors[0]; 
     byte[] pixelData; 
     Skeleton[] skeletons; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      /////////////////////////////////// 


      /////////////////////////////// 


      //Runtime initialization is handled when the window is opened. When the window 
      //is closed, the runtime MUST be unitialized. 
      this.Loaded += new RoutedEventHandler(MainWindow_Loaded); 
      this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded); 

      sensor.ColorStream.Enable(); 
      sensor.SkeletonStream.Enable(); 
     } 

     void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) 
     { 
      bool receivedData = false; 

      using (SkeletonFrame SFrame = e.OpenSkeletonFrame()) 
      { 
       if (SFrame == null) 
       { 
        // The image processing took too long. More than 2 frames behind. 
       } 
       else 
       { 
        skeletons = new Skeleton[SFrame.SkeletonArrayLength]; 
        SFrame.CopySkeletonDataTo(skeletons); 
        receivedData = true; 
       } 
      } 

      if (receivedData) 
      { 

       Skeleton currentSkeleton = (from s in skeletons 
              where s.TrackingState == SkeletonTrackingState.Tracked 
              select s).FirstOrDefault(); 

       if (currentSkeleton != null) 
       { 
        SetEllipsePosition(head, currentSkeleton.Joints[JointType.Head]); 
        SetEllipsePosition(leftHand, currentSkeleton.Joints[JointType.HandLeft]); 
        SetEllipsePosition(rightHand, currentSkeleton.Joints[JointType.HandRight]); 
        SetEllipsePosition(shoulder_center, currentSkeleton.Joints[JointType.ShoulderCenter]); 

       } 
      } 
     } 


     //This method is used to position the ellipses on the canvas 
     //according to correct movements of the tracked joints. 

     //IMPORTANT NOTE: Code for vector scaling was imported from the Coding4Fun Kinect Toolkit 
     //available here: http://c4fkinect.codeplex.com/ 
     //I only used this part to avoid adding an extra reference. 
     private void SetEllipsePosition(Ellipse ellipse, Joint joint) 
     { 
      Microsoft.Kinect.SkeletonPoint vector = new Microsoft.Kinect.SkeletonPoint(); 
      vector.X = ScaleVector(640, joint.Position.X); 
      vector.Y = ScaleVector(480, -joint.Position.Y); 
      vector.Z = joint.Position.Z; 

      Joint updatedJoint = new Joint(); 
      updatedJoint = joint; 





      updatedJoint.TrackingState = JointTrackingState.Tracked; 
      updatedJoint.Position = vector; 

      Canvas.SetLeft(ellipse, updatedJoint.Position.X); 
      Canvas.SetTop(ellipse, updatedJoint.Position.Y); 
     } 

     private float ScaleVector(int length, float position) 
     { 
      float value = (((((float)length)/1f)/2f) * position) + (length/2); 
      if (value > length) 
      { 
       return (float)length; 
      } 
      if (value < 0f) 
      { 
       return 0f; 
      } 

      string r = Convert.ToString(value); 

      string path = @"C:\Test\MyTest.txt"; 
      // This text is added only once to the file. 
      if (!File.Exists(path)) 
      { 
       // Create a file to write to. 
       string createText = "Hello and Welcome" + Environment.NewLine; 
       File.WriteAllText(path, createText); 
      } 

      // This text is always added, making the file longer over time 
      // if it is not deleted. 
      //string appendText = "This is extra text" + Environment.NewLine; 
      File.AppendAllText(path, r); 

      // Open the file to read from. 
      string readText = File.ReadAllText(path); 


      return value; 
     } 

     void MainWindow_Unloaded(object sender, RoutedEventArgs e) 
     { 
      sensor.Stop(); 
     } 

     void MainWindow_Loaded(object sender, RoutedEventArgs e) 
     { 
      sensor.SkeletonFrameReady += runtime_SkeletonFrameReady; 
      sensor.ColorFrameReady += runtime_VideoFrameReady; 
      sensor.Start(); 
     } 

     void runtime_VideoFrameReady(object sender, ColorImageFrameReadyEventArgs e) 
     { 
      bool receivedData = false; 

      using (ColorImageFrame CFrame = e.OpenColorImageFrame()) 
      { 
       if (CFrame == null) 
       { 
        // The image processing took too long. More than 2 frames behind. 
       } 
       else 
       { 
        pixelData = new byte[CFrame.PixelDataLength]; 
        CFrame.CopyPixelDataTo(pixelData); 
        receivedData = true; 
       } 
      } 

      if (receivedData) 
      { 
       BitmapSource source = BitmapSource.Create(640, 480, 96, 96, 
         PixelFormats.Bgr32, null, pixelData, 640 * 4); 

       videoImage.Source = source; 
      } 
     } 
    } 
} 
+0

請說明您要完成,而不是給我們一個代碼轉儲,並期待我們理解您需要什麼幫助什麼的更多細節。你需要存儲的內容很大程度上取決於你想要對數據做什麼 – StormeHawke 2014-09-03 17:00:46

+0

我想要在數據庫或txt文件中存儲頭部,手和腳的位置。我希望數據能夠理解數據的運動站在kinect前面的人。例如,如果有人移動她的手,kinect會發送一個值。我必須存儲它並理解並理解所採取的行動。請留下少數信息。 – user3906418 2014-09-03 17:56:50

回答

0

我想你要找的是爲某些接頭獲得X,Y,Z座標嗎? 在這種情況下,添加以下代碼:

Vector3D ShoulderCenter = new Vector3D(skeleton.Joints[JointType.ShoulderCenter].Position.X, skeleton.Joints[JointType.ShoulderCenter].Position.Y, skeleton.Joints[JointType.ShoulderCenter].Position.Z); 
      Vector3D RightShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderRight].Position.X, skeleton.Joints[JointType.ShoulderRight].Position.Y, skeleton.Joints[JointType.ShoulderRight].Position.Z); 
      Vector3D LeftShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderLeft].Position.X, skeleton.Joints[JointType.ShoulderLeft].Position.Y, skeleton.Joints[JointType.ShoulderLeft].Position.Z); 
      Vector3D RightElbow = new Vector3D(skeleton.Joints[JointType.ElbowRight].Position.X, skeleton.Joints[JointType.ElbowRight].Position.Y, skeleton.Joints[JointType.ElbowRight].Position.Z); 
      Vector3D LeftElbow = new Vector3D(skeleton.Joints[JointType.ElbowLeft].Position.X, skeleton.Joints[JointType.ElbowLeft].Position.Y, skeleton.Joints[JointType.ElbowLeft].Position.Z); 
      Vector3D RightWrist = new Vector3D(skeleton.Joints[JointType.WristRight].Position.X, skeleton.Joints[JointType.WristRight].Position.Y, skeleton.Joints[JointType.WristRight].Position.Z); 
      Vector3D LeftWrist = new Vector3D(skeleton.Joints[JointType.WristLeft].Position.X, skeleton.Joints[JointType.WristLeft].Position.Y, skeleton.Joints[JointType.WristLeft].Position.Z); 

此只定義了向量的上身部分,你可以看到。按照我所做的方式添加缺失的關節。 您需要以下組件:

using System.Windows.Media; 
using Microsoft.Kinect.Toolkit.Fusion; 
using System.Windows.Media.Media3D;