Ask Your Question
0

Background Subtraction using running average in opencv

asked 2016-05-19 08:58:26 -0600

MOB gravatar image

updated 2016-05-19 14:24:34 -0600

LBerger gravatar image

Hi, everybody ! i used Background Subtraction using running average, but i can't runnn ! thank you so much !!

#include "opencv2/opencv.hpp"

using namespace cv;

int main()
{
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
    return -1;

// Variables
Mat frame;
Mat accumulator;
Mat scaled;

// Initialize the accumulator matrix
accumulator = Mat::zeros(frame.size(), CV_32FC3);

while (1) {
    // Capture frame
    cap >> frame;

    // Get 50% of the new frame and add it to 50% of the accumulator
    accumulateWeighted(frame, accumulator, 0.5); //error in hereeeeee!

    // Scale it to 8-bit unsigned
    convertScaleAbs(accumulator, scaled);

    imshow("Original", frame);

    imshow("Weighted Average", scaled);

    waitKey(1);
}

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-05-19 15:06:22 -0600

LBerger gravatar image

Your accumulator is empty because your frame size is empty. You should do something like this:

VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
    return -1;

// Variables
Mat frame;
Mat accumulator;
Mat scaled;    // Initialize the accumulator matrix
cap >> frame; // Now frame is not empty
accumulator = Mat::zeros(frame.size(), CV_32FC3);

while (1) {
    // Capture frame
    cap >> frame;

    // Get 50% of the new frame and add it to 50% of the accumulator
    accumulateWeighted(frame, accumulator, 0.5); //now no error here (i hope so)
edit flag offensive delete link more

Comments

i think frame.convertTo(accumulator, CV_32FC3); is better

// Initialize the accumulator matrix
cap >> frame;
frame.convertTo(accumulator, CV_32FC3);
sturkmen gravatar imagesturkmen ( 2016-05-19 16:24:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-19 08:58:26 -0600

Seen: 3,341 times

Last updated: May 19 '16