ホームに戻る
 スケルトン

0、はじめに

Kinectから80cm以上後ろに下がること。
右手と左手の位置をキャプチャして点で表示する。
位置の深さによって点の色が変わる。

1、WPF

<Window x:Class="KinectTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="KinectTest" Height="200" Width="300"
        HorizontalContentAlignment="Center" Closing="Window_Closing">
    <Grid>
        <Image Name="image1" Stretch="Uniform"/>
    </Grid>
</Window>

2、コード

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;

namespace KinectTest{
  public partial class MainWindow : Window{
    readonly int BytesPerPixel = 4;
    readonly int Image_Width = 100;
    readonly int Image_Height = 100;

    public MainWindow(){
      try{
        InitializeComponent();

        if(KinectSensor.KinectSensors.Count == 0){
          throw new Exception("Connect Kinect.");
        }

        StartKinect(KinectSensor.KinectSensors[0]);
      }
      catch(Exception ex){
        MessageBox.Show(ex.Message);
        Close();
      }
    }

    private void StartKinect(KinectSensor kinect){
      kinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinect_SkeletonFrameReady);
      kinect.SkeletonStream.Enable();
      kinect.Start();
    }

    private void StopKinect(KinectSensor kinect){
      if(kinect != null){
        if(kinect.IsRunning){
          kinect.SkeletonFrameReady -= kinect_SkeletonFrameReady;
          kinect.Stop();
          kinect.Dispose();
        }
      }
    }

    void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e){
      try{
        KinectSensor kinect = sender as KinectSensor;

        if(kinect == null){
          return;
        }

        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()){
          if(skeletonFrame != null){
            byte[] bufferImage = new byte[Image_Width * Image_Height * BytesPerPixel];

            Skeleton[] skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
            skeletonFrame.CopySkeletonDataTo(skeletons);

            foreach(Skeleton skeleton in skeletons){
              if(skeleton.TrackingState == SkeletonTrackingState.Tracked){
                foreach(Joint joint in skeleton.Joints){
                  // AnkleLeft
                  // AnkleRight
                  // ElbowLeft
                  // ElbowRight
                  // FootLeft
                  // FootRight
                  // HandLeft
                  // HandRight
                  // Head
                  // HipCenter
                  // HipLeft
                  // HipRight
                  // KneeLeft
                  // KneeRight
                  // ShoulderCenter
                  // ShoulderLeft
                  // ShoulderRight
                  // Spine
                  // WristLeft
                  // WristRight
                  if(joint.JointType == JointType.HandRight || joint.JointType == JointType.HandLeft){
                    if(joint.TrackingState == JointTrackingState.Tracked){
                      int x = (int)(((joint.Position.X + 1.0) / 2.0) * (double)Image_Width);
                      int y = (int)(((-joint.Position.Y + 1.0) / 2.0) * (double)Image_Height);
                      int c = (int)((-joint.Position.Z / 4.0) * 255.0);

                      bufferImage[(y * Image_Width + x) * BytesPerPixel] = 0;
                      bufferImage[(y * Image_Width + x) * BytesPerPixel + 1] = (byte)c;
                      bufferImage[(y * Image_Width + x) * BytesPerPixel + 2] = 255;
                    }
                  }
                }
              }
            }

            BitmapSource bitmap = BitmapSource.Create(Image_Width, Image_Height, 96, 96, PixelFormats.Bgr32, null, bufferImage, Image_Width * BytesPerPixel);

            image1.Source = bitmap;
          }
        }
      }
      catch(Exception ex){
        MessageBox.Show(ex.Message);
      }
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e){
      StopKinect(KinectSensor.KinectSensors[0]);
    }
  }
}

inserted by FC2 system