Ask Your Question
0

Doubt about BackgroundSubtractorMOG2 apply method

asked 2017-01-12 15:39:23 -0600

simozz gravatar image

updated 2017-01-12 16:44:35 -0600

This is of course an opencv's noob question but I need to clarify something about BackgroundSubtractorMOG2 and how to use it.

I am looking more in depth the source code for BackgroundSubtractorMOG2 class, particularly the apply method:

void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, double learningRate)
{
    CV_INSTRUMENT_REGION()

    bool needToInitialize = nframes == 0 || learningRate >= 1 || _image.size() != frameSize || _image.type() != frameType;

    if( needToInitialize )
        initialize(_image.size(), _image.type());

#ifdef HAVE_OPENCL
    if (opencl_ON)
    {
        CV_OCL_RUN(_image.isUMat(), ocl_apply(_image, _fgmask, learningRate))

        opencl_ON = false;
        initialize(_image.size(), _image.type());
    }
#endif

    Mat image = _image.getMat();
    _fgmask.create( image.size(), CV_8U );
    Mat fgmask = _fgmask.getMat();

    ++nframes;
    learningRate = learningRate >= 0 && nframes > 1 ? learningRate : 1./std::min( 2*nframes, history );
    CV_Assert(learningRate >= 0);

    parallel_for_(Range(0, image.rows),
                  MOG2Invoker(image, fgmask,
                              bgmodel.ptr<GMM>(),
                              (float*)(bgmodel.ptr() + sizeof(GMM)*nmixtures*image.rows*image.cols),
                              bgmodelUsedModes.ptr(), nmixtures, (float)learningRate,
                              (float)varThreshold,

                              backgroundRatio, varThresholdGen,
                              fVarInit, fVarMin, fVarMax, float(-learningRate*fCT), fTau,
                              bShadowDetection, nShadowDetection),
                              image.total()/(double)(1 << 16));
}

which is the method to call when we want to generate the foreground.

When using it in a infinite loop, it is correct that I should check for the new foreground every nframes times I calls the apply method ?

edit retag flag offensive close merge delete

Comments

Have you try this tutorial?

LBerger gravatar imageLBerger ( 2017-01-12 15:45:04 -0600 )edit

Yes, but that example doesn't solve completely my doubt.

simozz gravatar imagesimozz ( 2017-01-13 01:52:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-01-13 02:50:20 -0600

LBerger gravatar image

I think you can solve your problem using Parameter learningrate

* learningRate The value between 0 and 1 that indicates how fast the background model is learnt. Negative parameter value makes the algorithm to use some automatically chosen learning rate. 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame. *

when learningrate =0 background is initialise at first call and never update when you use apply method

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-01-12 15:39:23 -0600

Seen: 561 times

Last updated: Jan 13 '17