我使用下面的代码从相机读取每一帧并将其推送到 vector 。我这样做是为了稍后处理 vector 中所有收集的帧。
我使用下面的代码从相机收集帧。但在 2005 帧之后,代码会抛出以下错误。
OpenCV Error: Insufficient memory (Failed to allocate 921604 bytes) in OutOfMemoryError, file D:\Opencv\modules\core\src\alloc.cpp, line 52
下面是我用来收集帧并将其推送到 vector 中的代码。
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture(0);
if(!capture.isOpened())
return -1;
vector <Mat> frame;
int delay = 10;
int count = 0;
Mat src;
while(true) {
capture >> src;
frame.push_back(src.clone());
imshow("VIDEO", src);
if( waitKey( delay ) >= 0) break;
count = count+1;
cout << count << endl;
}
/* I need to process each frame stored inside the vector*/
destroyWindow("VIDEO");
capture.release();
return 0;
}
最佳答案
您可能像错误提示的那样耗尽了内存。如果每帧是 640*480(即相当低的分辨率),则 2005 * 640 * 480 * 3(每像素字节数)= 1,847,808,000(即 1.8GB 的 RAM 被分配在一个连续的 block 中)。
同样,每次 vector 必须调整自身大小时,它都需要分配足够大的空间来容纳新数据,然后将旧数据复制到新 vector,然后释放旧分配,有效地使您需要的内存量增加一倍.
关于c++ - 将视频文件插入vector时vector内存不足?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16419632/