From da38a95de6a65e5e0c8e148fee026fda170b393a Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Thu, 9 Aug 2012 12:28:30 +0400 Subject: [PATCH] fixed number of update operation --- modules/gpu/src/bgfg_gmg.cpp | 18 +++++++++++++----- modules/gpu/src/cuda/bgfg_gmg.cu | 31 +++++++++++++------------------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/modules/gpu/src/bgfg_gmg.cpp b/modules/gpu/src/bgfg_gmg.cpp index 7ee6add..238435a 100644 --- a/modules/gpu/src/bgfg_gmg.cpp +++ b/modules/gpu/src/bgfg_gmg.cpp @@ -96,7 +96,8 @@ void cv::gpu::GMG_GPU::initialize(cv::Size frameSize, float min, float max) nfeatures_.setTo(cv::Scalar::all(0)); - boxFilter_ = cv::gpu::createBoxFilter_GPU(CV_8UC1, CV_8UC1, cv::Size(smoothingRadius, smoothingRadius)); + if (smoothingRadius > 0) + boxFilter_ = cv::gpu::createBoxFilter_GPU(CV_8UC1, CV_8UC1, cv::Size(smoothingRadius, smoothingRadius)); loadConstants(frameSize_.width, frameSize_.height, minVal_, maxVal_, quantizationLevels, backgroundPrior, decisionThreshold, maxFeatures, numInitializationFrames); } @@ -130,14 +131,21 @@ void cv::gpu::GMG_GPU::operator ()(const cv::gpu::GpuMat& frame, cv::gpu::GpuMat initialize(frame.size(), 0.0f, frame.depth() == CV_8U ? 255.0f : frame.depth() == CV_16U ? std::numeric_limits::max() : 1.0f); fgmask.create(frameSize_, CV_8UC1); + if (stream) + stream.enqueueMemSet(fgmask, cv::Scalar::all(0)); + else + fgmask.setTo(cv::Scalar::all(0)); funcs[frame.depth()][frame.channels() - 1](frame, fgmask, colors_, weights_, nfeatures_, frameNum_, learningRate, cv::gpu::StreamAccessor::getStream(stream)); // medianBlur - boxFilter_->apply(fgmask, buf_, cv::Rect(0,0,-1,-1), stream); - int minCount = (smoothingRadius * smoothingRadius + 1) / 2; - double thresh = 255.0 * minCount / (smoothingRadius * smoothingRadius); - cv::gpu::threshold(buf_, fgmask, thresh, 255.0, cv::THRESH_BINARY, stream); + if (smoothingRadius > 0) + { + boxFilter_->apply(fgmask, buf_, cv::Rect(0,0,-1,-1), stream); + int minCount = (smoothingRadius * smoothingRadius + 1) / 2; + double thresh = 255.0 * minCount / (smoothingRadius * smoothingRadius); + cv::gpu::threshold(buf_, fgmask, thresh, 255.0, cv::THRESH_BINARY, stream); + } // keep track of how many frames we have processed ++frameNum_; diff --git a/modules/gpu/src/cuda/bgfg_gmg.cu b/modules/gpu/src/cuda/bgfg_gmg.cu index f2f091d..26a04bb 100644 --- a/modules/gpu/src/cuda/bgfg_gmg.cu +++ b/modules/gpu/src/cuda/bgfg_gmg.cu @@ -181,32 +181,18 @@ namespace cv { namespace gpu { namespace device { int nfeatures = nfeatures_(y, x); - bool isForeground = false; - - if (frameNum > c_numInitializationFrames) + if (frameNum >= c_numInitializationFrames) { // typical operation + const float weight = findFeature(newFeatureColor, colors_, weights_, x, y, nfeatures); // see Godbehere, Matsukawa, Goldberg (2012) for reasoning behind this implementation of Bayes rule const float posterior = (weight * c_backgroundPrior) / (weight * c_backgroundPrior + (1.0f - weight) * (1.0f - c_backgroundPrior)); - isForeground = ((1.0f - posterior) > c_decisionThreshold); - } - - fgmask(y, x) = (uchar)(-isForeground); + const bool isForeground = ((1.0f - posterior) > c_decisionThreshold); + fgmask(y, x) = (uchar)(-isForeground); - if (frameNum <= c_numInitializationFrames + 1) - { - // training-mode update - - insertFeature(newFeatureColor, 1.0f, colors_, weights_, x, y, nfeatures); - - if (frameNum == c_numInitializationFrames + 1) - normalizeHistogram(weights_, x, y, nfeatures); - } - else - { // update histogram. for (int i = 0, fy = y; i < nfeatures; ++i, fy += c_height) @@ -220,6 +206,15 @@ namespace cv { namespace gpu { namespace device { nfeatures_(y, x) = nfeatures; } } + else + { + // training-mode update + + insertFeature(newFeatureColor, 1.0f, colors_, weights_, x, y, nfeatures); + + if (frameNum == c_numInitializationFrames - 1) + normalizeHistogram(weights_, x, y, nfeatures); + } } template -- 2.7.4