如何使用 Kinect 深度影像 (For Windows SDK V1)

Photobucket


透過 Kinect的距離量測功能,可以有多層次的深度影像,

可過濾掉背景影像或將背景塗黑,只留下使用者(Player)的影像。


目前SDK 正式版 V1,提供了一組 Kinect工具,


工具名稱為Kinect Wpf Viewers,方便使用者操作Kinect。

而如何在工具箱裡,加入這套控制項可以參考 KT這一篇:

如何在工具箱裡,加入Kinect Wpf Viewers 7個控制項 




Kinect Wpf Viewers 加入工具箱後,

這次我們需要使用其中兩個控制項 「KinectSensorChooser 」和 


KinectDepthViewer」 ,來實作 Kinect 深度影像。
Photobucket


目前顏色與距離關係表示如圖:
Photobucket







若需更改每段距離的顏色的表現,可以修改Kinect Wpf Viewers專案裡的

 KinectDepthViewer .xaml.cs」。


距離和幾位使用者計算公式:
Photobucket






開啟Visual Studio 2010 建立一個Kinect專案後,


在設計畫面中加入「KinectSensorChooser 」和 KinectDepthViewer」 。


設定KinectDepthViewer 寬度和高度Width=320和Height=240,

設定KinectDepthViewer 的Kinect 屬性,資料繫結到 KinectSensorChooser。
(詳細圖文步驟可參考: 如何使用 Kinect 彩色影像 (For Windows SDK V1),類似的作法)

再來只要加入,開啟深度影像功能相關事件即可完成。


結果展示:


XAML 程式碼如下:


    
        
        
    


C# 程式碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace KinectDepthViewer_Demo
{
    /// 
    /// MainWindow.xaml 的互動邏輯
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            StopKinect(kinectSensorChooser1.Kinect);
        }

        void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

            var oldSensor = (KinectSensor)e.OldValue;

            //關閉舊的 Kinect
            if (oldSensor != null)
            {
                oldSensor.Stop();
                oldSensor.AudioSource.Stop();
            }

            //開啟新的 Kinect
            var newSensor = (KinectSensor)e.NewValue;
            if (newSensor == null)
            {
                return;
            }

            //開啟深度影像功能
            newSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
            newSensor.SkeletonStream.Enable();

            try
            {
                newSensor.Start();
            }
            catch (System.IO.IOException)
            {
                //this happens if another app is using the Kinect
                kinectSensorChooser1.AppConflictOccurred();
            }
        }    

        private void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                if (sensor.IsRunning)
                {
                    //關閉 Kinect
                    sensor.Stop();

                    //關閉 AudioSource
                    if (sensor.AudioSource != null)
                    {
                        sensor.AudioSource.Stop();
                    }
                }
            }
        } 
    }
}







範例程式下載:


這個網誌中的熱門文章

最新入門零基礎 Java 教學【從零開始學 Java 程式設計】Java教學課程目錄 (IntelliJ IDEA 開發教學)

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

nano 文字編輯器

Android Studio 歷代版本下載點

2022 最新入門零基礎 Flutter教學 【Flutter 程式設計入門實戰 30 天】Flutter 教學課程目錄 (IntelliJ IDEA 開發教學)