如何使用Kinect显示彩色图像和深度图像。
来源:互联网 发布:mt4软件哪个好 编辑:程序博客网 时间:2024/04/26 07:56
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{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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<AllFramesReadyEventArgs>(_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(); } }}
成功显示图:
行了显示完彩色图像,开始显示深度图像:
先上效果图:
其实代码页很简单,就是在原来的代码的基础上加上一个:private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame);函数。这个函数的大概意思就是把8位的深度图转换成32位的彩色图。再在
void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)函数中添加下图的代码就ok了。
改后的代码如下:
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{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> 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<AllFramesReadyEventArgs>(_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至此深度图可以显示了,。
- 如何使用Kinect显示彩色图像和深度图像。
- ROS使用openni获取Kinect彩色图像和深度图像
- OpenCV--鼠标响应Kinect彩色图像显示深度信息
- kinect,openni,opencv 获取并显示深度与彩色图像
- OpenNI结合OpenCV显示Kinect彩色、深度及融合图像
- opencv2.3读取kinect深度信息和彩色图像
- 关于Kinect深度图和彩色图像提取
- kinect 深度彩色图像对齐的问题
- Kinect开发教程二:OpenNI读取深度图像与彩色图像并显示
- Kinect开发教程二:OpenNI读取深度图像与彩色图像并显示
- Kinect开发教程二:OpenNI读取深度图像与彩色图像并显示
- Ogre中显示Kinect的彩色图像
- Heresy透過 OpneNI 合併 Kinect 深度以及彩色影像資料(Kinect开发教程二:OpenNI读取深度图像与彩色图像并显示)
- Kinect开发教程八:OpenNI2显示深度、彩色及融合图像
- Kinect开发教程八:OpenNI2显示深度、彩色及融合图像
- [KinectWPF程序]2彩色图像,使用WriteableBitmap对象改进Kinect图像显示&复杂的Kinect初始化方法
- Heresy透過 OpneNI 讀取 Kinect 深度影像資料(Kinect开发教程二:OpenNI读取深度图像与彩色图像并显示)
- kinect+openni获取kinect的颜色图像和深度图像
- 制作镜像需要注意的问题
- Android开发中如何调用摄像头的功能
- 使用 HTML5 IndexedDB API
- Socket通信小结
- struts2弹出信息提示
- 如何使用Kinect显示彩色图像和深度图像。
- Android Camera
- myeclipse修改不能自动编译成相应的文件
- 定制自己的linux系统
- C语言--第一篇导言
- Android开发者指南-摄像头-Camera[原创译文]
- PERL LWP
- KMP算法
- struts2 type="chain" 用法