Remove shadows with background subtraction for stationary objects detection

  background-subtraction, c++, computer-vision, opencv

I’ve to detect a stationary object in a predefined area using Opencv V4.4.0. I’m working with two background subtractor that store two different background mask, one that store the stationary initial background mask, and one that detect the moving objects as updated mask; I’ve implemented them using MOG2 background subtractor, but the shadows removal system doesn’t seem to works properly.

Instatiation of the two background subtractor MOG2:

Ptr<BackgroundSubtractorMOG2> bgSubtractor;
Ptr<BackgroundSubtractorMOG2> bgSubtractorMotion;

bgSubtractor = createBackgroundSubtractorMOG2(500, 16.0);

bgSubtractorMotion = createBackgroundSubtractorMOG2(500, 16.0);


And I call the background removal as below:

// stationary initial background mask

bgSubtractor->apply(sourceFrame, backMaskFrame, 0.0);
blur(backMaskFrame, backMaskFrame, cv::Size(15, 15), cv::Point(-1, -1));
threshold(backMaskFrame, backMaskFrame, 254, 255, THRESH_BINARY);

// updated background mask

bgSubtractorMotion->apply(sourceFrame, backgroundSubtractionUpdatedFrame);

blur(backgroundSubtractionUpdatedFrame, backgroundSubtractionUpdatedFrame, cv::Size(15, 15), cv::Point(-1, -1));
threshold(backgroundSubtractionUpdatedFrame, backgroundSubtractionUpdatedFrame, 254, 255, THRESH_BINARY);

Now, I’ve to find the stationary objects in the area using the mask of moving object but the system recognize also shadows as stationary object, and this kind of problem doesn’t make the system works correctly in daytime with a sunnyday and shadows well defined. I’ve tried to set the background detector property DetectShadows to false, but this seem be useless. I’ve tried also to threshold the resulting mask from the stationary background and the updated background, without any results.
There is a possible working solution to this problem?

Thanks in advance.

Source: Windows Questions C++