OpenCV Python cv2.BackgroundSubtractor Options

Summary

It seems that changing the BackgroundSubtractorMOG parameters does not affect the results of the algorithm.

I am using Python: 2.7.6 | Anaconda 2.1.0 (64-bit)
OpenCV: "2.4.10"
OS: Windows 7 x64

Default parameters for the algorithm:

history=200, nmixtures=5, backgroundRatio=0.7 

So, creating a background subtractor with different parameters should give different results. But I always get the same result (front mask) with default options and with custom options.


Reproducing the problem

First, create two background subtraction objects with different parameters:

 bg1 = cv2.BackgroundSubtractorMOG() bg2 = cv2.BackgroundSubtractorMOG(history=3, nmixtures=5, backgroundRatio=0.0001) 

Create 2 "VideoCaptrue" objects:

 cap = cv2.VideoCapture('video.mp4') cap2 = cv2.VideoCapture('different_video.mp4') 

Check the results:

 # Get a frame from the first capturing object: frame = cap.read()[1] # Get the foreground mask from both background subtractors: fg1 = bg1.apply(frame) fg2 = bg2.apply(frame) # Show both results and the difference between them: cv2.imshow('Window name', np.hstack((fg1, fg2, fg1 - fg2))) cv2.waitKey(30) 

After starting this code block for some frames, the window displays the results from the first background subtracter, the result of the second and the difference of two.

Since both masks are the same, the result of their difference (third panel) is a full black frame:

2 BackgroundSubtractorMOG and difference

Then, suddenly change the video source (second VideoCapture object):

 # Get a frame from the second capturing object: frame = cap2.read()[1] # Get the foreground mask from both background subtractors: fg1 = bg1.apply(frame) fg2 = bg2.apply(frame) # Show both results and the difference between them: cv2.imshow('Window name', np.hstack((fg1, fg2, fg1 - fg2))) cv2.waitKey(30) 

And after running this code block for some frames, you will get:

2 BackgroundSubtractorMOG and difference with new video

The two foreground masks look the same, and so the third panel is a full black frame.

But after starting the last block of code for more than three frames, the results of the second panel will again turn blacker (due to history=3 when creating the bg2 object). But both background subtractors continue to get the same results (as shown in the third full black panel).


Alternatives

I also tried changing their settings with

 bg2.setInt('history') 

But I get the same results.

And I always get the parameters that I installed (for example):

 >>>bg2.getInt('history') 3 

Is there anything I can't see?

+7
python parameters opencv background-subtraction
source share
1 answer

I found that (with some version of OpenCV), when I call the apply method from the BackgroundSubtractorMOG object, it uses the default learningRate parameter set to 0.0 , so BackgroundSubtractor does not "learn" new frames, it just sticks to the first frame used.

To avoid this, I have to call the apply method with the explicit parameter learnRate, then it works the way it worked before.

So, after running the code:

 bg1 = cv2.BackgroundSubtractorMOG() cap = cv2.VideoCapture('video.mp4') frame = cap.read()[1] 

... instead of doing:

 fg1 = bg1.apply(frame) 

I must do:

 fg1 = bg1.apply(frame, learningRate=0.001) 
+5
source share

All Articles