OpenCV Background subtraction by frame averaging

How do i implement background subtraction(background model obtained by averaging first..say 50 frames..) in opencv I tried looking for some codes but found they were in working in c++(visual studio 2013) A small working code snippet will help..thanks!


OpenCV provide background subtraction capabilities. See BackgroundSubtractorMOG2, that models the background with a Mixture of Gaussians, and is therefore quite robust to background changes.

The parameters history is the numbers of frame used to build the background model.

#include <opencv2\opencv.hpp>
using namespace cv;

int main(int argc, char *argv[])
    int history = 50;
    float varThreshold = 16.f; // default value
    BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(history, varThreshold);
    VideoCapture cap(0);
    Mat3b frame;
    Mat1b fmask;

    for (;;)
        cap >> frame;
        bg(frame, fmask, -1);

        imshow("frame", frame);
        imshow("mask", fmask);
        if (cv::waitKey(30) >= 0) break;               
    return 0;

Need Your Help

Setting log level of message at runtime in slf4j

java logging log4j slf4j

When using log4j, the Logger.log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect

Best way to implement a connection between a smartphone and a webpage on a pc

android ios network-programming

I am looking for an existing library to establish a connection between any smartphone(android/ios/win phone) and a web page.