ホームに戻る
 深度

0、はじめに

Kinectからの距離に応じて表示する色を変える。

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;

    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.DepthStream.Enable();
      kinect.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(kinect_DepthFrameReady);

      kinect.Start();
    }

    private void StopKinect(KinectSensor kinect){
      if(kinect != null){
        if(kinect.IsRunning){
          kinect.DepthFrameReady -= kinect_DepthFrameReady;

          kinect.Stop();
          kinect.Dispose();

          image1.Source = null;
        }
      }
    }

    void kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e){
      try{
        KinectSensor kinect = sender as KinectSensor;

        if(kinect == null){
          return;
        }

        using(DepthImageFrame depthFrame = e.OpenDepthImageFrame()){
          if(depthFrame != null){
            DepthImageStream depthStream = kinect.DepthStream;

            short[] depthPixel = new short[depthFrame.PixelDataLength];
            depthFrame.CopyPixelDataTo(depthPixel);

            byte[] bufferImage = new byte[depthFrame.PixelDataLength * BytesPerPixel];

            for(int i = 0; i < depthPixel.Length; i++){
              int distance = depthPixel[i] >> DepthImageFrame.PlayerIndexBitmaskWidth;

              // unknown -0.4m (distance values -1)
              if(distance == depthStream.UnknownDepth){
                bufferImage[i * 4] = 0;
                bufferImage[i * 4 + 1] = 0;
                bufferImage[i * 4 + 2] = 255;
              }
              // too near 0.4-0.8m (distance values 0)
              else if (distance == depthStream.TooNearDepth){
                bufferImage[i * 4] = 0;
                bufferImage[i * 4 + 1] = 255;
                bufferImage[i * 4 + 2] = 0;
              }
              // too far 4.0m- (distance values 4095)
              else if(distance == depthStream.TooFarDepth){
                bufferImage[i * 4] = 255;
                bufferImage[i * 4 + 1] = 0;
                bufferImage[i * 4 + 2] = 0;
              }
              // normal values 0.8-4.0m (distance values 1-4094)
              else{
                bufferImage[i * 4] = 0;
                bufferImage[i * 4 + 1] = (byte)((distance / 4094.0) * 255.0);
                bufferImage[i * 4 + 2] = 255;
              }
            }

            image1.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, bufferImage, depthFrame.Width * BytesPerPixel);
          }
        }
      }
      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