我尝试将从 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 但没有任何改变。
最佳答案
kinect 示例代码创建如下纹理:
tex = new Texture2D(320,240,TextureFormat.ARGB32,false);
将其更改为:
tex = new Texture2D(320,240,TextureFormat.RGB24,false);
解决了问题。 在这个link它声称 EncodeToPNG 函数适用于 ARGB32 和 RGB24,但事实似乎并非如此!
关于kinect - 从 kinect 保存的图像是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41169665/