Ask Your Question
1

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:

namedWindow("bg-window",CV_WINDOW_AUTOSIZE);

for(;;)
{
....

bg(frame,foreground);
bg.getBackgroundImage(background);
imshow("bg-window",background);

....
}

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
4

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

Something like this:

#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>

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;

cv::namedWindow("Frame");
cv::namedWindow("Background");

for(;;)
{
    cap >> frame;
    bg.operator ()(frame,fore);
    bg.getBackgroundImage(back);
    cv::erode(fore,fore,cv::Mat());
    cv::dilate(fore,fore,cv::Mat());
    cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
    cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
    cv::imshow("Frame",frame);
    cv::imshow("Background",back);
    if(cv::waitKey(30) >= 0) break;
}
return 0;

}

edit flag offensive delete link more

Comments

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″);

bg->set(“nmixtures”,3);

(taken from http://mateuszstankiewicz.eu/?p=189 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 http://mateuszstankiewicz.eu/?p=189 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{

public:    
    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
0

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
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 3,474 times

Last updated: Mar 03 '15