KOSAKA LABORATORY->Tips

Kinect SDK XNAで骨格情報の取得 その1

kinect.jpg

Kinect for Windows SDKでスケルトン情報、骨格情報の取得を行いましょう。
今回は、純粋に各骨格情報の座標だけを出力するプログラムです。
ソース
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Research.Kinect.Nui;  //Kinect Uniの読み込み

namespace WindowsGame6
{
  /// <summary>
  /// 基底 Game クラスから派生した、ゲームのメイン クラスです。
  /// </summary>
  public class Game1 : Microsoft.Xna.Framework.Game
  {
    GraphicsDeviceManager graphics;

    Runtime nui;  //Kinectのセンサクラス

    public Game1(){
      graphics = new GraphicsDeviceManager(this);
      Content.RootDirectory = "Content";
    }

    protected override void Initialize(){

      base.Initialize();

      nui = new Runtime();  //Kinectセンサクラスの初期化

      try{
        //奥行の取得、トラッキング、実画像
        nui.Initialize(  RuntimeOptions.UseDepthAndPlayerIndex |
                RuntimeOptions.UseSkeletalTracking |
                RuntimeOptions.UseColor);
  
      }
      catch (InvalidOperationException){
        Console.WriteLine("Runtime initialization failed.");
        return;
      }

      //フレーム更新毎にnui_SkeletonFrameReadyを呼び出す
      nui.SkeletonFrameReady += 
        new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);

    }

    protected override void LoadContent(){

    }

    protected override void UnloadContent(){
    }

    protected override void Update(GameTime gameTime){
      if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();
      base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime){
      GraphicsDevice.Clear(Color.CornflowerBlue);
      base.Draw(gameTime);
    }


    #region スケルトン
    void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e){
      SkeletonFrame skeletonFrame = e.SkeletonFrame;

      Console.WriteLine("----------------------------------------------");
      
      foreach (SkeletonData data in skeletonFrame.Skeletons)
      {
        if (SkeletonTrackingState.Tracked == data.TrackingState)
        {

          foreach (Joint joint in data.Joints)
          {
            Console.WriteLine(  joint.ID     +"\t\t\t" +
                      joint.Position.X +"\t"+
                      joint.Position.Y +"\t"+
                      joint.Position.Z);
          }
        }
      }
    }

    #endregion

  }
}
実行結果
kinect.jpg
解説
OpenNIの時と比べると関節が増えて、計20点の骨格情報を取得することができます。 各骨格情報は、公式のProgramingGuide_KinectSDK.pdfを参考にすると、以下の図のようになります。
kinect.jpg