




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
学习资料收集于网络,仅供参考我们来说说景深数据Depth Data,这是Kinect的Depth Camera为我们提供的全新功能,以往的技术只能够通过图像识别来完成的一些工作,我们可以借助景深来帮我们完成了。比如,前景与背景的分离,以前只能将背景设置为蓝屏或者绿屏,但是现在有了景深数据,我们就可以很容易地将前景物体从背景中分离出来。当然,需要特别说明的是,Depth Camera技术是由以色列公司PrimeSense提供的。程序布局在这一章里,我们要完成的工作非常简单,根据物体距离Kinect的远近,设置成不同的颜色。首先,我们要创建一个新的WPF工程,然后添加一个Image控件:然后是MainWindow.xaml.cs中的核心代码,这部分代码与之前的代码大体一致,所以不做过多解释了:/Kinect RuntimeRuntime Runtime nui = new Runtime(); private void Window_Loaded(object sender, RoutedEventArgs e)/UseDepthAndPlayerIndex and UseSkeletalTrackingnui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking);/register for eventnui.DepthFrameReady += new EventHandler(nui_DepthFrameReady);/DepthAndPlayerIndex ImageTypenui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240,ImageType.DepthAndPlayerIndex);private void Window_Closed(object sender, EventArgs e)nui.Uninitialize(); 唯一需要大家注意的是,我们在初始化函数中传递的参数是RuntimeOptions.UseDepthAndPlayerIndex,这表示景深信息中会包含PlayerIndex的信息。接下来,我们就要花点时间来理解DepthAndPlayerIndex的实际结构。理解DepthAndPlayerIndex先来看DepthFrameReady事件处理函数,如下:void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)/ Convert depth information for a pixel into color information byte ColoredBytes = GenerateColoredBytes(e.ImageFrame); / create an image based on the colored bytes PlanarImage image = e.ImageFrame.Image;image1.Source = BitmapSource.Create( image.Width, image.Height, 96, 96, PixelFormats.Bgr32, null, ColoredBytes, image.Width * PixelFormats.Bgr32.BitsPerPixel / 8); DepthFrameReady事件会返回一个ImageFrame对象,其中会包含一个PlanarImage对象。PlanarImage对象会包含一个byte数组,这个数组中包含每个像素的深度信息。这个数组的特点是,从图像左上角开始、从左到右,然后从上到下。该数组中每个像素由两个bytes表示,我们需要通过位运算的办法来获取每个像素的位置信息。如果是Depth Image Type,将第二个bytes左移8位即可。Distance (0,0) = (int)(Bits0 | Bits1 3 | Bits1 5); DepthAndPlayerIndex image类型的第一个bytes前三位包括Player Index信息。Player Index最多包含六个可能值:0, 没有玩家;1,玩家1;2,玩家2,以此类推。我们可以用下面的方法来获取Player Index信息:private static int GetPlayerIndex(byte firstFrame)/returns 0 = no player, 1 = 1st player, 2 = 2nd player.return (int)firstFrame & 7; 设置颜色接下来,我们创建一个bytes数组,用来存储颜色值。if (distance 0)/we are the farthestcolorFrameindex + BlueIndex = 0;colorFrameindex + GreenIndex = 255;colorFrameindex + RedIndex = 255;当然,我们最好是设置几个边界值,来定义不同距离的不同颜色:/equal coloring for monochromatic histogramvar intensity = CalculateIntensityFromDepth(distance);colorFrameindex + BlueIndex = intensity;colorFrameindex + GreenIndex = intensity;colorFrameindex + RedIndex = intensity;const float MaxDepthDistance = 4000; / max value returnedconst float MinDepthDistance = 850; / min value returnedconst float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;public static byte CalculateIntensityFromDepth(int distance)/formula for calculating monochrome intensity for histogramreturn (byte)(255 - (255 * Math.Max(distance - MinDepthDistance, 0) / (MaxDepthDistanceOffset);最后,把所有代码放在一起,我们设定的颜色值为:蓝色,小于900;900到2000,绿色;大于2000,红色代码如下: private byte GenerateColoredBytes(ImageFrame imageFrame) int height = imageFrame.Image.Height; int width = imageFrame.Image.Width; /Depth data for each pixel Byte depthData = imageFrame.Image.Bits; /colorFrame contains color information for all pixels in image /Height x Width x 4 (Red, Green, Blue, empty byte) Byte colorFrame = new byteimageFrame.Image.Height * imageFrame.Image.Width * 4; /Bgr32 - Blue, Green, Red, empty byte /Bgra32 - Blue, Green, Red, transparency /You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparent /hardcoded locations to Blue, Green, Red (BGR) index positions const int BlueIndex = 0; const int GreenIndex = 1; const int RedIndex = 2; var depthIndex = 0; for (var y = 0; y height; y+) var heightOffset = y * width; for (var x = 0; x width; x+) var index = (width - x - 1) + heightOffset) * 4; /var distance = GetDistance(depthDatadepthIndex, depthDatadepthIndex + 1); var distance = GetDistanceWithPlayerIndex(depthDatadepthIndex, depthDatadepthIndex + 1); if (distance 900 & distance 2000) /we are the farthest colorFrameindex + BlueIndex = 0; colorFrameindex + GreenIndex = 0; colorFrameindex + RedIndex = 255; /Color a player if (GetPlayerIndex(depthData
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 南昌校园安全教育主题展厅
- 累积滑移施工方案
- 保险公司营销策略方案
- 景区公众号活动方案策划
- 会计实习报告范文两篇
- 建筑车行坡道分析方案设计
- 会务会展活动施工方案
- 九年级化学第五单元定量研究化学反应第2节化学反应的表示练习试题以及答案(适合鲁教版)
- 城镇雨污管网施工方案
- 2025至2030年中国维生物磷酸酯镁市场分析及竞争策略研究报告
- 中国冠心病康复循证实践指南(2024版)第一部分
- DL∕T 1870-2018 电力系统网源协调技术规范
- 预防导管相关性血流感染(CDC指南解读)
- AQ 1083-2011 煤矿建设安全规范 (正式版)
- FZ∕T 54007-2019 锦纶6弹力丝行业标准
- 2024年江苏省高中学业水平合格性考试数学试卷试题(答案详解1)
- DZ∕T 0148-2014 水文水井地质钻探规程(正式版)
- 关于颈椎病介绍
- 膝痹病的中医治疗方案
- Know Before You Go:趣谈“一带一路”国家智慧树知到期末考试答案2024年
- 静疗健康宣教
评论
0/150
提交评论