opencv 2.4.11 BackgroundSubtractorMOG2 problems with background and foreground

asked 2016-08-26 11:22:51 -0500

Eng4OpenCV gravatar image

I'm having problems getting BackgroundSubtractorMOG2 to work.

I'm using:

1) OpenCV 2.4.11 (x64)

2) Visual Studio 2010

Here are 2 problems I'm experiencing:

1) The foreground (e.g. fgMask) and background (e.g. bgImg) don't seem correct. The background image is identical to the current image (e.g. frame), and the foreground image looks like it might only have been computed from a single frame.

2) When I include the line mog2.set("detectShadows",false); the fgMask output is all white (i.e. 255).

Sample Code:

double learningRate = 0.01;  //I've tried a few other values too
cv::BackgroundSubtractorMOG2 mog2;
//mog2.set("detectShadows",false);  //including this line causes fgMask to be all white
mog2.set("nShadowDetection", 20);

for each image fileName    //replaced C++ for conditions line w/ psuedocode for simplicity
{
    cv::Mat frame;
    frame = cv::imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
    if( frame.empty())
    {
        std::cout << "frame is empty" << std::endl;
        break;
    }
    cv::Mat fgMask;
    mog2(frame, fgMask, learningRate);

    //display images
    cv::imshow("original", frame);
    cv::imshow("fgMask", fgMask);
    cv::imshow("bgImg", bgImg);
    keyboard = cv::waitKey(10000);
    cv::destroyAllWindows();
}
edit retag flag offensive close merge delete