Ask Your Question

Getting background subtraction to work

asked 2016-07-22 12:41:06 -0500

LasOz gravatar image

I am trying to use OpenCV's background subtraction in MSVS 2015. When I try to use it I get very unimpressive results compared to what I see online in examples and videos, and I am wondering what I am doing wrong.

This YouTube video I made demonstrates my problem.

It is hard to explain my problem but essentially I am getting a close to blank mask, and the background subtractor seems to focus on the tiniest of differences rather than massive blobs moving across the scene.

Here is the snippet of code I am using, ignore some unused variables:

void find_hand_bs(hand_detection_type type, std::vector<cv::Point> &digits, cv::Mat &input)
    cv::Mat frame = input.clone(); //current frame
    cv::Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
    cv::Ptr<cv::BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
    pMOG2 = cv::createBackgroundSubtractorMOG2(); //MOG2 approach
    pMOG2->apply(frame, fgMaskMOG2);
    imshow("FG Mask MOG 2", fgMaskMOG2);

If there is anyone out there with good background subtraction knowledge with OpenCV what is your take on my problem, what settings are going to improve my subtraction? In addition how do I correctly change those settings? Because I have tried to adjust the thresholds and gotten close to no change in the output so I feel I may not be doing it correctly.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-22 14:19:18 -0500

Tetragramm gravatar image

You are re-creating the background subtractor every frame. You need to create it once and run many frames through it. The background subtractor learns what the background looks like over many frames. What you are seeing is just it's first attempt at guessing what's there.

I also suspect that the output mask is black with grey and no white. The grey is the estimated shadows, while white is the object. That way you can ignore any shadows or include them as you need. That's not your problem though.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-07-22 12:38:20 -0500

Seen: 114 times

Last updated: Jul 22 '16