このエントリはKINECT SDK Advent Calendar 2011 : ATNDの12月3日分です!!
Advent Calendarでの、僕の全プロジェクトはこちらです
普段はもっぱらC++か、C#のFormアプリなのですが、WPFの勉強も兼ねてWPFで作ってみました。
はじめてのF# de KINECTプログラミング #kinectsdk_ac - pocketberserkerの爆走のC#版です。こんなんなります。
見た目の解説
まずは見た目です。XAMLのツールボックスが空っぽのままだったので、手打ちしました。。。
表示するサイズのImageを作ってるだけです
<Window x:Class="CameraImageWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="518" Width="665"> <Grid> <Image Name="kinectImage" Height="480" Width="640" /> </Grid> </Window>
中身の解説
KINECTの初期化と、データ更新時の表示処理です
using System; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using Microsoft.Research.Kinect.Nui; namespace CameraImageWPF { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { Runtime kinect; public MainWindow() { InitializeComponent(); kinect = Runtime.Kinects[0]; kinect.Initialize( RuntimeOptions.UseColor ); kinect.VideoStream.Open( ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color ); kinect.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>( kinect_VideoFrameReady ); } void kinect_VideoFrameReady( object sender, ImageFrameReadyEventArgs e ) { PlanarImage image = e.ImageFrame.Image; kinectImage.Source = BitmapSource.Create( image.Width, image.Height, 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel ); } } }
インスタンスの取得
操作するKINECTのインスタンスを番号付きで取得します。
Beta1からインスタンスの取得方法が変わっています。これは、KINECTの挿抜状態を取得できることと関連しているかと思います。
kinect = Runtime.Kinects[0];
カメラの初期化
初期化方法については、Beta1と同様に、KINECTの初期化、カメラの初期化、データ更新時のコールバック先の登録です。
kinect.Initialize( RuntimeOptions.UseColor ); kinect.VideoStream.Open( ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color ); kinect.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>( kinect_VideoFrameReady );
データの更新
データが更新されると、イベントが発生するので、Imageに設定します。
void kinect_VideoFrameReady( object sender, ImageFrameReadyEventArgs e ) { PlanarImage image = e.ImageFrame.Image; kinectImage.Source = BitmapSource.Create( image.Width, image.Height, 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel ); }