首页 > 代码库 > Kinect学习笔记(五)——更专业的深度图
Kinect学习笔记(五)——更专业的深度图
这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用sdk里面封装好的功能来减少测量的误差,以及避免转换为灰度图时,出现绿巨人时候的掉针的bug.
下面直接贴代码吧。
private byte[]convertDepthFrameToColorFrame2(short[] depthFrame,DepthImageStream depthSteam) { byte[] depthFrame32 = new byte[depthSteam.FrameWidth * depthSteam.FrameHeight * Bgr32BytesPerPixel]; //通过常量获得有效视距,不用硬解码(就是自己定义距离) int tooNearDepth = depthSteam.TooNearDepth; int tooFarDepth = depthSteam.TooFarDepth; int unknownDepth = depthSteam.UnknownDepth; for(int i16=0,i32=0;i16<depthFrame.Length&&i32<depthFrame32.Length;i16++,i32+=4) { int player = depthFrame[i16] & DepthImageFrame.PlayerIndexBitmask; int realDepth = depthFrame[i16] >> DepthImageFrame.PlayerIndexBitmaskWidth; //通过位运算,将13位的深度图,裁剪为8位,对于显示足够了。 byte intensity = (byte)(~(realDepth >> 4)); if(player==0&&realDepth==0) { //白色 depthFrame32[i32 + RedIndex] = 255; depthFrame32[i32 + GreenIndex] = 255; depthFrame32[i32 + BlueIndex] = 255; } else if(player==0&&realDepth==tooFarDepth) { //深紫色 depthFrame32[i32 + RedIndex] = 66; depthFrame32[i32 + GreenIndex] = 0; depthFrame32[i32 + BlueIndex] = 66; } else if (player == 0 && realDepth == unknownDepth) { //深棕色 depthFrame32[i32 + RedIndex] = 66; depthFrame32[i32 + GreenIndex] = 66; depthFrame32[i32 + BlueIndex] = 33; } else { depthFrame32[i32 + RedIndex] = (byte)(intensity >> IntensityShiftByPlayerR[player]); depthFrame32[i32 + GreenIndex] = (byte)(intensity >> IntensityShiftByPlayerG[player]); depthFrame32[i32 + BlueIndex] = (byte)(intensity >> IntensityShiftByPlayerB[player]); } } return depthFrame32; }
void _kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e) { using(DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if(depthFrame!=null) { short[] depthPixelData = http://www.mamicode.com/new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(depthPixelData); byte[] pixels = this.convertDepthFrameToColorFrame2(depthPixelData, ((KinectSensor)sender).DepthStream); int stride = depthFrame.Width * 4; imageDepth.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride); } } }
再附上效果图
Kinect学习笔记(五)——更专业的深度图
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。