Background substraction using OpenCV MOG from live camera feed.
Hello, I am studying OpenCV and I'm having a lot of fun, but now I'm stuck with a problem where I'm trying to use a background subtraction algorithm to detect any changes.
I followed this tutorial and I managed to get it working to detect changes in a video file (AVI).
The problem I'm having right now is that it tends to incorrectly subtract the background noise and other small changes and instead fill the whole screen pretty much with white.
Here is my implementation of the MOG algorithm on a live camera feed, but the relevant part is this:
VideoCapture cap;
if (argc > 1)
cap.open(argv[1]);
else
cap.open(0);
cap.set(CV_CAP_PROP_FOURCC ,CV_FOURCC('D', 'I', 'V', '3') );
........
Mat frame, fgMaskMOG;
Ptr<BackgroundSubtractor> pMOG = new BackgroundSubtractorMOG();
for (;;)
{
if(!cap.read(frame)) {
cerr << "Unable to read next frame." << endl;
continue;
}
// process the image to obtain a mask image.
pMOG->operator()(frame, fgMaskMOG);
std::string time = getDateTime();
cv::rectangle(frame,cv::Rect(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-25,290,20),cv::Scalar(255,255,255),-1);
cv::putText(frame,time,cv::Point(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-5),1,1.5,cv::Scalar(0,0,0),2);
........
// show image.
imshow("Image", frame);
imshow("Debug",fgMaskMOG);
int c = waitKey(30);
if (c == 'q' || c == 'Q' || (c & 255) == 27)
break;
}
This implementation works just fine for a video file as you can see:
But this is the result when I try to use MOG on a live camera feed:
(EDIT:
EXPECTED RESULT: My expectation was the same as the video file (see pictures one and two above).
ACTUAL RESULT: The actual result was far from my expected result, much noise was generated (i.e not filtered out), when one put something in front of the camera, it would be black instead of white (inverse from the result of the video file).
- - - - SYSTEM DETAILS - - - -
OS: Windows x64bit
MEMORY AVAILABLE: 3890 MB
WEBCAM: I'm using my built-in webcam on my Satellite C660 TOSHIBA laptop.
COMPILER:
Microsoft Visual Studio Express 2012 for Windows Desktop
Version 11.0.61030.00 Update 4
Microsoft .NET Framework
Version 4.5.50948
OpenCV Version: OpenCV V. 2.4.9, built for Windows, downloaded from SourceForge.
OUTPUT FROM cv::getBuildInformation()
: OpenCV_BUILD.txt
Microsoft Visual Studio Project Property Sheet: OpenCV Project Property Sheet
)