Commit da38a95d authored by Vladislav Vinogradov's avatar Vladislav Vinogradov

fixed number of update operation

parent 9ec96597
...@@ -96,7 +96,8 @@ void cv::gpu::GMG_GPU::initialize(cv::Size frameSize, float min, float max) ...@@ -96,7 +96,8 @@ void cv::gpu::GMG_GPU::initialize(cv::Size frameSize, float min, float max)
nfeatures_.setTo(cv::Scalar::all(0)); 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); 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 ...@@ -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<ushort>::max() : 1.0f); initialize(frame.size(), 0.0f, frame.depth() == CV_8U ? 255.0f : frame.depth() == CV_16U ? std::numeric_limits<ushort>::max() : 1.0f);
fgmask.create(frameSize_, CV_8UC1); 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)); funcs[frame.depth()][frame.channels() - 1](frame, fgmask, colors_, weights_, nfeatures_, frameNum_, learningRate, cv::gpu::StreamAccessor::getStream(stream));
// medianBlur // medianBlur
boxFilter_->apply(fgmask, buf_, cv::Rect(0,0,-1,-1), stream); if (smoothingRadius > 0)
int minCount = (smoothingRadius * smoothingRadius + 1) / 2; {
double thresh = 255.0 * minCount / (smoothingRadius * smoothingRadius); boxFilter_->apply(fgmask, buf_, cv::Rect(0,0,-1,-1), stream);
cv::gpu::threshold(buf_, fgmask, thresh, 255.0, cv::THRESH_BINARY, 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 // keep track of how many frames we have processed
++frameNum_; ++frameNum_;
......
...@@ -181,32 +181,18 @@ namespace cv { namespace gpu { namespace device { ...@@ -181,32 +181,18 @@ namespace cv { namespace gpu { namespace device {
int nfeatures = nfeatures_(y, x); int nfeatures = nfeatures_(y, x);
bool isForeground = false; if (frameNum >= c_numInitializationFrames)
if (frameNum > c_numInitializationFrames)
{ {
// typical operation // typical operation
const float weight = findFeature(newFeatureColor, colors_, weights_, x, y, nfeatures); const float weight = findFeature(newFeatureColor, colors_, weights_, x, y, nfeatures);
// see Godbehere, Matsukawa, Goldberg (2012) for reasoning behind this implementation of Bayes rule // 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)); const float posterior = (weight * c_backgroundPrior) / (weight * c_backgroundPrior + (1.0f - weight) * (1.0f - c_backgroundPrior));
isForeground = ((1.0f - posterior) > c_decisionThreshold); const bool isForeground = ((1.0f - posterior) > c_decisionThreshold);
} fgmask(y, x) = (uchar)(-isForeground);
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. // update histogram.
for (int i = 0, fy = y; i < nfeatures; ++i, fy += c_height) for (int i = 0, fy = y; i < nfeatures; ++i, fy += c_height)
...@@ -220,6 +206,15 @@ namespace cv { namespace gpu { namespace device { ...@@ -220,6 +206,15 @@ namespace cv { namespace gpu { namespace device {
nfeatures_(y, x) = nfeatures; 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 <typename SrcT> template <typename SrcT>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment