【问题标题】:Texture2D.LoadImage() with EmguCV on Unity在 Unity 上使用 EmguCV 的 Texture2D.LoadImage()
【发布时间】:2014-03-21 14:23:46
【问题描述】:

我正在使用 EmguCV 的 Capture 类,使用 LoadImage 函数将来自相机的图像放在 Texture2D 上。在尝试这样做之前,我正在使用 SetPixel 函数,但它太慢了。

当我执行下面的代码时,会出现一个白色背景上的红色问号,而不是相机的图像。

我在这里做错了什么?

public class testEmguCV : MonoBehaviour
{
    private Capture capture;

    void Start() 
    {
        capture = new Capture();
    }

    void Update()
    {
        Image<Gray, Byte> currentFrame = capture.QueryGrayFrame();
        Texture2D camera = new Texture2D(400, 400);
        if (currentFrame != null)
        {
            camera.LoadImage(currentFrame.Bytes);
            renderer.material.mainTexture = camera;
        }
     }
}

【问题讨论】:

  • 通过快速搜索,LoadImage 似乎用于将 Jpgs 和 Pngs 转换为 Texture2d。鉴于您使用的是 emgu 图像类,我怀疑这会起作用...也许将图像作为 jpg 保存到内存流中,然后将其扔到 LoadImage 中?
  • 谢谢史蒂文,我做到了,现在可以了。

标签: c# unity3d camera emgucv texture2d


【解决方案1】:

以下是我为纠正问题所做的修改:

public class testEmguCV : MonoBehaviour
{
    private Capture capture;

    void Start() 
    {
        capture = new Capture();
    }

    void Update()
    {
        Image<Gray, Byte> currentFrame = capture.QueryGrayFrame();
        Bitmap bitmapCurrentFrame = currentFrame.ToBitmap();
        MemoryStream m = new MemoryStream();
        bitmapCurrentFrame.Save(m, bitmapCurrentFrame.RawFormat);

        Texture2D camera = new Texture2D(400, 400);
        if (currentFrame != null)
        {
            camera.LoadImage(m.ToArray());
            renderer.material.mainTexture = camera;
        }
     }
}

【讨论】:

  • 我在使用您的代码时遇到此错误。有什么见解吗? ArgumentNullException:参数不能为空。参数名称:encoder System.Drawing.Image.Save(System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams) System.Drawing.Image.Save(System.IO.Stream流, System.Drawing.Imaging.ImageFormat 格式) (wrapper remoting-invoke-with-check) System.Drawing.Image:Save (System.IO.Stream,System.Drawing.Imaging.ImageFormat) Script.Update() (at资产/Script.cs:61)
  • 我解决了使用 Unity 安装文件夹中的 system.drawing.dll 时的问题。
猜你喜欢
  • 2014-04-16
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多