Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to take average every X frames

Hi, I want to take average of every 15 images

This is currently what I have:

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<cv::Mat> images(9000);
    for (framenumb = 0; framenumb < 9000; ++framenumb)
    {
        Mat frame;
        cap >> frame;
        if (frame.empty()) break; // end of video stream
        if (waitKey(1) == 27) break; // stop capturing by pressing ESC 
        images[framenumb].create(480, 640, CV_8UC3);
        frame.copyTo(images[framenumb]);
        imshow("webcam", images[framenumb]);
    }
    for (int i = 0; i < 600; i++)
            {
                Mat avgImg(480, 640, CV_32FC3, Scalar());
                for (framenumb = 15 * i; framenumb < (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);

However, this will need me to create a vector of 9000 images, which is a bit excessive. Is there a way for me to accomplish this without having to use a vector?