【问题标题】:saved images from kinect are black从kinect保存的图像是黑色的
【发布时间】: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


【解决方案1】:

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

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

改成:

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

解决了这个问题。 在这个link 中,它声称 EncodeToPNG 函数可以在 ARGB32 和 RGB24 上工作,但似乎情况并非如此!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多