博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kinect sdk开发入门WPFdemo笔记[2] 获取深度数据
阅读量:6332 次
发布时间:2019-06-22

本文共 6838 字,大约阅读时间需要 22 分钟。

1.相比较之前的版本,多了一个Mode,如下所示:

2.两种Mode下的不同距离的效果:

3.获取depth和player的C#语言:

在上一篇的前提下(就是不改变XAML的情况下,修改后台程序),代码如下:

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 KinectSdkDemo {
/// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent(); } const float MaxDepthDistance = 4000;// max value returned const float MinDepthDistance = 850;// min value returned const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance; KinectSensor _sensor; private void Window_Loaded(object sender, RoutedEventArgs e) {
kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged); if (KinectSensor.KinectSensors.Count > 0) {
_sensor = KinectSensor.KinectSensors[0]; } if (_sensor.Status == KinectStatus.Connected) {
_sensor.ColorStream.Enable(); _sensor.DepthStream.Enable(); _sensor.SkeletonStream.Enable(); _sensor.AllFramesReady += new EventHandler
(_sensor_AllFramesReady); _sensor.Start(); } } void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) {
/*throw new NotImplementedException();*/ //using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) //{
// if (colorFrame == null) // {
// return; // } // byte[] pixels = new byte[colorFrame.PixelDataLength]; // colorFrame.CopyPixelDataTo(pixels); // int stride = colorFrame.Width * 4; ////image1.Source = //// BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96 //// , PixelFormats.Bgr32, null, pixels, stride); //} using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) {
if (depthFrame == null) {
return; } byte[] depthPixels = GenerateColoredBytes(depthFrame); int stride_depth = depthFrame.Width * 4; image1.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96 , PixelFormats.Bgr32, null, depthPixels, stride_depth); } } private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) {
//get the raw data from kinect with the depth for every pixel short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); //use depthFrame to create the image to display on-screen //depthFrame contains color information for all pixels in image //Height * Width * 4(Red, Green, Blue, empth byte) Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; //Bgr32 - Blue, Green, Red, empth byte //Bgra32 - Blue, Green, Red, transparency //You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparency //hardcoded locations to Blue, Green, Red(RGB) index positions const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; //loop through all distances //pick a RGB color based on distance for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) {
//get the player(requires skeleton tracking enabled for values) int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; //gets the depth value int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; //.9M or 2.95' if (depth <= 900) {
//we are very close pixels[colorIndex + BlueIndex] = 255; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } // 0.9M - 2, or 2.95' - 6.56' else if (depth > 900 && depth < 2000) {
//we are a bit further away pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } else if (depth > 2000) {
//we are the farthest pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 255; } //equal coloring for monochromatic histogram byte intensity = calculateIntensityFromDepth(depth); pixels[colorIndex + BlueIndex] = intensity; pixels[colorIndex + GreenIndex] = intensity; pixels[colorIndex + RedIndex] = intensity; //Color all players "gold" if (player > 0) {
pixels[colorIndex + BlueIndex] = Colors.Gold.B; pixels[colorIndex + GreenIndex] = Colors.Gold.G; pixels[colorIndex + RedIndex] = Colors.Gold.R; } } return pixels; } private byte calculateIntensityFromDepth(int depth) {
//formula for calculating monochrome intensity for histogram return (byte)(255 - (255 * Math.Max(depth - MinDepthDistance, 0) / (MaxDepthDistanceOffset))); } void StopKinect(KinectSensor sensor) {
if (sensor != null) {
sensor.Stop(); sensor.AudioSource.Stop(); } } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
StopKinect(kinectSensorChooser1.Kinect); } void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e) {
KinectSensor oldSensor = (KinectSensor)e.OldValue; StopKinect(oldSensor); KinectSensor newSensor = (KinectSensor)e.NewValue; newSensor.ColorStream.Enable(); newSensor.DepthStream.Enable(); newSensor.SkeletonStream.Enable(); // newSensor.AllFramesReady += new EventHandler
(_sensor_AllFramesReady); try {
newSensor.Start(); } catch (System.IO.IOException) {
kinectSensorChooser1.AppConflictOccurred(); } } } }

转载于:https://www.cnblogs.com/yemeishu/archive/2012/02/03/2337208.html

你可能感兴趣的文章
windows的服务中的登录身份本地系统账户、本地服务账户和网络服务账户修改
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
redis 安装
查看>>
SQL some any all
查看>>
电子书下载:Programming Windows Identity Foundation
查看>>
有理想的程序员必须知道的15件事
查看>>
用于测试的字符串
查看>>
财付通和支付宝资料收集
查看>>
PHPCMS V9数据库表结构分析
查看>>
理解 IEnumerable 与 IEnumerator
查看>>
NHibernate 2.0 Beta 1 Released和一些工具
查看>>
【每天一个Linux命令】12. Linux中which命令的用法
查看>>
软件接口数据一致性机制
查看>>
微服务架构介绍和RPC框架对比
查看>>
Debian下使用OpenLDAP 管理端
查看>>
泛型排序器TComparer
查看>>
9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路...
查看>>
创建符合标准的、有语意的HTML页面——ASP.NET 2.0 CSS Friendly Control Adapters 1.0发布...
查看>>
Adobe驳斥Flash过度耗电论 称HTML5更耗电
查看>>
No!No!No! It's not fashion!
查看>>