The Mixture of Gaussians approach

Before we talk about Mixture of Gaussians (MOG), let's see what a mixture model is. A mixture model is just a statistical model that can be used to represent the presence of subpopulations within our data. We don't really care about what category each data point belongs to. All we need to do is identify that the data has multiple groups inside it. If we represent each subpopulation using the Gaussian function, then it's called Mixture of Gaussians. Let's consider the following photograph:

Now, as we gather more frames in this scene, every part of the image will gradually become a part of the background model. This is what we discussed earlier in the Frame differencing section as well. If a scene is static, the model adapts itself to make sure the background model is updated. The foreground mask, which is supposed to represent the foreground object, looks like a black image at this point because every pixel is part of the background model.

OpenCV has multiple algorithms implemented for the Mixture of Gaussians approach. One of them is called MOG and the other is called MOG2: refer to this link for a detailed explanation: http://docs.opencv.org/master/db/d5c/tutorial_py_bg_subtraction.html#gsc.tab=0. You will also be able check out the original research papers that were used to implement these algorithms.

Let's wait for some time and then introduce a new object into the scene. Let's look at what the new foreground mask looks like, using the MOG2 approach:

As you can see, the new objects are being identified correctly. Let's look at the interesting part of the code (you can get the full code in the .cpp files):

int main(int argc, char* argv[])
{

// Variable declaration and initialization
....
// Iterate until the user presses the Esc key
while(true)
{
// Capture the current frame
cap >> frame;

// Resize the frame
resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);

// Update the MOG2 background model based on the current frame
pMOG2->apply(frame, fgMaskMOG2);

// Show the MOG2 foreground mask
imshow("FG Mask MOG 2", fgMaskMOG2);

// Get the keyboard input and check if it's 'Esc'
// 27 -> ASCII value of 'Esc' key
ch = waitKey( 30 );
if (ch == 27) {
break;
}
}

// Release the video capture object
cap.release();

// Close all windows
destroyAllWindows();

return 1;
}