1

I am working with the BackgroundSubtractorMOG2 class in OpenCV (Python), and am trying to extract the individual components of the background model. As I understand it, each pixel will be modeled by the mixture of a varying number of gaussian distributions, each defined by a mean and variance. So, how can I determine what all of these components (means and variances) are after feeding the background subtractor a given number of frames?

The documentation here: https://docs.opencv.org/3.4.3/d7/d7b/classcv_1_1BackgroundSubtractorMOG2.html#adbb1d295befaff88a54a929e50aaf879
Does not seem to discuss doing this.

This information must be contained somewhere in the background subtractor object. Does anyone know how to get to it?

Thanks!

Edit: A little more searching has led me to believe that the cv2.Algorithm class is required to read the parameters from the BackgroundSubtractorMOG2 object. I think the two questions posed here:
http://answers.opencv.org/question/28008/how-to-derive-from-algorithm/
Reading algorithm parameters from file in OpenCV
are similar to what I am asking, but I am unable to interpret the answers. I thought the solution would be something along the lines of:
Parameters = cv2.Algorithm.read('name_of_backgroundsubtractorMOG2_object')
but this returns an error of: 'Required argument 'fn' (pos 1) not found'

Edit 2: Unfortunately I think this question has been answered here: Save opencv BackgroundSubtractorMOG to file? Short answer: It cannot be done! Sad!

Jeremy
  • 71
  • 4
  • If you go [here](https://docs.opencv.org/3.3.0/db/d5c/tutorial_py_bg_subtraction.html), you can read more about how the algorithm works. Additionally, the description also includes the research papers the algorithm is based on. In the link that you've added, there are **clear** functions for getting the variance of the individual gaussian components. – Rick M. Oct 18 '18 at 15:38
  • Thanks for the reply Rick. Maybe I am using these clear functions incorrectly then. Each of the functions, .getNMixtures(), .getVarInit(), .getVarMax(), .getVarMin(), for instance, return a single value. How can I use these functions, or others that maybe I am not recognizing, to determine the individual mean and variance of each gaussian component of each pixel? – Jeremy Oct 18 '18 at 15:50
  • Hmm, that is true, I get what you mean now. The functions `getVarMAx`, `getVarMin` all should return the individual values. Why don't you try to save the initialized constructor as a .xml file and then use the algorithm on it? I think this might not work for BackgroundSubtractorMOG2 as it isn't overloaded in the Algorithm class. – Rick M. Oct 19 '18 at 07:26
  • @Jeremy have you found any alternatives? – B.D. Jan 20 '21 at 12:43

0 Answers0