Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 网络摄像头纹理图像底部的黑线_C#_Unity3d_Webcam - Fatal编程技术网

C# 网络摄像头纹理图像底部的黑线

C# 网络摄像头纹理图像底部的黑线,c#,unity3d,webcam,C#,Unity3d,Webcam,通过WebCamTexture从网络摄像头获取视频输入时,返回图像的底行完全为黑色(RGB=0,0,0)。 我试过几种不同的网络摄像头,结果都一样。 在使用Windows10摄像头应用程序时,以及在Processing或Java中获取网络摄像头提要时,我确实获得了正确的图像 在画布上显示视频、将快照保存到磁盘以及使用GetPixels32()直接查看像素数据时,会出现黑线(始终为1像素高,与图像一样宽) 这是图片底部的黑线: 我已确认返回的图像大小正确,即黑行不是额外的一行。它始终是图像的最低

通过WebCamTexture从网络摄像头获取视频输入时,返回图像的底行完全为黑色(RGB=0,0,0)。 我试过几种不同的网络摄像头,结果都一样。 在使用Windows10摄像头应用程序时,以及在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);
    }
}
公共类控制器:单行为
{
私有网络摄像机纹理网络摄像机纹理;
专用网络摄像机设备[]设备;
void Start()
{
//启动网络摄像头
webcamTexture=新的webcamTexture();
设备=WebCamTexture.devices;
webcamTexture.deviceName=设备[0]。名称;
webcamTexture.Play();
}
无效更新()
{
//如果用户按下C键,则捕获cam图像
if(Input.GetKeyDown(KeyCode.C))
captureImage();
}
无效捕获图像()
{
//获取网络摄像头像素
颜色32[]坎皮克斯;
camPixels=webcamTexture.GetPixels32();
//将图像第一行和第二行(从底部)的像素数据打印到控制台
对于(int y=0;y<2;y++)
{
调试日志(“行:+y”);
对于(int x=0;x
调用
SetPixels32
后,您必须调用将更改应用于
纹理2D

在将
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的一个bug,您应该通过编辑器提交bug报告。

调用Apply()在这里没有帮助。Apply将更改后的像素上载到图形卡,在编码为png时不需要。我通过尝试你的建议证实了这一点。另外,正如我前面提到的,即使直接(在编码完成之前)查看GetPixels32()返回的像素数据,也会出现黑线。通过查看,我的意思是将像素数据打印到控制台或同等设备上。“在编码为png时不需要”否。这是必要的。是的,它确实将更改上传到图形卡,它还将更改应用到纹理2D上,以便更新新像素。通过调用Apply,您现在可以访问CPU上的像素,包括调用
EncodeToPNG
函数。有关更多信息,请参阅链接的文档。另外,哪个图像是黑色的?保存的png文件?好的,我理解,
Apply()
将更改应用于GPU和CPU(我不知道这一点)。这不是整个图像是黑暗的,只有底线(1像素高的线)。此黑线不仅出现在保存的png中,而且直接出现在通过
GetPixels32()
从WebCamTexture返回的Color32数组中,此数组中表示图像底线的元素都具有值RGBA=(0,0,0255)。确定。这是在编辑器还是构建中发生的?你能在构建中尝试一下吗?你在
Unity
中使用的
shader
是什么来显示来自摄影机提要的
Texture2D
?我的目标不是显示摄影机提要,而是直接使用图像像素数据而不显示图像。我只显示了用于测试的摄影机提要,当时没有指定着色器。我在画布上使用了一个RawImage,这段代码在上面显示摄像头提要
publicRawImage camImage
camImage.texture=webcamTexture
我认为这不是着色器问题,因为黑色像素已经出现在
GetPixels32()返回的像素数据中。