Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save opencv BackgroundSubtractorMOG to file?

I use opencv for a vehicle counting process. How could I save my BackgroundSubtractorMOG object such as BackgroundSubtractorMOG2 etc to a file, so that I can read it from the file, use the trained background model again without training it for a long time?

like image 632
user1427275 Avatar asked Dec 09 '14 01:12

user1427275


1 Answers

I happened to be searching for this exact thing, so I'll just share what I've learned, if someone else is still looking for it. It seems like the short answer is no. At least it does not seem to be supported by OpenCV.

BackgroundSubtractorMOG2 inherits from BackgroundSubtractor which inherits from Algorithm which has built in read/write functionality for storing parameters of the algorithms. According to the OpenCV doc on Algorithm::write it stores only the parameters for background model algorithms and not the background model itself. Quote from the link:

"However, the background model is not stored. First, it’s rather big. Then, if you have stored the background model, it would likely become irrelevant on the next run (because of shifted camera, changed background, different lighting etc.). Therefore, BackgroundSubtractorMOG and BackgroundSubtractorMOG2 also rely on the standard Algorithm::write() to store just the algorithm parameters."

As a side note I completely disagree with OpenCV, and I don't see why they feel the need to make this decision for me and not just allow me to save the background model if I see a purpose for it. Anyway, that's what I've found, if someone else knows how to store the background model or a workaround: please share.

like image 197
pethul Avatar answered Oct 20 '22 22:10

pethul