Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of "history" inside BackgroundSubtractorMOG2?

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++).

like image 494
natario Avatar asked Sep 07 '15 13:09

natario


1 Answers

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.

like image 200
Leo Avatar answered Sep 28 '22 17:09

Leo