How to use cv::BackgroundSubtractorMOG in OpenCV?
Asked Answered
D

4

13

I'm using OpenCV2.2 to implement moving objects detection with the method of Background Subtraction. And I use the Gaussian Mixture Model(GMM) method to model the background reference image.

I directly get the foreground pixels(or foreground mask) by using the class cv::BackgroundSubtractorMOG provided in OpenCV2.2. It's convenient but the foreground mask returned by cv::BackgroundSubtractorMOG is not as good as I expected. In addition, it seems that cv::BackgroundSubtractorMOG performs poorer than the method of GMM wrote in C language provided in OpenCV1.0.

The following is my code in OpenCV2.2:

cv::BackgroundSubtractorMOG mog;
mog(frame, fgMask, 0.01);

So, did I use the method in a wrong way?

By the way, does cv::BackgroundSubtractorMOG perform shadow removal on the foreground pixels?

Thank you very much.

Diastema answered 5/5, 2012 at 3:39 Comment(1)
Here's a useful overview, from 3.0 docs, of using OpenCV's Background Subtraction functions: docs.opencv.org/master/db/d5c/tutorial_py_bg_subtraction.htmlMeasure
S
12

When you create mog, you are not defining any parameters, so it is created with default parameters. Here you have a description of each parameter, maybe is just that. Try with 3, 4 5 Gaussians.

This function does not perforn shadow-removal but you have this other function that does. Good luck!

Shults answered 1/6, 2012 at 10:52 Comment(0)
D
5

There are recent algorithms which remove backgrounds (detect foreground) far better than the standard GMM implementation in OpenCV.

For example, there is a block-based classifier cascade approach described in this journal article, along with its C++ based source code.

Device answered 2/12, 2013 at 4:59 Comment(1)
What should the input directory structure look like for using this tool? It seems to segfault on readdir() no matter what I throw at it.Redblooded
D
1

F.X.'s answer on this thread gives sample parameters of

backgroundSubtractor = new BackgroundSubtractorMOG(3, 4, 0.8);
Deepfreeze answered 5/3, 2013 at 6:29 Comment(1)
but that's running very slowly on my app. i'm on the lookout for some good parameters myselfDeepfreeze
A
0

I will recommend using the following settings to get started. Then you can start tuning your parameters:

cv::BackgroundSubtractorMOG2 mog;
mog(rawFrame,foregroundFrame,-1);
mog.set("nmixtures", 3);
mog.set("detectShadows",1);   

In this example I set the MOG2 subtractor with 3 Gaussian mixtures. I also enabled shadow detection.

Agustin answered 14/2, 2014 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.