【发布时间】: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