从Kinect保存的图像是黑色的

3

我尝试将从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;
}

我在示例代码中添加了三行,这些行在Update函数中有注释标记。我还尝试将Update更改为LateUpdate,但没有任何变化。


如果您尝试记录Color32[] src中的任何值,您是否会得到预期的颜色值(基本上不是全部为0,0,0)? - Serlite
不,它们并不全是0。我将纹理(一张图片和一个深度)映射到两个平面上,我可以在平面上看到图像。 - D3GAN
嗯...你在遍历数组时,记录一下你所获取的值怎么样?TLidx、TRidx、BLidx、BRidx 获取了哪些范围内的数值?它们也是在 0-255 变化吗? - Serlite
@Serlite 值不在0-255之间。我得到的是这样的:247040 247041 247680 247681。 - D3GAN
1个回答

0

Kinect的示例代码创建纹理如下:

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

将其更改为:

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

解决了问题。 在这个链接中声称EncodeToPNG函数可以处理ARGB32和RGB24,但事实并非如此!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接