如何使用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;
namespace WpfApplication2
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()//vs2010自动生成的。
        {
            InitializeComponent();
        }
        private KinectSensor _kinect;//一个全局变量,自然是用来调用Kinect用的,
        private void startKinect()
        {
            if (KinectSensor.KinectSensors.Count > 0)//如果有一个Kinect是可以使用的。
            {
                _kinect = KinectSensor.KinectSensors[0];//打开第一个Kinect
                MessageBox.Show("kinect 目前的状态为:" + _kinect.Status);//这里调用一个messagebox来显示现在的状态。

                _kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                _kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                _kinect.SkeletonStream.Enable();//使能skeleton

                _kinect.AllFramesReady +=
                    new EventHandler(_kinect_AllFramesReady);              //大概意思就是添加一个事件处理程序吧,类似于单片机中的//中断。
                _kinect.Start();//设置完成之后启动Kinect
            }//if
            else
            {
                MessageBox.Show("没有任何Kinect设备");
            }
        }//start kinect 
        void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)//打开并显示彩色图像的具体程序。
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }//if null
                byte[] pixels = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(pixels);

                int stride = colorFrame.Width * 4;
                imageCamera.Source =//这个imageCamera变量时在mainFrame中定义的一个Image控件。
                    BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                    96, 96, PixelFormats.Bgr32, null, pixels, stride);

            }
            //throw new NotImplementedException();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)//当第一次窗体加载时,调用StartKinect。
        {
            startKinect();
        }

        

        
    }
}

成功显示图:

如何使用Kinect显示彩色图像和深度图像。_第1张图片

行了显示完彩色图像,开始显示深度图像:

先上效果图:

如何使用Kinect显示彩色图像和深度图像。_第2张图片

其实代码页很简单,就是在原来的代码的基础上加上一个:private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame);函数。这个函数的大概意思就是把8位的深度图转换成32位的彩色图。再在

 void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)函数中添加下图的代码就ok了。

如何使用Kinect显示彩色图像和深度图像。_第3张图片

改后的代码如下:

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 WpfApplication2
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private KinectSensor _kinect;
        const  float MaxDepthDistance = 4095;
        const float MinDepthDistance = 850;
        const float MaxDepthDistanceOffSet = MaxDepthDistance - MinDepthDistance;

        private const int RedIndex = 2;
        private const int GreenIndex = 1;
        private const int BlueIndex = 0;

        private void startKinect()
        {
            if (KinectSensor.KinectSensors.Count > 0)
            {
                _kinect = KinectSensor.KinectSensors[0];
                MessageBox.Show("kinect 目前的状态为:" + _kinect.Status);

                _kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                _kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                _kinect.SkeletonStream.Enable();

                _kinect.AllFramesReady +=
                    new EventHandler(_kinect_AllFramesReady);              
                _kinect.Start();
            }//if
            else
            {
                MessageBox.Show("没有任何Kinect设备");
            }
        }//start kinect 
        void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }//if null
                byte[] pixels = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(pixels);

                int stride = colorFrame.Width * 4;
                imageCamera.Source =
                    BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                    96, 96, PixelFormats.Bgr32, null, pixels, stride);

            }
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame == null)
                {
                    return;
                }
                byte[] pixels = convertDepthFrameToColorFrame(depthFrame);
                int stride = depthFrame.Width * 4;
                imageDepth.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height,
                    96, 96, PixelFormats.Bgr32, null, pixels, stride);

            }//depth frame ;
            //throw new NotImplementedException();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            startKinect();
        }
        private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame)
        {
            short[] rawDepthData = new short[depthFrame.PixelDataLength];
            depthFrame.CopyPixelDataTo(rawDepthData);
            byte[] pixels = new byte[depthFrame.Height * depthFrame.Width*4 ];
            for (int depthIndex = 0, colorIndex = 0;
                depthIndex < rawDepthData.Length && colorIndex < pixels.Length;
                depthIndex++, colorIndex+=4)
            {
                int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
                int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;
                if (depth <= 900)
                {
                    pixels[colorIndex + BlueIndex] = 255;
                    pixels[colorIndex + GreenIndex] = 0;
                    pixels[colorIndex + RedIndex] = 0;
                }//距离Kinect很近
                else if (depth > 900 && depth < 2000)
                {
                    pixels[colorIndex + BlueIndex] = 0;
                    pixels[colorIndex + GreenIndex] = 255;
                    pixels[colorIndex + RedIndex] = 0;
                }//900,20000
                else if (depth > 2000)
                {
                    pixels[colorIndex + BlueIndex] = 0;
                    pixels[colorIndex + GreenIndex] = 0;
                    pixels[colorIndex + RedIndex] = 255;

                }//2000
                if (player > 0)
                {
                    pixels[colorIndex + BlueIndex] = Colors.LightGreen.B;
                    pixels[colorIndex + GreenIndex] = Colors.LightGreen.G;
                    pixels[colorIndex + RedIndex] = Colors.LightGreen.R;
                }
            }//for
            return pixels;
        }//convert 

    }
}

ok至此深度图可以显示了,。


你可能感兴趣的:(互动投影)