Reduce memory consumption
<p>I'm currently using this code to analyze webcam frames:</p>

<pre><code>int main()
{
    VideoCapture cap(0); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
        return -1;
    cap.set(CV_CAP_PROP_FPS, 15);

    std::vector&lt;cv::Mat&gt; images(9000);
    for (framenumb = 0; framenumb &lt; 9000; ++framenumb) 
    {
        images[framenumb].create(480, 640, CV_8UC3);
    }
    for (framenumb = 0; framenumb &lt; 9000; ++framenumb)
    {
        Mat frame;
        cap &gt;&gt; frame;
        if (frame.empty()) break; // end of video stream
        if (waitKey(1) == 27) break; // stop capturing by pressing ESC 
        frame.copyTo(images[framenumb]);
        imshow("webcam", images[framenumb]);
    }
    //We can then process the captured frames
</code></pre>

<p>However, it leads to insufficient memory error:</p>

<pre><code>OpenCV Error: Insufficient Memory (failed to allocate xyz bytes) in unknown function, file 
..\..\opecv\modules\core\src\alloc.cpp line 52.
</code></pre>

<p>Is there a way for me to avoid this? Thanks a lot in advanced!</p>

<p>Edit: Thanks for the all constructive feedback everyone! I guess all I can do for now is to change my approach to the problem. What I tried to accomplish was to take average of every 15 frames. For example: with 9000 frames, I will have 600 average pictures. Thus, I created a vector of 9000 images and use counter to achieve that.This is the code i used later:</p>

<pre><code>for (int i = 0; i &lt; 600; i++)
            {
                Mat avgImg(480, 640, CV_32FC3, Scalar());
                for (framenumb = 15 * i; framenumb &lt; (15 * i) + 15; ++framenumb)
                {
                    cv::accumulate(images[framenumb], avgImg);
                }
                //stacking every 15 images into a single image
                avgImg = avgImg / 15;
                avgImg.convertTo(avgImg, CV_8UC3);
                char filename[80];
                sprintf(filename, "C:/AvgPics/test_%d.jpeg", framenumb);
                imwrite(filename, avgImg);
</code></pre>

<p>So is there a way for me to specify opencv to take average of every 15 images without having to use a 9000-image vector?</p>