What is the meaning of "history" inside BackgroundSubtractorMOG2?
Asked Answered
C

1

7

I'm on OpenCV for java (but that's not relevant I guess). I'm using the BackgroundSubtractorMOG2 class which is (poorly) referenced here. I have read and understood the Zivkovic paper about the algorithm which you can find here.

BackgroundSubtractorMOG2 takes in its constructor a parameter called history. What is it, and how does it influence the result? Could you point me to its reference inside the paper, for example?

From the class source code, line 106, it is said that alpha = 1/history. That would mean that history is namely the T parameter inside the paper, i.e. (more or less) the number of frames that constitute the training set.

However it doesn't seem so. Changing the value in the constructor, from 10 to 500 or beyond, has no effect on the final result. This is what I'm calling:

Mat result = new Mat();
int history = 10; //or 50, or 500, or whatever
BackgroundSubtractorMOG2 sub = new BackgroundSubtractorMOG2(history, 16, false);
for (....) {
    sub.apply(frame[i], result);
}
imshow(result); //let's see last frame

It doesn't matter what history I set, be it 5, 10, 500, 1000 - the result is always the same. Whereas, if I change the alpha value (the learning rate) through apply(), I can see its real influence:

Mat result = new Mat();
float alpha = 0.1; //learning rate, 1/T (1/history?)
BackgroundSubtractorMOG2 sub = new BackgroundSubtractorMOG2(whatever, 16, false);
for (...) {
    sub.apply(frame[i], result, alpha);
}
imshow(result);

If I change alpha here, result changes a lot, which is understandable. So, two conjectures:

  1. history is not really 1/alpha as the source code states. But then: what is it? how does it affect the algorithm?

  2. history is really 1/alpha, but there's a bug in the java wrapper that makes the history value you set in the constructor useless.

Could you help me?

(Tagging c++ also as this is mainly a question about an OpenCV class and the whole OpenCV java framework is just a wrapper around c++).

Cautionary answered 7/9, 2015 at 13:56 Comment(0)
X
4

It seems clear that alpha = 1 / history (except for some transitory instants). In void BackgroundSubtractorMOG2Impl::apply method:

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

You can test if the BackgroundSubtractorMOG2 object is using the history value that you pass in the constructor using the getHistory() method.

Xantho answered 7/9, 2015 at 14:37 Comment(2)
That is what I had understood, but my tests are against, as I have shown - changing history doesn't change the result. Seeing your line, maybe that is because 2*nframes < history? Will check it out. I could do some debugging with getHistory(), but there's no such method in java.Cautionary
That min means that in the very first frame it is using alpha = 0.5, then slowly going down to alpha = 1/history. Interesting.Cautionary

© 2022 - 2024 — McMap. All rights reserved.