首页 文章

Kinect V2彩色流字节顺序

提问于
浏览
2

我正在开发一个应用程序,它将从Kinect V2传感器流式传输颜色,深度和红外视频数据 . 现在我只是把应用程序的彩色视频部分放在一起 . 我已经阅读了一些教程并且实际上有一些视频数据进入我的应用程序 - 问题似乎是字节顺序似乎是错误的顺序,这给了我一个奇怪的变色图像(见下文) .

enter image description here

那么,让我解释一下我是怎么来到这里的 . 在我的代码中,我首先打开传感器并实例化一个新的多源帧读取器 . 在我创建了阅读器之后,我创建了一个名为Reader_MultiSourceFrameArrived的事件处理程序:

void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
    {
        if (proccessing || gotframe) return;

        // Get a reference to the multi-frame
        var reference = e.FrameReference.AcquireFrame();

        // Open color frame
        using (ColorFrame frame = reference.ColorFrameReference.AcquireFrame())
        {
            if (frame != null)
            {
                proccessing = true;
                var description = frame.ColorFrameSource.FrameDescription;

                bw2 = description.Width / 2;
                bh2 = description.Height / 2;
                bpp = (int)description.BytesPerPixel;

                if (imgBuffer == null)
                {
                    imgBuffer = new byte[description.BytesPerPixel * description.Width * description.Height];
                }

                frame.CopyRawFrameDataToArray(imgBuffer);
                gotframe = true;
                proccessing = false;
            }
        }
    }

现在,每次收到一帧(而不是处理)时,它应该将帧数据复制到一个名为imgBuffer的数组中 . 当我的应用程序准备就绪时,我会调用此例程将数组转换为我可以在屏幕上显示的Windows位图 .

if (gotframe)
        {
            if (theBitmap.Rx != bw2 || theBitmap.Ry != bh2) theBitmap.SetSize(bw2, bh2);

            int kk = 0;
            for (int j = 0; j < bh2; ++j)
            {
                for (int i = 0; i < bw2; ++i)
                {
                    kk = (j * bw2 * 2 + i) * 2 * bpp;
                    theBitmap.pixels[i, bh2 - j - 1].B = imgBuffer[kk];
                    theBitmap.pixels[i, bh2 - j - 1].G = imgBuffer[kk + 1];
                    theBitmap.pixels[i, bh2 - j - 1].R = imgBuffer[kk + 2];
                    theBitmap.pixels[i, bh2 - j - 1].A = 255;
                }
            }
            theBitmap.needupdate = true;
            gotframe = false;
        }
}

因此,在此运行之后,theBitmap现在包含在屏幕上绘制图像所需的图像信息...但是,如上图所示 - 它看起来很奇怪 . 最明显的变化是简单地改变像素B,G,R值的顺序,当它们被分配给double for循环中的位图时(我试过)...但是,这只会导致其他奇怪的彩色图像和没有提供准确的彩色图像 . 有什么想法我可能会出错吗?

2 回答

  • 1

    这是Bgra吗? Kinect v2中C#的正常“RGB”是BGRA .

    使用Kinect SDK 2.0,您不需要所有这些“for”循环 . 用于分配位图中像素的函数是这样的:

    colorFrame.CopyConvertedFrameDataToIntPtr(
                                this.colorBitmap.BackBuffer,
                                (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4),
                                ColorImageFormat.Bgra);
    

    1)使用Reader_ColorFrameArrived从kinect获取帧(参见ColorSamples - WPF);

    2)使用Bgra格式从ColorFrameSource创建colorFrameDescription;

    3)创建要显示的位图;

    如果您有任何问题,请说 . 但是,如果你按照样品,它实际上很干净,如何做到这一点 .

  • 1

    我永远坚持这个问题 . 问题是,您找到的几乎所有示例都是WPF示例 . 但对于Windows Forms来说,它是另一回事 .

    frame.CopyRawFrameDataToArray(imgBuffer);
    

    得到你的原始数据

    ColorImageFormat.Yuy2
    

    通过将其转换为RGB,您应该能够修复颜色问题 . 从YUY2到RGB的转换非常昂贵,您可能希望使用并行foreach循环来维持帧速率

相关问题