LoginSignup
0
1

More than 5 years have passed since last update.

KinectでColor画像を取得

Last updated at Posted at 2014-06-24

Kinectプログラミングの練習として、カメラ画像を表示するプログラムを動かしてみます。
環境は Windows7 SP1 と Visual Studio 2013、
言語は C#、WPF アプリケーションです。

大まかな処理の流れは以下の通り。

・Kinect の初期化と開始処理
・カメラ画像の取得とフレーム表示処理
・Kinect の終了処理

準備として、プロジェクトの参照に「Microsoft.Kinect」を追加します。
また、今回はフレーム表示にて Coding4FunToolkit のメソッドを使用しているのでこちらも予め用意します。

http://c4fkinect.codeplex.com/releases/view/111747

using Microsoft.Kinect;
using Coding4Fun.Kinect.Wpf;

Kinect の初期化と開始処理

KinectSensor kinect;
kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
kinect.ColorFrameReady += ColorFrame;

ColorStream.Enable でカメラを有効にし、ColorFrameReady でフレーム表示のイベントを設定します。
カメラ有効の際には ColorImageFormat で解像度とフレームレートが指定できます。
RGBタイプの場合、解像度を 1280×960 にすることもできますが、フレームレートは 12fps に下がります。

kinect.Start();

これで動作を開始します。

カメラ画像の取得とフレーム表示処理

ColorImageFrame Frame = e.OpenColorImageFrame();
Color.Source = Frame.ToBitmapSource();

OpenColorImageFrame() で画像データを取得し、ToBitmapSource() でビットマップ画像への変換を行います。
変換したものを Color に格納し、XAMLフォームの Imageタグで呼び出しています。

なお、ToBitmapSource() は先述の Coding4FunのToolkit で追加されているメソッドです。
BitmapSource.Create を利用する例などがありましたが、簡単だったのでこちらを利用してみました。

また、本当はフレームデータのメモリリークや null のチェックなどが必要となるようなのですが、、、ここでは省いてしまっています。

Kinect の終了処理

if (kinect.IsRunning) 
{
    kinect.Stop();
}

Kinect が動作している状態から、Stop() により処理を停止します。
ここのメソッド名と XAMLフォームの Closing属性が対応しており、ウインドウを閉じたときに呼び出される処理となります。

プログラム

最終的に、以下の通りとなりました。
これだけで一応動作はするものの、先ほどのメモリリークの件など大切な処理が所々足りないですね。
まだまだ理解しなければならないことは多いですが、とりあえずこんなに簡単に動かせたよということで……

XMALフォーム部【MainWindow.xaml】

<Window x:Class="Kinect_Color.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="480" Width="640" Closing="ColorClose">
    <Grid>
        <Image Name="Color" />
    </Grid>
</Window>

ロジック部【MainWindow.xaml.cs】

using System;
using System.Collections.Generic;
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;

using Microsoft.Kinect;
using Coding4Fun.Kinect.Wpf;

namespace Kinect_Color
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        KinectSensor kinect;

        public MainWindow()
        {
            InitializeComponent();

            if (KinectSensor.KinectSensors.Count == 0) 
            {
                MessageBox.Show("Kinectを接続してください");
            }

            kinect = KinectSensor.KinectSensors[0];
            kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
            kinect.ColorFrameReady += ColorFrame;

            try
            {
                kinect.Start();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        void ColorFrame(object sender, ColorImageFrameReadyEventArgs e) 
        {
            try
            {
                ColorImageFrame Frame = e.OpenColorImageFrame();
                Color.Source = Frame.ToBitmapSource();
            }

            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }

        }

        void ColorClose(object sender, System.ComponentModel.CancelEventArgs e) 
        {
            if (kinect.IsRunning) 
            {
                kinect.Stop();
            }
        }

    }
}

次は深度の取得を行ってみます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1