【发布时间】:2016-12-15 17:06:18
【问题描述】:
我尝试将从 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 函数中用 cmets 对其进行了标记。我也尝试将 Update 更改为 LateUpdate,但没有任何改变。
【问题讨论】:
-
如果您尝试记录来自
Color32[] src的任何值,您是否会返回预期的颜色值(基本上不是全部 0,0,0)? -
不,不全是 0。我将纹理(一张图片和一张深度)映射到两个平面,我可以在平面上看到图像。
-
嗯...在遍历数组时记录正在检索的值怎么样? TLidx、TRidx、BLidx、BRidx 的值范围是多少?也在 0-255 之间变化?
-
@Serlite 值不在 0-255 之间。我得到这样的信息:247040 247041 247680 247681。
标签: unity3d kinect unity5 texture2d