gpt4 book ai didi

c# - WebCamTexture 图像底部的黑线

转载 作者:行者123 更新时间:2023-11-30 17:33:06 25 4
gpt4 key购买 nike

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

当在 Canvas 上显示视频、将快照保存到磁盘以及使用 GetPixels32() 直接查看像素数据时,黑线(总是 1 像素高且与图像一样宽)出现。

这是图片底部的黑线:

enter image description here

我已确认返回的图像尺寸正确,即黑色行不是额外的行。它始终是黑色图像的最低行。

我在下面包含了我正在使用的 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 的错误,您应该通过编辑器提交错误报告。

关于c# - WebCamTexture 图像底部的黑线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com