kinect 开发入门——读取深度信息和显示深度信息图像




本文代码来自http://www.cnblogs.com/yangecnu/archive/2012/04/04/2431615.html

本人只是将其整合并添加适量的注释


xaml文件:

<Window x:Class="kinect_depth_image.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">
    <Grid>
        <Image x:Name="DepthImage" </Image>
    </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 System.IO;

namespace kinect_depth_image
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        //私有Kinectsensor对象
        private KinectSensor kinect;


        private WriteableBitmap depthImageBitMap;
        private Int32Rect depthImageBitmapRect;
        private int depthImageStride;
        //private byte[] colorImagePixelData;


        public KinectSensor Kinect
        {
            get { return this.kinect; }
            set
            {
                //如果带赋值的传感器和目前的不一样
                if (this.kinect != value)
                {
                    //如果当前的传感对象不为null
                    if (this.kinect != null)
                    {
                        UninitializeKinectSensor(this.kinect);
                        //uninitailize当前对象
                        this.kinect = null;
                    }
                    //如果传入的对象不为空,且状态为连接状态
                    if (value != null && value.Status == KinectStatus.Connected)
                    {
                        this.kinect = value;

                        InitializeKinectSensor(this.kinect);

                    }

                }
            }
        }

        private void InitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                //初始化深度信息流,并使其可以运行
                DepthImageStream depthStream = kinectSensor.DepthStream;
                depthStream.Enable();

                //新建一个位图
                this.depthImageBitMap = new WriteableBitmap(depthStream.FrameWidth, depthStream.FrameHeight, 96, 96, PixelFormats.Gray16, null);
                depthImageBitmapRect = new Int32Rect(0, 0, depthStream.FrameWidth, depthStream.FrameHeight);
                //Stride是指图像每一行需要占用的字节数。根据BMP格式的标准,Stride一定要是4的倍数。
                depthImageStride = depthStream.FrameWidth * depthStream.FrameBytesPerPixel;

                DepthImage.Source = depthImageBitMap;
                kinectSensor.DepthFrameReady += kinectSensor_DepthFrameReady;
                kinectSensor.Start();
            }


        }

        private void UninitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.Stop();
                kinectSensor.DepthFrameReady -= new EventHandler<DepthImageFrameReadyEventArgs>(kinectSensor_DepthFrameReady);
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            //出现图像显示
            this.Loaded += (s, e) => DiscoverKinectSensor();//见下边的注释
            this.Unloaded += (s, e) => this.kinect = null;


            /*
             *在上面的代码中,+=是在委托链上增加一个委托,(s,e) =>是一个lambda表达式,这个表达式创建一个委托,
             * 委托处理的主体就是=> 搜索后面的部分。其实这个写法相当于

             * this.Loaded += new EventHandler(Form_Loaded);
             * private void Form_Loaded(object sender,   EventArgs e)
             * {    DiscoverKinectSensor();}
             */


            //深度信息——仅仅是出现数字
           /*
           //初始化sensor实例
           KinectSensor sensor = KinectSensor.KinectSensors[0];

           //初始化照相机
           sensor.DepthStream.Enable();
           sensor.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(sensor_DepthFrameReady);

           Console.ForegroundColor = ConsoleColor.Green;

           //打开数据流
           sensor.Start();

          */

        }
        //该方法用于将深度信息的图像显示出来
        void kinectSensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame != null)
                {
                    //创建存储深度信息的数组,大小为一帧深度信息的长度
                    short[] depthPixelDate = new short[depthFrame.PixelDataLength];
                    //将深度信息存到深度信息的数组中
                    depthFrame.CopyPixelDataTo(depthPixelDate);
                    //深度信息图,将那些数据写到图中并显示出来   Stride是指图像每一行需要占用的字节数。根据BMP格式的标准,Stride一定要是4的倍数。
                    depthImageBitMap.WritePixels(depthImageBitmapRect, depthPixelDate, depthImageStride, 0);
                }
            }
        }

      
        //该方法用于将深度数据显示出来
        static void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
            using (var depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame == null) return;
                short[] bits = new short[depthFrame.PixelDataLength];
                //将深度信息转换为bit,每一个bit都写出来
                depthFrame.CopyPixelDataTo(bits);
                foreach (var bit in bits)
                    Console.Write(bit);
            }

        }
        //发现kinect传感器
        private void DiscoverKinectSensor()
        {
            KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
            this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
        }
        //kinect传感器连接或是不连接,并对其进行处理
        private void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case KinectStatus.Connected:
                    if (this.kinect == null)
                        this.kinect = e.Sensor;
                    break;
                case KinectStatus.Disconnected:
                    if (this.kinect == e.Sensor)
                    {
                        this.kinect = null;
                        this.kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
                        if (this.kinect == null)
                        {
                            //TODO:通知用于Kinect已拔出
                        }
                    }
                    break;
                //TODO:处理其他情况下的状态
            }
        }

    }
}


你可能感兴趣的:(传感器,数据,注释,实例,kinect)