ホームに戻る
 BGR

0、はじめに

黒っぽい色を赤、白っぽい色を青、それ以外を緑で表示。

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{
    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){
      // RgbResolution640x480Fps30
      // RgbResolution1280x960Fps12
      // YuvResolution640x480Fps15
      // RawYuvResolution640x480Fps15
      ColorImageFormat colorImageFormat = ColorImageFormat.RgbResolution640x480Fps30;

      kinect.ColorStream.Enable(colorImageFormat);
      kinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady);
      kinect.Start();
    }

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

          kinect.Stop();
          kinect.Dispose();
          kinect = null;

          image1.Source = null;
        }
      }
    }

    void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e){
      try{
        using(ColorImageFrame colorFrame = e.OpenColorImageFrame()){
          if(colorFrame != null){
            byte[] bufferImage = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(bufferImage);

            for(int i = 0; i < colorFrame.PixelDataLength; i += 4){
              if(bufferImage[i] < 50 && bufferImage[i + 1] < 50 && bufferImage[i + 2] < 50){
                bufferImage[i] = 0;       // B
                bufferImage[i + 1] = 0;   // G
                bufferImage[i + 2] = 255; // R
              }
              else if(bufferImage[i] > 200 && bufferImage[i + 1] > 200 && bufferImage[i + 2] > 200){
                bufferImage[i] = 255;   // B
                bufferImage[i + 1] = 0; // G
                bufferImage[i + 2] = 0; // R
              }
              else{
                bufferImage[i] = 0;       // B
                bufferImage[i + 1] = 255; // G
                bufferImage[i + 2] = 0;   // R
              }
            }

            BitmapSource bitmap = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, bufferImage, colorFrame.Width * colorFrame.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