kinect - 从 kinect 保存的图像是黑色的

标签 kinect unity-game-engine texture2d

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

相关文章:

c# - 使用 SlimDX 从 RGBA 值创建纹理

android - 在 OpenCL 内核中读取 GL_UNSIGNED_BYTE OpenGL texture2D (android)

C# XNA 如何更改整个 Texture2D 的 alpha?

kinect - 可以将 Microsoft Kinect 与 .NET Gadgeteer 一起使用吗?

kinect - 使用网络摄像头/第二个网络摄像头注册/覆盖 kinect 深度配置文件

c# - 为什么主机玩家没有权限发送服务器【命令】?统一-镜子

c# - 如何从另一个脚本访问任何创建的游戏对象的属性 Unity C# 2D

java - SimpleOpenNI 记录和回放用户跟踪数据

c# - Kinect:计算头部的俯仰角、偏航角、滚动角

android - 第一次播放时Unity动画卡住