OpenCV的CV ::垫造成潜在的内存泄漏的std ::矢量 [英] OpenCV cv::Mat causing potential memory leak with std::vector

查看:3345
本文介绍了OpenCV的CV ::垫造成潜在的内存泄漏的std ::矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为它的立场,现在我试着去保存图像的整个列表CV ::垫的形式,为以后处理的载体里面。现在我有一些看起来是这样的:

As it stands right now im trying to save an entire list of images in the form of cv::Mats inside of a vector for later processing. Right now I have something that looks like this:

do
{
 image = readimage();
 cv::Mat mat = cv::Mat((length, width, CV_8UC4, image));
 cv::Mat temp = mat.clone();
 saved_images.push_back();

 mat.release();
 temp.release();
 freeimagememory(image);
}
while(hasimage);

这实际工作。对于图像的超小名单它将存储他们就好了。然而,由于我得到大量图像的程序崩溃一贯中止说()被调用,经查它说,它的投掷简历::例外。

This actually works. For exceptionally small lists of images it will store them just fine. However as I get to large amounts of images the program consistently crashes saying Abort() was called, and upon inspection it says it's throwing a cv::exception.

有谁知道这是为什么?我想过,为了节省空间,改变矢量指针的向量CV ::垫(克隆似乎昂贵的),但我不知道如何将工作。

Does anyone know why this is? I've thought about changing the vector to a vector of pointers to cv::Mats in order to save space (cloning seems expensive) but I'm not sure how well that will work.

谁能帮助?

EDIT1:被抛出确切的错误是未能分配的X字节。我想这是因为它是吃了所有的可用内存以某种方式(即使我坐在内存8音乐会,肯定有免费的内存)。

The exact error being thrown is failed to allocated X bytes. I assume this is because it's eating up all of the available memory somehow (even though I'm sitting on 8 gigs of memory and definitely have memory free).

EDIT2:

下面code也似乎工作。

The below code also seems to work.

std::vector<cv::Mat*> ptrvec;
do{

 image.readimage();
 ptrvec.push_back(new cv::Mat((length, width, CV_8UC4, image)));
 freeimagememory(image);
}
while(hasimage);

这一个不与内存出现问题(我可以把所有我想它的图像),但我得到一个访问冲突时,我尝试做

This one doesn't have a problem with memory (I can push all the images I want to it) but I get an access violation when I try to do

cv::imshow("Test Window", *ptrvec[0]);

EDIT3:

有机会我打32位的上限?我有能力重新编译成64位的项目如此多。

Is there a chance I'm hitting the upper limit of 32 bit? I'm more than capable of recompiling this into a 64 bit project.

推荐答案

您可能会耗尽内存,当你存储3000彩色图像的800×600的载体。存储在内存中垫球不会解决你的问题,因为数据是在RAM中还是分配。

You may be running out of memory when you store 3000 color images 800 x 600 in a vector. Storing Mat pointers in memory will not solve your problem, since the data is still allocated in RAM.

检查是否有足够的内存在系统中存储​​的所有图像。如果没有,你可以批量上传图像,例如,过程中的第500张图片,然后处理下一个500张图片,等等。

Check whether there is enough memory in your system to store all the images. If not you can upload images in batches, for example, process the first 500 images, then process the next 500 images, etc.

在你的程序你分配上的的载体。分配上的的,当你需要的内存(你的情况)的大块建议。所以,可以尝试在堆上分配的,而不是载体(前提是你有足够的内存来存储载体)。见,叠堆VS 或的有关详细信息,这CPP-教程

In your program you allocate the vector on the stack. Allocating on the heap is recommended when you need a large block of memory (your case). So can try to allocate the vector on the heap instead (provided that you have enough memory to store the vector). See, stack vs heap, or this cpp-tutorial for more information.

这篇关于OpenCV的CV ::垫造成潜在的内存泄漏的std ::矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆