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