【问题标题】:image problems with webcamtexture网络摄像头纹理的图像问题
【发布时间】:2017-10-17 21:44:16
【问题描述】:

我在 Unity 工作中遇到了一个小问题,我正在尝试从网络摄像头纹理中获取图像,并将其保存在一个字节 [] 中。当我显示图像时出现问题,它看起来不正确,它看起来像某种类型的网格。我认为问题在于我用来传递像素的原因。我希望你能帮助我。

    void Update()
{

    //texto.text = texto.text +  width +" / "+ height + " / " + ini.ToString()+ " mal: "+ testo().ToString()+ " SizeIMG: "+ imgData.Length + " NUEVO: "+ nuevo +"\n";

    imgData = null;
    imgData = new byte[width * height * 3];
    resultado = null;
    resultado = new byte[width * height * 3];

    color = webcam.GetPixels32();
    for (int i = 0; i < color.Length; i += 3)
    {
        imgData[(i * 3)] = color[i].r;
        imgData[(i * 3) + 1] = color[i].g;
        imgData[(i * 3) + 2] = color[i].b;

    }
    color = null;

    //video(imgData, resultado);

    //ProcessFrame(imgData, resultado, 0, 0, 0,0, nuevo);
    nuevo = false;

    textura2 = new Texture2D(width, height, TextureFormat.RGB24, false);
    textura2.LoadRawTextureData(imgData);
    textura2.Apply();

    //Left IMAGE
    renderer.material.mainTexture = textura2;
    textura2 = null;

    RightImage.GetComponent<Renderer>().material.mainTexture = webcam;

    if (kont == 30)
    {
        texto.text = "";
        kont = 0;
    }
    kont++;

    Resources.UnloadUnusedAssets();
}

【问题讨论】:

  • 您确定图像是每像素 3 位而不是 4 位吗?
  • 我认为它是 3bits,RGB 格式,但我不确定。
  • 顺便说一句,图片尺寸和color.length的值是多少?
  • 图像尺寸它是相机分辨率的尺寸,在本例中为 640x480。我假设 webcamtexture 是 RGB 图像格式,所以 640x480x3 是 imgData 的尺寸。 color 是 Color32 结构,它包含一个数组中的图像像素
  • 不相关但不要每帧都调用Resources.UnloadUnusedAssets

标签: c# unity3d


【解决方案1】:

我认为问题在于您如何索引到您的 imgData 数组。除了乘以 3 之外,您还要将 i 增加 3。

int bytesPerPixel = 3;
const int indexR = 0;
const int indexG = 1;
const int indexB = 2;
for(var i = 0; i < color.Length; i++)
{
    imgData[(i * bytesPerPixel) + indexR] = color[i].r;
    imgData[(i * bytesPerPixel) + indexG] = color[i].g;
    imgData[(i * bytesPerPixel) + indexB] = color[i].b;
}

【讨论】:

  • oooooooo 它有效,现在我看到了失败。我在 3 中添加了 3。谢谢
  • @UrkoSanchezOrtiz 没问题!如果问题已解决,请随意投票并标记为已接受
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 2011-08-16
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多