Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you should not cache thousands of images in a vector to achieve this, rather use a ringbuffer for the desired accumulation, and do it all online

c++ also has more containers than vector, we can use a std::deque here, and simply push into one end, and pop the other (fifo)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <deque>

using namespace cv;
using namespace std;


int main()
{
    std::deque<cv::Mat> queue;

    VideoCapture cap(0); 
    if (!cap.isOpened())
        return -1;

    while(true)
    {
        Mat frame;
        cap >> frame;
        if (frame.empty()) break;

        queue.push_back(frame);
        if (queue.size() > 15) 
        {
            queue.pop_front();

            Mat avgImg(frame.size(), CV_32FC3, Scalar());
            for (int i=0; i<15; i++)
                accumulate(queue[i], avgImg);

            avgImg.convertTo(avgImg, CV_8UC3, 1.0/15);
            imshow("avg", avgImg);
        }

        imshow("webcam", frame);
        if (waitKey(10) > 0) break;
    }
    return EXIT_SUCCESS;
}

you should not cache thousands of images in a vector to achieve this, rather use a (short) ringbuffer for the desired accumulation, and do it all online

c++ also has more containers than vector, we can use a std::deque here, and simply push into one end, and pop the other (fifo)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <deque>

using namespace cv;
using namespace std;


int main()
{
    std::deque<cv::Mat> queue;

    VideoCapture cap(0); 
    if (!cap.isOpened())
        return -1;

    while(true)
    {
        Mat frame;
        cap >> frame;
        if (frame.empty()) break;

        queue.push_back(frame);
        if (queue.size() > 15) 
        {
            queue.pop_front();

            Mat avgImg(frame.size(), CV_32FC3, Scalar());
            for (int i=0; i<15; i++)
                accumulate(queue[i], avgImg);

            avgImg.convertTo(avgImg, CV_8UC3, 1.0/15);
            imshow("avg", avgImg);
        }

        imshow("webcam", frame);
        if (waitKey(10) > 0) break;
    }
    return EXIT_SUCCESS;
}

you should not cache thousands of images in a vector to achieve this, rather use a (short) ringbuffer for the desired accumulation, and do it all online

c++ also has more containers than vector, we can use a std::deque here, and simply push into one end, and pop the other (fifo)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <deque>

using namespace cv;
using namespace std;


int main()
{
    std::deque<cv::Mat> queue;
ring;

    VideoCapture cap(0); 
    if (!cap.isOpened())
        return -1;

    while(true)
    {
        Mat frame;
        cap >> frame;
        if (frame.empty()) break;

        queue.push_back(frame);
ring.push_back(frame);
        if (queue.size() (ring.size() > 15) 
        {
            queue.pop_front();
ring.pop_front();

            Mat avgImg(frame.size(), CV_32FC3, Scalar());
            for (int i=0; i<15; i++)
                accumulate(queue[i], accumulate(ring[i], avgImg);

            avgImg.convertTo(avgImg, CV_8UC3, 1.0/15);
            imshow("avg", avgImg);
        }

        imshow("webcam", frame);
        if (waitKey(10) > 0) break;
    }
    return EXIT_SUCCESS;
}

you should not cache thousands of images in a vector to achieve this, rather use a (short) ringbuffer for the desired accumulation, and do it all online

c++ also has more containers than vector, we can use a std::deque here, and simply push the most recent image into one end, and pop the oldest image at the other end (fifo)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <deque>

using namespace cv;
using namespace std;


int main()
{
    std::deque<cv::Mat> ring;

    VideoCapture cap(0); 
    if (!cap.isOpened())
        return -1;

    while(true)
    {
        Mat frame;
        cap >> frame;
        if (frame.empty()) break;

        ring.push_back(frame);
        if (ring.size() > 15) 
        {
            ring.pop_front();

            Mat avgImg(frame.size(), CV_32FC3, Scalar());
            for (int i=0; i<15; i++)
                accumulate(ring[i], avgImg);

            avgImg.convertTo(avgImg, CV_8UC3, 1.0/15);
            imshow("avg", avgImg);
        }

        imshow("webcam", frame);
        if (waitKey(10) > 0) break;
    }
    return EXIT_SUCCESS;
}