Ask Your Question

How to change BackgroundSubtractorMOG2 options

asked 2012-07-28 07:41:24 -0500

George Profenza gravatar image

I'm learning OpenCV and loving it so far. Currently I'm looking into background subtractor and it's awesome that the library already offers algorithms for this. I was looking at BackgroundSubtractorMOG2 and I've managed to get a basic setup running:

cv::BackgroundSubtractorMOG2 bg;
//in update loop
bg.operator ()(frame,foreground);

but I haven't worked out how change/use options with for this class. The documentation lists members to control the algorithm (such as nmixtures,backgroundRatio,etc.), but they appear to be protected (in OpenCV 2.4.1). Would I still be able to control the algorithm using OpenCV 2.4 ? If so, how ?


edit retag flag offensive close merge delete


I've moved the protected components I wanted to change to the public area in the header file which feels a bit dirty. Should I also call initialize() after updating a property (like nmixtures for example) ? Also, how is the Size calculated ? I'm working with a 640x384 image and with the default settings Size is (0,1124007936). Should that change based on different parameters ?

George Profenza gravatar imageGeorge Profenza ( 2012-07-28 09:46:21 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-29 01:18:25 -0500

Kirill Kornyakov gravatar image

BackgroundSubtractorMOG2 is inherited from BackgroundSubtractor which is inherited from cv::Algorithm (read the documentation carefully). That means that you can use Algorithm::set and Algorithm::get methods for accessing parameters. Here is a sample on how you can work with cv::Algorithm.


  1. You can write bg(frame,foreground); instead of your second line.
  2. Do not make protected members public, try to avoid the modification of OpenCV header files, it is usually a bad practice.
edit flag offensive delete link more


Unfortunately my version of OpenCV is missing BackgroundSubtractorGMG, so I've replaced it with Ptr<BackgroundSubtractorMOG2> fgbg = Algorithm::create<BackgroundSubtractorMOG2>("BackgroundSubtractor.MOG2"); and have succesfully used the set() method which I presume is what you're pointing me to. Thank again, this is great !

George Profenza gravatar imageGeorge Profenza ( 2012-07-29 04:28:39 -0500 )edit

I think I'm doing it wrong again :( I've added a slider to modify a member of fgbg using bg-&gt;set("nmixtures",bgNMixtures); where bgNMixtures is an int mapped to a trackbar. When I use the slider the application crashes instantly with no errors. Any hints to what I might be doing wrong ?

George Profenza gravatar imageGeorge Profenza ( 2012-07-29 14:58:07 -0500 )edit

in version 2.4.2 only detectShadows, history and nmixtures are supported, what about other params?

Victor1234 gravatar imageVictor1234 ( 2012-08-08 09:39:30 -0500 )edit

I would recommend you to create a new question.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-08-09 02:11:49 -0500 )edit

This does not answer the question. I have the same problem. Is this a bug? I opened a new question

alex gravatar imagealex ( 2012-10-12 13:26:07 -0500 )edit

answered 2012-10-23 08:06:31 -0500

alex gravatar image
edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2012-07-28 07:41:24 -0500

Seen: 9,106 times

Last updated: Oct 23 '12