From c5c620bab0721f09aa5463633b2c00ebc1003c33 Mon Sep 17 00:00:00 2001 From: Glue Crow Date: Sun, 21 Jan 2018 23:02:05 +0800 Subject: [PATCH] ocl: Avoid unnecessary initializing when non-UMat parameters are used --- modules/video/src/bgfg_gaussmix2.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index 3030f9e225..cb8fe660b8 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -770,6 +770,11 @@ public: bool BackgroundSubtractorMOG2Impl::ocl_apply(InputArray _image, OutputArray _fgmask, double learningRate) { + bool needToInitialize = nframes == 0 || learningRate >= 1 || _image.size() != frameSize || _image.type() != frameType; + + if( needToInitialize ) + initialize(_image.size(), _image.type()); + ++nframes; learningRate = learningRate >= 0 && nframes > 1 ? learningRate : 1./std::min( 2*nframes, history ); CV_Assert(learningRate >= 0); @@ -841,21 +846,21 @@ void BackgroundSubtractorMOG2Impl::apply(InputArray _image, OutputArray _fgmask, { 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(_fgmask.isUMat(), ocl_apply(_image, _fgmask, learningRate)) opencl_ON = false; - initialize(_image.size(), _image.type()); + nframes = 0; } #endif + bool needToInitialize = nframes == 0 || learningRate >= 1 || _image.size() != frameSize || _image.type() != frameType; + + if( needToInitialize ) + initialize(_image.size(), _image.type()); + Mat image = _image.getMat(); _fgmask.create( image.size(), CV_8U ); Mat fgmask = _fgmask.getMat();