我试着在新的Unity3D UI系统上渲染Emgu相机捕捉到的图像。到目前为止,我使用了这个存储库中的ImageToTexture2d:https://github.com/neutmute/emgucv/blob/3ceb85cba71cf957d5e31ae0a70da4bbf746d0e8/Emgu.CV/PInvoke/Unity/TextureConvert.cs,然后使用Sprite.Create()来实现想要的结果。
但!它似乎有一些巨大的内存泄漏,因为在我的游戏运行2-3分钟后,团结编辑器突然拿出大约3GB的内存,它开始时大约200 my。
我有两个展望:
你们中有谁知道将Emgu的图像转换为Sprite/纹理(不使用InterOp)的其他方法,或者我可以在上显示它的任何其他方式。它必须是Emgu的图像,因为我也做一些操作,我从相机收到的图像。
提前感谢您的回复和帮助。:D
发布于 2015-03-30 23:04:48
经过一些研究,我发现了问题所在,但没有时间描述。我不知道每个框架的纹理都保存在引擎的某个地方。在从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));
}发布于 2015-01-22 10:44:10
在不知道游戏的大部分内容的情况下,您是否正在销毁内存中创建的对象的可能复制?
3GB的突然增长是否与游戏中的任何行为有关?即使没有活动,它也会增加吗?
发布于 2018-04-30 10:15:47
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;
}https://stackoverflow.com/questions/27914042
复制相似问题