首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将EmguCV图像转换成Unity3D雪碧?

如何将EmguCV图像转换成Unity3D雪碧?
EN

Stack Overflow用户
提问于 2015-01-13 02:26:43
回答 4查看 1.1K关注 0票数 1

我试着在新的Unity3D UI系统上渲染Emgu相机捕捉到的图像。到目前为止,我使用了这个存储库中的ImageToTexture2d:https://github.com/neutmute/emgucv/blob/3ceb85cba71cf957d5e31ae0a70da4bbf746d0e8/Emgu.CV/PInvoke/Unity/TextureConvert.cs,然后使用Sprite.Create()来实现想要的结果。

但!它似乎有一些巨大的内存泄漏,因为在我的游戏运行2-3分钟后,团结编辑器突然拿出大约3GB的内存,它开始时大约200 my。

我有两个展望:

  1. (更可能)我使用的方法没有清理内存。它使用InterOp并创建一些不安全的指针--它闻起来有泄漏的味道。
  2. Sprite.Create运行每个框架,在内存中保存旧的精灵,并且不删除它们。

你们中有谁知道将Emgu的图像转换为Sprite/纹理(不使用InterOp)的其他方法,或者我可以在上显示它的任何其他方式。它必须是Emgu的图像,因为我也做一些操作,我从相机收到的图像。

提前感谢您的回复和帮助。:D

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-03-30 23:04:48

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

下面是我在项目中使用的代码的一部分:

代码语言:javascript
复制
//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
EN

Stack Overflow用户

发布于 2015-01-22 10:44:10

在不知道游戏的大部分内容的情况下,您是否正在销毁内存中创建的对象的可能复制?

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

票数 0
EN

Stack Overflow用户

发布于 2018-04-30 10:15:47

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27914042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档