WebCamTexture 图像底部的黑线

Black line at bottom of WebCamTexture image

当通过 WebCamTexture 从网络摄像头获取视频输入时,返回图像的底行是全黑的 (RGB = 0,0,0)。 我尝试了几种不同的网络摄像头,但都获得了相同的结果。 使用 Windows 10 Camera 应用程序以及在 Processing 或 Java.

中获取网络摄像头时,我确实得到了正确的图像

在 canvas 上显示视频、将快照保存到磁盘以及使用 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);
    }
}

调用 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 的错误,您应该通过编辑器提交错误报告。