Question

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

Was it helpful?

Solution

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

OTHER TIPS

have you tried all the different background subtraction methods available in opencv? do this first, if none of them solve your shadow problem you might have to write your own or use a different library. however, if you are new to computer vision and c++ this is going to be a challenge.

either way, my suggestion would be to look at hue ( the colour without intensity) rather than colour values directly. shadows typically do not change the hue, just the intensity, so this is a way to separate the two. hue is easy to calculate.

you could try to do this in opencv by converting your colour image to the HSL colour space. then extract the H channel (H - stands for hue, S - saturation, L - lightness) as a greyscale image and feed that to the background subtracter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top