Background Subtraction
// Create MOG Background Subtractor object
pMOG= cv::bgsegm::createBackgroundSubtractorMOG();//new BackgroundSubtractorMOG();
// Create MOG2 Background Subtractor object
pMOG2 = createBackgroundSubtractorMOG2(20, 16, true);//new BackgroundSubtractorMOG2();
// Capture the current frame
cap >> frame;
// Resize the frame
resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER\_AREA);
// Update the MOG background model based on the current frame
pMOG->apply(frame, fgMaskMOG);
// Update the MOG2 background model based on the current frame
pMOG2->apply(frame, fgMaskMOG2);
// Show the current frame
//imshow("Frame", frame);
// Show the MOG foreground mask
//imshow("FG Mask MOG", fgMaskMOG);
// Show the MOG2 foreground mask
imshow("FG Mask MOG 2", fgMaskMOG2);
Frame Differencing
Mat frameDiff(Mat prevFrame, Mat curFrame, Mat nextFrame)
{
Mat diffFrames1, diffFrames2, output;
// Compute absolute difference between current frame and the next frame
absdiff(nextFrame, curFrame, diffFrames1);
// Compute absolute difference between current frame and the previous frame
absdiff(curFrame, prevFrame, diffFrames2);
// Bitwise "AND" operation between the above two diff images
bitwise\_and(diffFrames1, diffFrames2, output);
return output;
}