c++ - 将视频文件插入vector时vector内存不足?

标签 c++ opencv image-processing computer-vision

我使用下面的代码从相机读取每一帧并将其推送到 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/

相关文章:

c++ - 一个奇怪的 C++ 错误,可能是相对于 long long 类型

python - 从图像中去除噪声线

javascript - 如何将详细图像加载为 HTML 背景?

java - 使用java自动旋转扫描的文档

c++ - 什么是常量无效?

c++ - 在 boost::program_options 解析我的命令行参数后,如何获取非标志和非选项标记

OpenCV:相机姿态估计

python - cv2.drawContours() - 取消填充字符内的圆圈(Python、OpenCV)

c++ - 将一个 cv::Mat 插入另一个会复制它吗?

c++ - "std::string const &s"和 "const std::string &s"有什么区别?