来自Kinect的保存图像是黑色的



我试图将从kinect收到的图像保存为png。我从包装中捡起了一个Kinect样品,该样品在两架飞机上显示了深度和颜色图片,然后对其进行了修改。我尝试了不同的方法,例如直接保存color32或将其转移到其他纹理中,但没有任何作用。请注意,我可以在Unity场景中看到两个在两架飞机上显示的图像。这是我必须保存图像的代码。

void Update () {
    if (kinect.pollColor())
    {
        tex.SetPixels32(mipmapImg(kinect.getColor(),640,480));
        // Code by me to save the image
        byte[] bytes = tex.EncodeToPNG();
        File.WriteAllBytes("screenshots/testscreen-" + imageCount + ".png", bytes);
        imageCount++;
        //
        tex.Apply(false);
    }
}
private Color32[] mipmapImg(Color32[] src, int width, int height)
{
    int newWidth = width / 2;
    int newHeight = height / 2;
    Color32[] dst = new Color32[newWidth * newHeight];
    for(int yy = 0; yy < newHeight; yy++)
    {
        for(int xx = 0; xx < newWidth; xx++)
        {
            int TLidx = (xx * 2) + yy * 2 * width;
            int TRidx = (xx * 2 + 1) + yy * width * 2;
            int BLidx = (xx * 2) + (yy * 2 + 1) * width;
            int BRidx = (xx * 2 + 1) + (yy * 2 + 1) * width;
            dst[xx + yy * newWidth] = Color32.Lerp(Color32.Lerp(src[BLidx],src[BRidx],.5F),
                                                   Color32.Lerp(src[TLidx],src[TRidx],.5F),.5F);
        }
    }
    return dst;
}

我在示例代码中添加了三行,我以更新功能中的注释为标记。我还尝试将更新更改为liteupdate,但没有任何更改。

kinect示例代码正在创建这样的纹理:

tex = new Texture2D(320,240,TextureFormat.ARGB32,false);

将其更改为:

tex = new Texture2D(320,240,TextureFormat.RGB24,false);

解决了问题。在此链接中,它声称encodetopng函数将在ARGB32和RGB24上都起作用,但似乎并非如此!

最新更新