Ask Your Question

Eng4OpenCV's profile - activity

2019-04-26 04:08:23 -0600 received badge  Popular Question (source)
2016-10-24 16:54:48 -0600 received badge  Teacher (source)
2016-10-24 16:54:44 -0600 received badge  Student (source)
2016-10-24 14:58:40 -0600 received badge  Self-Learner (source)
2016-10-24 14:03:20 -0600 received badge  Editor (source)
2016-10-24 13:58:29 -0600 answered a question Python cv2.BackgroundSubtractorMOG2 history parameter doesn't work

In order for the the history parameter to be used, the apply method needed to be called with the learningRate parameter set to -1 (actually anything less than or equal to zero should work). To see this, look in the source code file bgfb_gaussmix2.cpp in the BackgroundSubtractorMOG2::operator method definition. (Note, this operator method gets wrapped to the apply method for python). There you will find the following line:

learningRate = learningRate >= 0 && nframes > 1 ? learningRate : 1./min( 2*nframes, history );

If you explicitly set the learningRate to a value between 0 and 1 on each call to apply, then when nframes is greater than 1 the method will use the learningRate you specified and history will not be used.

Replacing the apply call in my original post with the following worked for me:

gMask = bgSub.apply(currentFrame, None, -1)
2016-10-24 13:53:27 -0600 commented question Python cv2.BackgroundSubtractorMOG2 history parameter doesn't work

I have 200 images. However, I don't think the number of images was the issue. I'll post the solution.

2016-10-20 13:28:31 -0600 commented question What is the history parameter in BackgroundSubtractorMOG2?

Did you find an answer? I'm having a problem with this in opencv 2.4.11 for python. Changing the history value doesn't seem to work.

2016-10-20 13:25:48 -0600 received badge  Supporter (source)
2016-10-20 13:14:23 -0600 asked a question Python cv2.BackgroundSubtractorMOG2 history parameter doesn't work

I'm using opencv 2.4.11 with python 2.7.12.

The BackgroundSubtractorMOG2 works (i.e. background subtraction results are ok), but I wanted to tune the parameters.

However, changing the history parameter is not showing any changes the in the background subtraction results.

Here is the code snippet:

history = 30  #I've also tried 2,5,20,200
varThreshold = 16
bShadowDetection = False
bgSub = cv2.BackgroundSubtractorMOG2(history, varThreshold, bShadowDetection)

learningRate = 0.1
for currentFrame in allFrames:  #assume allFrames is a list containing the images
    fgMask = bgSub.apply(currentFrame, None, learningRate)

I've also tried setting the history using the setInt method:

history = 30  #I've also tried 2,5,20,200
varThreshold = 16
bShadowDetection = False
bgSub = cv2.BackgroundSubtractorMOG2(history, varThreshold, bShadowDetection)

bgSub.setInt('history',30)

learningRate = 0.1
for currentFrame in allFrames:  #assume allFrames is a list containing the images
    fgMask = bgSub.apply(currentFrame, None, learningRate)

Has anyone else had this problem or know what's going on?

2016-10-20 13:02:47 -0600 commented answer OpenCV Python cv2.BackgroundSubtractor parameters

I can set the learning rate, but setting the history doesn't appear to work. Have you encountered this or any idea what might be causing this?

2016-08-26 11:35:37 -0600 asked a question opencv 2.4.11 BackgroundSubtractorMOG2 problems with background and foreground

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();
}