2016-12-15 76 views
3

我试图将从Kinect收到的图像保存为png。我从包装中拿出一个kinect样本,它在两个平面上显示深度和颜色图片,并对其进行修改。我尝试了不同的方法,例如直接保存color32或将其转换为其他纹理,但没有任何效果。注意我可以在Unity场景的两个平面上看到两个图像。这是我必须保存图像的代码。从kinect保存的图像是黑色

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; 
} 

我添加三行示例代码,我通过在更新功能的注释标记。我也尝试将更新更改为LateUpdate,但没有任何更改。

+0

如果您尝试记录来自'Color32 [] src'的任何值,是否会返回预期的颜色值(基本上不是全部0,0,0)? – Serlite

+0

不是所有的都是0。我将纹理(一张图片和一个深度)映射到两个平面上,我可以在平面上看到图像。 – D3GAN

+0

嗯......如何在您通过数组遍历时记录检索的值?您为TLidx,TRidx,BLidx,BRidx获得什么样的价值?也在0-255之间变化? – Serlite

回答

0

Kinect的样本代码是这样创建的质感:

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

其更改为:

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

问题解决了。 在这link它声称EncodeToPNG函数将同时在ARGB32和RGB24上工作,但它似乎并非如此!

相关问题