[KinectWPF程序]1深度图像,使用WriteableBitmap对象改进Kinect图像显示&复杂的Kinect初始化方法

2016-12-17 19:59:26来源:CSDN作者:cvMat人点击

[KinectWPF程序]1深度图像,使用WriteableBitmap对象改进Kinect图像显示&复杂的Kinect初始化方法

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;using System.Drawing;namespace 差帧{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        //私有Kinectsensor对象        private KinectSensor kinect;        private WriteableBitmap depthImageBitMap;        private Int32Rect depthImageBitmapRect;        private int depthImageStride;        public KinectSensor Kinect        {            get { return this.kinect; }            set            {                //如果带赋值的传感器和目前的不一样                if (this.kinect != value)                {                    //如果当前的传感对象不为null                    if (this.kinect != null)                    {                        //uninitailize当前对象                        this.kinect = null;                        UninitializeKinectSensor(this.kinect);                    }                    //如果传入的对象不为空,且状态为连接状态                    if (value != null && value.Status == KinectStatus.Connected)                    {                        this.kinect = value;                        InitializeKinectSensor(this.kinect);                    }                }            }        }        public MainWindow()        {            InitializeComponent();            this.Loaded += (s, e) => DiscoverKinectSensor();            this.Unloaded += (s, e) => this.kinect = null;        }        private void DiscoverKinectSensor()        {            KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;            this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);        }        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:处理其他情况下的状态            }        }        private void InitializeKinectSensor(KinectSensor kinectSensor)        {            if (kinectSensor != null)            {                DepthImageStream depthStream = kinectSensor.DepthStream;                depthStream.Enable();                depthImageBitMap = new WriteableBitmap(depthStream.FrameWidth, depthStream.FrameHeight, 96, 96, PixelFormats.Gray16, null);                depthImageBitmapRect = new Int32Rect(0, 0, depthStream.FrameWidth, depthStream.FrameHeight);                depthImageStride = depthStream.FrameWidth * depthStream.FrameBytesPerPixel;                DepthImage.Source = depthImageBitMap;                kinectSensor.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(kinectSensor_DepthFrameReady);                kinectSensor.Start();            }        }        private void UninitializeKinectSensor(KinectSensor kinectSensor)        {            if (kinectSensor != null)            {                kinectSensor.Stop();                kinectSensor.DepthFrameReady -= new EventHandler<DepthImageFrameReadyEventArgs>(kinectSensor_DepthFrameReady);            }        }        void kinectSensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)        {            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())            {                if (depthFrame != null)                {                    short[] depthPixelDate = new short[depthFrame.PixelDataLength];                    depthFrame.CopyPixelDataTo(depthPixelDate);                    depthImageBitMap.WritePixels(depthImageBitmapRect, depthPixelDate, depthImageStride, 0);                }            }        }    }}

相关文章

    无相关信息

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台