KinectでカメラのRGBデータ取得
Kinectプログラミング、はじめの一歩として、RGBカメラの画像を表示してみました。
手順
- プロジェクトの参照に以下を追加
- Microsoft.Kinect.dll
- コード
Kinectの画像データを格納するWriteableBitmapは、DependencyPropertyにしておき、XAML側からバインドしました。
取得するColorImageの種類
ColorStreamのEnableメソッドに渡す引数で、取得する画像の種類を指定できます。
取得できる画像の種類は以下の列挙体で定義されています。
ColorImageFormat Enumeration
全コードは以下の通り。
MainWindow.xaml
<Window x:Class="KinectTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closing="Window_Closing"> <Grid> <Image Source="{Binding ColorBitmap}"/> </Grid> </Window>
MainWindow.xaml.cs
using Microsoft.Kinect; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace KinectTest { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { private KinectSensor sensor; public WriteableBitmap ColorBitmap { get { return (WriteableBitmap)GetValue(ColorBitmapProperty); } set { SetValue(ColorBitmapProperty, value); } } // Using a DependencyProperty as the backing store for ColorBitmap. This enables animation, styling, binding, etc... public static readonly DependencyProperty ColorBitmapProperty = DependencyProperty.Register("ColorBitmap", typeof(WriteableBitmap), typeof(MainWindow), new PropertyMetadata(null)); private byte[] colorPixels; public MainWindow() { InitializeComponent(); this.DataContext = this; } private void Window_Loaded(object sender, RoutedEventArgs e) { if (KinectSensor.KinectSensors.Count <= 0) { MessageBox.Show("Kinectが接続されていません。"); Close(); } this.sensor = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected); if (this.sensor != null) { this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength]; this.ColorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null); this.sensor.ColorFrameReady += sensor_ColorFrameReady; } try { this.sensor.Start(); } catch (IOException) { this.sensor = null; } if (this.sensor == null) { MessageBox.Show("使用可能なKinectが見つかりません"); } } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (this.sensor != null) { this.sensor.Stop(); } } void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame != null) { var bmp = this.ColorBitmap; colorFrame.CopyPixelDataTo(this.colorPixels); bmp.WritePixels( new Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight), this.colorPixels, bmp.PixelWidth * sizeof(int), 0); } } } } }