简体   繁体   English

OpenCV在循环中调整大小-内存泄漏

[英]OpenCV resize in loop - memory leak

I'm trying to resize Mats stored in vector. 我正在尝试调整矢量中存储的Mat的大小。 As a result I'd like to have all resized Mats stored in original vector. 因此,我想将所有调整大小的Mats存储在原始矢量中。 Using resize(original_mat, original_mat ....) in for loop results in memory leak. 在for循环中使用resize(original_mat, original_mat ....)导致内存泄漏。 The same thing is when I use the code below - loop acts like the whole vector was copied and stored in memory. 当我使用下面的代码时,同样的事情-循环就像整个矢量都被复制并存储在内存中一样。

Mat temp;
for (int i=0; i< im.size();i++){
    resize(im[i],temp,size,0,0,CV_INTER_LINEAR);
    im[i] = temp.clone();
}

Is there a way to avoid copying whole vector and allocate memory only for single Mats? 有没有一种方法可以避免复制整个矢量并仅为单个Mats分配内存? Thanks in advance. 提前致谢。

Edit: I perform a lot of operations on my input frames before resizing them (for example - downsampling), so I tried to see what happens when I resize frames form vector right after reading the video. 编辑:在调整输入帧的大小之前,我会对输入帧执行很多操作(例如,降采样),因此我尝试查看在读取视频后立即将帧调整为矢量大小时会发生什么。 So, I made this: 所以,我做到了:

VideoCapture input_file(input_filename);
int number_of_frames = input_file.get(CV_CAP_PROP_FRAME_COUNT);
vector <Mat> im;
for (int i=0; i<number_of_frames; i++){
    Mat frame;
    bool isFrame = input_file.read(frame);
    im.push_back(frame);
    frame.release();
}
Mat temp;
for (int i=0; i< im.size();i++){
    resize(im[i],temp,Size,0,0,CV_INTER_LINEAR);
    im[i] = temp.clone();
}

Now memory leaks in first for loop, which makes my problem similar to this: out of memory when reading file (I'm also using OpenCV 3.0.0). 现在,内存在first for循环中泄漏,这使我的问题类似于此: 读取文件时内存不足 (我也使用OpenCV 3.0.0)。 So, I think that I'm just forced to avoid holding my whole video in a vector. 因此,我认为我只是被迫避免将整个视频保存在矢量中。

frame.release(); will cause problem where you are freeing the actual data of the frame. 在释放框架的实际数据时会引起问题。 The item in the vector will contain then just a header without data. 向量中的项目将仅包含没有数据的标头。

Notice that when you push_back an item to a vector, the copy constructor of that item is called. 请注意,当您将项目推入向量后,该项目的复制构造函数将被调用。 The copy constructor of the Mat is a shallow copy not a deep copy which means that only the header will be copied and no deep copy will happen. Mat的副本构造函数是浅表副本,而不是深表副本,这意味着将仅复制标题,而不会进行深表副本。 In other words, both Mat will reference the same actual data. 换句话说,两个Mat将引用相同的实际数据。

There must not be any memory leak. 不得有任何内存泄漏。 How you know there is a memory leak? 您如何知道内存泄漏? if you are using a profiler, it may be wrong. 如果您使用的是探查器,则可能是错误的。 If you are watching memory using and see it increasing significantly, it is normal because you are storing a video in the memory without any encoding. 如果您正在查看正在使用的内存,并且看到内存显着增加,那是正常现象,因为您正在将视频存储在内存中而没有任何编码。 Which means that each frame may be 5-6 MB with basic math : 5(MB)*30(FPS)*60(Sec)~ 9 GB per minute . 这意味着每个帧可能具有5-6 MB的基本数学运算能力: 每分钟 5(MB)* 30(FPS)* 60(Sec)〜9 GB So there is no memory leak, there is a huge data in the memory and they are not the same. 因此,没有内存泄漏,内存中有巨大的数据,它们是不一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM