Ask Your Question
1

Python cv2.BackgroundSubtractorMOG2 history parameter doesn't work

asked 2016-10-20 13:14:23 -0500

Eng4OpenCV gravatar image

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?

edit retag flag offensive close merge delete

Comments

I've also tried 2,5,20,200

scale problem. try 500, 2000, 5000 (and then, wait)

how many images do you have ?

again, background subtraction is supposed to run on streaming video data, you probably do not have enough images to make it work properly.

berak gravatar imageberak ( 2016-10-20 15:37:42 -0500 )edit
1

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

Eng4OpenCV gravatar imageEng4OpenCV ( 2016-10-24 13:53:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-10-24 13:58:29 -0500

Eng4OpenCV gravatar image

updated 2016-10-24 14:03:20 -0500

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)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2016-10-20 13:14:23 -0500

Seen: 1,922 times

Last updated: Oct 24 '16