尝试根据距离将“Kinect深度”从灰度级转换为RGB

本文关键字:灰度级 转换 RGB 深度 Kinect 距离 | 更新日期: 2023-09-27 17:57:42

我与Kinect合作进行一个研究项目,直到现在我只处理骨骼跟踪。现在我进入了深度流的深度,我想知道如何创建我们在一些深度流中看到的RGB色标。我的是灰色的。深度事件中有一部分我不理解,我觉得这对理解它是如何工作的,以及我如何将其更改为颜色非常重要,这是整数变量的定义。

private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e){
        using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
        {
            if (depthFrame != null)
            {
                // Copy the pixel data from the image to a temporary array
                depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
                // Get the min and max reliable depth for the current frame
                int minDepth = depthFrame.MinDepth;
                int maxDepth = depthFrame.MaxDepth;
                // Convert the depth to RGB
                int colorPixelIndex = 0;              
                for (int i = 0; i < this.depthPixels.Length; ++i)
                {
                    // Get the depth for this pixel
                    short depth = depthPixels[i].Depth;
                    if (depth > 2000) depth = 0; //ive put this here just to test background elimination
                    byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
                    //WHAT IS THIS LINE ABOVE DOING?
                    // Write out blue byte
                    this.colorPixels[colorPixelIndex++] = intensity;
                    // Write out green byte
                    this.colorPixels[colorPixelIndex++] = intensity;
                    // Write out red byte                        
                    this.colorPixels[colorPixelIndex++] = intensity;
                    // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                    // If we were outputting BGRA, we would write alpha here.

                    ++colorPixelIndex;
                }
                // Write the pixel data into our bitmap
                this.colorBitmap.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                    this.colorPixels,
                    this.colorBitmap.PixelWidth * sizeof(int),
                    0);
            }
        }
}

尝试根据距离将“Kinect深度”从灰度级转换为RGB

byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
//WHAT IS THIS LINE ABOVE DOING?

上面的行使用三元运算符

基本上它是一个单行if语句,等价于:

byte intensity;
if (depth >= minDepth && depth <= maxDepth) 
{
    intensity = (byte)depth;
}
else
{
    intensity = 0;
}

给深度图像着色的技巧是将强度乘以色调。例如:

Color tint = Color.FromArgb(0, 255, 0) // Green
// Write out blue byte
this.colorPixels[colorPixelIndex++] = intensity * tint.B;
// Write out green byte
this.colorPixels[colorPixelIndex++] = intensity * tint.G;
// Write out red byte                        
this.colorPixels[colorPixelIndex++] = intensity * tint.R;