如何将EmguCV Image转换成Unity3D Sprite?

问题描述 投票:1回答:4

我尝试在新的Unity3D UI系统上渲染Emgu相机捕获的图像。直到现在,我使用了来自此存储库的ImageToTexture2d:https://github.com/neutmute/emgucv/blob/3ceb85cba71cf957d5e31ae0a70da4bbf746d0e8/Emgu.CV/PInvoke/Unity/TextureConvert.cs然后使用Sprite.Create()来最终实现想要的结果。

但!看起来有一些大规模的内存泄漏,因为在我的游戏运行2-3分钟后,Unity编辑器突然需要大约3GB的RAM,大约200MB。

我有两个嫌犯:

  1. (更多Probabble)我正在使用的方法不会清理内存。它使用InterOp并创建一些不安全的指针 - 它会闻到泄漏。
  2. 每个帧运行的Sprite.Create都会在内存中保存旧的sprite,并且不会删除它们。

你是否知道任何其他方式将Emgu的图像转换为Sprite / Texture(不使用InterOp)或任何其他方式我可以在New Unity的UI上显示它。它必须是Emgu的图像,因为我还对从相机中接收的图像进行了一些操作。

在此先感谢您的回复和帮助。 :d

c# unity3d emgucv unity3d-gui
4个回答
0
投票

经过一些研究,我发现了问题所在,但没有时间来描述它。我不知道Textures创建的每个帧都保存在引擎的某个地方。在从Emgu Image生成一个新的之前必须销毁它们。

这是我的项目中使用的代码的一部分:

//Capture used for taking frames from webcam
private Capture capture;
//Frame image which was obtained and analysed by EmguCV
private Image<Bgr,byte> frame;
//Unity's Texture object which can be shown on UI
private Texture2D cameraTex;

//...

if(frame!=null)
    frame.Dispose();
frame = capture.QueryFrame();
if (frame != null)
{
    GameObject.Destroy(cameraTex);
    cameraTex = TextureConvert.ImageToTexture2D<Bgr, byte>(frame, true);
    Sprite.DestroyImmediate(CameraImageUI.GetComponent<UnityEngine.UI.Image>().sprite);
    CameraImageUI.sprite = Sprite.Create(cameraTex, new Rect(0, 0, cameraTex.width, cameraTex.height), new Vector2(0.5f, 0.5f));
}

0
投票

在不了解游戏的大部分内容的情况下,您是否正在破坏在内存中创建的对象的可能复制?

突然3GB的增加是否与游戏中的任何行为有关?即使没有活动,它会增加吗?


0
投票
public static Texture2D ArrayToTexture2d(Image<Rgb, byte> picture) {
    Array bytes = picture.ManagedArray;
    int h = bytes.GetLength(0);
    int w = bytes.GetLength(1);
    Texture2D t2d = new Texture2D(w, h);
    double r, b, g;

    for (int heigth = 0; heigth < bytes.GetLength(0); heigth++)
    {
        for (int width = 0; width < bytes.GetLength(1); width++)
        {
            r = Convert.ToDouble(bytes.GetValue(heigth, width, 0));
            g = Convert.ToDouble(bytes.GetValue(heigth, width, 1));
            b = Convert.ToDouble(bytes.GetValue(heigth, width, 2));

            t2d.SetPixel(width, h - heigth - 1, new Color((float)r / 256, (float)g / 256, (float)b / 256, 1f));                
        }
    }

    t2d.Apply();
    return t2d;
}

0
投票
RawImage targetRawImage;
Image<Rgb, byte> scanned = new Image<Rgb, byte>("Assets/Textures/scanned.png");
Texture2D loaded = new Texture2D(scanned.Width, scanned.Height);
loaded.LoadImage(scanned.ToJpegData());
targetRawImage.texture = loaded;
© www.soinside.com 2019 - 2024. All rights reserved.