【发布时间】:2017-06-24 09:40:48
【问题描述】:
当通过 WebCamTexture 从网络摄像头获取视频输入时,返回图像的底行是全黑的 (RGB = 0,0,0)。 我尝试了几种不同的网络摄像头,并得到了相同的结果。 在使用 Windows 10 相机应用程序以及在 Processing 或 Java 中获取网络摄像头源时,我确实得到了正确的图像。
在画布上显示视频、将快照保存到磁盘以及使用 GetPixels32() 直接查看像素数据时会出现黑线(总是 1 像素高和图像一样宽)。
这是图片底部的黑线:
我已确认返回的图像尺寸正确,即黑色行不是多余的行。始终是黑色的图像的最低线。
我在下面包含了我正在使用的 c# 代码。
这条黑线是什么原因造成的,有办法避免吗?
我已查找有关此问题的任何信息,但未在网上找到任何信息。我是 Unity 的初学者,如果能提供任何帮助,我将不胜感激。
我使用的是 Unity 版本 5.6.2,但在 5.5 中遇到了同样的问题
public class CamController : MonoBehaviour
{
private WebCamTexture webcamTexture;
private WebCamDevice[] devices;
void Start()
{
//start webcam
webcamTexture = new WebCamTexture();
devices = WebCamTexture.devices;
webcamTexture.deviceName = devices[0].name;
webcamTexture.Play();
}
void Update()
{
//if user presses C capture cam image
if (Input.GetKeyDown(KeyCode.C))
captureImage();
}
void captureImage()
{
//get webcam pixels
Color32[] camPixels;
camPixels = webcamTexture.GetPixels32();
//print pixel data for first and second (from bottom) lines of image to console
for (int y = 0; y < 2; y++)
{
Debug.Log("Line: " + y);
for (int x = 0; x < webcamTexture.width; x++)
{
Debug.Log(x + " - " + camPixels[y * webcamTexture.width + x]);
}
}
//save webcam image as png
Texture2D brightBGTexture = new Texture2D(webcamTexture.width, webcamTexture.height);
brightBGTexture.SetPixels32(camPixels, 0);
brightBGTexture.Apply();
byte[] pngBytes = brightBGTexture.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../camImage.png", pngBytes);
}
}
【问题讨论】:
-
您在
Unity中使用的shader是什么,用于显示来自相机源的Texture2D? -
我的目标不是显示相机源,而是直接使用图像像素数据而不显示图像。我只显示了用于测试的相机源,并且当时我没有指定着色器。我在 Canvas 上使用了 RawImage,此代码在其上显示相机输入
public RawImage camImage;camImage.texture = webcamTexture;我认为这不是着色器问题,因为黑色像素已经出现在GetPixels32()返回的像素数据中。