Ask Your Question

How to do Background substraction

asked 2013-07-01 10:31:18 -0500

kabhat gravatar image

Can someone please help me with a few lines on background substraction? I want to remove a.k.a "substract" the background and highlight the foreground images.

I tried with BackgroundSubstractorMOG2 method but apparently most parameters (e.g. nmixtures & bShadowDetection) are protected hence cannot play around. Also, the following code throws a memory exception:





edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-07-02 04:51:02 -0500

Something like this:


int main(int argc, char *argv[])
cv::Mat frame;
cv::Mat back;
cv::Mat fore;
cv::VideoCapture cap(0);
cv::BackgroundSubtractorMOG2 bg;
bg.nmixtures = 3;
bg.bShadowDetection = false;

std::vector<std::vector<cv::Point> > contours;


    cap >> frame;
    bg.operator ()(frame,fore);
    if(cv::waitKey(30) >= 0) break;
return 0;


edit flag offensive delete link more


nmixtures & bShadowDetection are inaccessible (protected)

vinayverma gravatar imagevinayverma ( 2013-07-02 05:56:36 -0500 )edit

Infact its working fine if we don't touch nmixtures & bShadowDetection. Could you pls provide some hint on how to remove shadows?

vinayverma gravatar imagevinayverma ( 2013-07-02 06:15:41 -0500 )edit

public :: int cv::BackgroundSubtractorMOG2::nmixtures

File: background_segm.hpp

albertofernandez gravatar imagealbertofernandez ( 2013-07-02 06:22:23 -0500 )edit

Seems nmixtures is protected in version > 2.4.0. Try this:

bg = Algorithm::create(“BackgroundSubtractor.MOG2″);


(taken from on the comments section)

albertofernandez gravatar imagealbertofernandez ( 2013-07-02 06:27:23 -0500 )edit

If you want to remove the shadow from the foreground, just add the code below after bg.bShadowDetection = True..

bg.nShadowDetection = 0;

bg.fTau = 0.5;

If the shadow still detected, you can adjust the value. bg.fTau = 0.5 means that if pixel is more than 2 times darker then it is not shadow.

bg.nShadowDetection default value is 127. if you wanna remove the shadow just set the foreground min.threshold to 127. or you can set the bg.nShadowDetection to 0 like me.

(taken from on the comments section)

albertofernandez gravatar imagealbertofernandez ( 2013-07-02 06:31:19 -0500 )edit

Thanks. It was really helpful.

It seems like the set doesn't work and crashes due to some existing bug. I used the following to set the parameters: { class BackgroundSubtractorMOG2ToSet: public BackgroundSubtractorMOG2{

    void setTau(float val){fTau = val;}
    void setnmixtures(int val){nmixtures = val;}
    void setnShadowDetection(int val){nShadowDetection = val;}
    void setbShadowDetection(bool val){bShadowDetection = val;}



vinayverma gravatar imagevinayverma ( 2013-07-03 02:25:54 -0500 )edit

answered 2015-03-03 05:58:57 -0500

Shinigami gravatar image

updated 2015-03-03 06:07:12 -0500

In OpenCV 2.4.10 you can use these functions from the Algorithm class:

void setInt(const char* name, int value);
void setDouble(const char* name, double value);
void setBool(const char* name, bool value);
void setString(const char* name, const string& value);
void setMat(const char* name, const Mat& value);
void setMatVector(const char* name, const vector<Mat>& value);
void setAlgorithm(const char* name, const Ptr<Algorithm>& value);

So it should look like:

bg = new BackgroundSubstractorMOG2();
bg->setInt("nmixtures" , 3);

for floats use setDouble(...)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-07-01 10:31:18 -0500

Seen: 3,578 times

Last updated: Mar 03 '15