# Revision history [back]

### Using BackgroundSubtractorMOG2 for images

Hi,

I am pretty new to OpenCV and I am stuck at the moment. Since I will have same background in my project, I thought it would be easier to work, if I could remove my background. But first, I have to ask one thing. Can I use BackgroundSubtractorMOG2 for images? Because it is under video analysis/motion analysis title.

I read the documentation on opencv.org and looked through countless examples/tutorials but I am still having difficulty understanding how MOG2 works.

Quick question: What is history that in parameters?

So, I have written a simple code. I get a foreground mask. So, what is next step? How can I remove the background and left with my object only? Shouldn't I load my background first, then the actual image, so that MOG2 could do the background subtraction?

I am using OpenCV 2.4.11.

Code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>

using namespace cv;
using namespace std;

//global variables
int history = 1;
float varThreshold = 16;

Mat src; //source image
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor

int main(int argc, char* argv[])
{
//create GUI windows
namedWindow("Source");

//create Background Subtractor objects
pMOG2 = new BackgroundSubtractorMOG2(history, varThreshold, bShadowDetection); //MOG2 approach
pMOG2->setInt("nmixtures", 3);
pMOG2->setDouble("fTau", 0.5);