BackgroundSubtractorMOG2 returns a white image

asked 2013-12-02 11:42:28 -0500

matteo gravatar image

I have a camera rigidly attached to a tool. A portion of this tool is included in the field of view of the camera itself. The tool is always on the move. My goal is to retrieve from each image acquired by the camera the segmentation of the tool. I want then to apply the BackgroundSubtractorMOG2 in order to highlight only the portion of the image that doesn't change over time (the tool).

The code of the function i'm using is the following (c++ code).

 Mat Background_removal(Mat img)

 Mat img_fore;

Mat background;

Mat foreground;

const int nmixtures=10;
const bool shadow=false;
const int history =1000;

BackgroundSubtractorMOG2 mog(history,nmixtures,shadow);


return foreground;


My issue is that this function returns an image completely white. What am I doing wrong?

edit retag flag offensive close merge delete


it takes more than 1 pass to train the model.

( in the 1st run, your foreground img is totally different from the background -> all white)

you want a continuous loop, where you feed in a new img, and then call mog(img,foreground);

berak gravatar imageberak ( 2013-12-02 13:27:42 -0500 )edit

I'm not sure that I understood...could you please make an example? btw, this function IS in a loop (a ROS while loop) but the result is the one previously exposed

matteo gravatar imagematteo ( 2013-12-03 03:25:42 -0500 )edit

don't make a new BackgroundSubtractorMOG2 every frame, keep 1 for the whole prog.

see example here

berak gravatar imageberak ( 2013-12-03 03:40:07 -0500 )edit