How to use BackgroundSubtractorMOG2 for images

三世轮回 提交于 2020-01-05 11:01:49

问题


I am pretty new to OpenCV and I am stuck at the moment. I am dealing with images, not a video. Since I will have same background in my project, I thought it would be easier to work, if I could remove my background. But first, I have to ask one thing. Can I use BackgroundSubtractorMOG2 for images? Because it is under video analysis/motion analysis title.

I read the documentation on opencv.org and looked through countless examples/tutorials but I am still having difficulty understanding how MOG2 works.

Quick question: What is history that in parameters?

So, I have written a simple code. I get a foreground mask. So, what is the next step? How can I remove the background and left with my object only? Shouldn't I load my background first, then the actual image, so that MOG2 could do the background subtraction?

I am using OpenCV 2.4.11.

Code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>

using namespace cv;
using namespace std;

//global variables
int history = 1;
float varThreshold = 16;
bool bShadowDetection = true;

Mat src; //source image
Mat fgMaskMOG2; //fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor

int main(int argc, char* argv[])
{
    //create GUI windows
    namedWindow("Source");
    namedWindow("FG Mask MOG 2");

    src = imread("bluePaper1.png", 1);

    //create Background Subtractor objects
    pMOG2 = new BackgroundSubtractorMOG2(history, varThreshold, bShadowDetection); //MOG2 approach
    pMOG2->setInt("nmixtures", 3);
    pMOG2->setDouble("fTau", 0.5);

    pMOG2->operator()(src, fgMaskMOG2);

    imshow("Source", src);
    imshow("FG Mask MOG 2", fgMaskMOG2);

    waitKey(0);

    return 0;
}

Source image:

fgMask that I get from MOG2:


回答1:


Mixture of Gaussian method learns background according to history of frames in a fixed camera and so you can not use it for only one image. The history parameter shows how many frames would have effect on construction of the background.

Shadow detection is not a process which depends on BGS method and should be implemented alongside. for example in MOG2 documentation we have:

The shadow is detected if the pixel is a darker version of the background. Tau is a threshold defining how much darker the shadow can be. Tau= 0.5 means that if a pixel is more than twice darker then it is not shadow

In case of your example the foreground could easily be obtained by a simple frame difference and you can easily remove shadows by the mentioned solution.

you can have the foreground by the following steps:

  1. Subtract given image from known background and threshold the result to obtain the foreground mask
  2. Apply AND operation on foreground mask and the given image to get your object with possible shadows.
  3. Remove pixels which is darker (amount of it should be adjust) than their corresponding pixel in background.
  4. Do some post processing like morphological and connected-component-labeling to have a better result.


来源:https://stackoverflow.com/questions/31369757/how-to-use-backgroundsubtractormog2-for-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!