Remove Shadow from Extracted Foreground

Shabanzo picture Shabanzo · May 3, 2013 · Viewed 10.1k times · Source

I'm newbie with OpenCV + C++ + Visual Studio 2012. And now I need to learn them. Here's the code for background substraction/foreground extraction, and I need to remove the shadow from the foreground, and include them into background model.

    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 = true; bg.nShadowDetection = 0; //resolved! bg.fTau = 0.5; //resolved! 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; }

I've changed bshadowdetection = true or false but nothing happened. So What should I do? Thanks. :)

NB: Sorry for my bad english. :D

================

Resolved!

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

bg.nShadowDetection = 0 and bg.fTau = 0.5, see the code above! :D

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.

Cheers! :D

Answer

Shabanzo picture Shabanzo · May 8, 2013

Resolved!

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

bg.nShadowDetection = 0 and bg.fTau = 0.5, see the code above in the question! :D

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.

Cheers! :D