这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用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 = 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); } } }
再附上效果图
时间: 2024-10-21 11:44:22