2vi*_*on2 1 c++ opencv image-processing computer-vision
我使用下面的代码从相机读取每个帧并将其推送到矢量.这样做是为了在稍后的时间点处理向量中的所有收集的帧.
我用下面的代码从相机中收集帧.但是在2005帧之后,代码会抛出以下错误.
OpenCV错误:OutOfMemoryError中的内存不足(无法分配921604字节),文件D:\ Opencv\modules\core\src\alloc.cpp,第52行
下面是我用来收集帧并将其推入向量的代码.
#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;
}
Run Code Online (Sandbox Code Playgroud)
您可能正在耗尽内存,就像错误所暗示的那样.如果每帧是640*480(即非常低的res),则2005*640*480*3(每像素字节数)= 1,847,808,000(即在连续块中分配1.8GB的RAM).
同样,每次向量必须调整自身大小时,它需要进行足够大的分配来保存新数据,然后将旧数据复制到新向量,然后释放旧分配,从而有效地将所需内存量增加一倍.