【问题标题】:Black line at bottom of WebCamTexture imageWebCamTexture 图像底部的黑线
【发布时间】: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() 返回的像素数据中。

标签: c# unity3d webcam


【解决方案1】:

调用SetPixels32 后,您必须调用Texture2D.Apply 将更改应用到Texture2D

在将Texture2D 编码为 png 之前,您应该这样做。

//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);

编辑

即使调用Texture2D.Apply(),问题仍然存在。这是WebCamTexture API 的一个错误,您应该通过编辑器提交错误报告。

【讨论】:

  • 调用 Apply() 在这里没有帮助。 Apply 将更改的像素上传到显卡,在编码为 png 时不需要。我已经通过尝试您的建议确认了这一点。此外,正如我之前提到的,即使直接(在完成任何编码之前)查看 GetPixels32() 返回的像素数据,黑线也存在。通过查看,我的意思是将像素数据打印到控制台或等效设备。
  • “编码为 png 时不需要” 不。有必要。是的,它确实会将更改上传到显卡,它还会将更改应用于 Texture2D,以便更新新像素。通过调用 Apply,您现在可以访问 CPU 上的像素,包括调用 EncodeToPNG 函数。有关详细信息,请参阅链接文档。
  • 另外,哪个图像是暗的?保存的 png 文件?
  • 好的,我明白了,Apply() 将更改应用于 GPU 和 CPU(我不知道这一点)。不是整个图像变暗,只有底线(1 像素高线)。这条黑线不仅出现在保存的 png 中,而且直接出现在 GetPixels32() 从 WebCamTexture 返回的 Color32 数组中,该数组中表示图像底线的元素都有值 RGBA = (0,0, 0,255)。
  • 好的。这发生在编辑器或构建中吗?你能在构建中试试这个,看看会发生什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 2017-10-11
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多