|
|
|
@ -42,13 +42,12 @@ |
|
|
|
|
|
|
|
|
|
#include "precomp.hpp" |
|
|
|
|
|
|
|
|
|
using namespace cv; |
|
|
|
|
using namespace cv::gpu; |
|
|
|
|
|
|
|
|
|
#if !defined HAVE_CUDA || defined(CUDA_DISABLER) |
|
|
|
|
|
|
|
|
|
cv::gpu::MOG_GPU::MOG_GPU(int) { throw_no_cuda(); } |
|
|
|
|
void cv::gpu::MOG_GPU::initialize(cv::Size, int) { throw_no_cuda(); } |
|
|
|
|
void cv::gpu::MOG_GPU::operator()(const cv::gpu::GpuMat&, cv::gpu::GpuMat&, float, Stream&) { throw_no_cuda(); } |
|
|
|
|
void cv::gpu::MOG_GPU::getBackgroundImage(GpuMat&, Stream&) const { throw_no_cuda(); } |
|
|
|
|
void cv::gpu::MOG_GPU::release() {} |
|
|
|
|
Ptr<gpu::BackgroundSubtractorMOG> cv::gpu::createBackgroundSubtractorMOG(int, int, double, double) { throw_no_cuda(); return Ptr<gpu::BackgroundSubtractorMOG>(); } |
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
|
@ -63,7 +62,7 @@ namespace cv { namespace gpu { namespace cudev |
|
|
|
|
} |
|
|
|
|
}}} |
|
|
|
|
|
|
|
|
|
namespace mog |
|
|
|
|
namespace |
|
|
|
|
{ |
|
|
|
|
const int defaultNMixtures = 5; |
|
|
|
|
const int defaultHistory = 200; |
|
|
|
@ -71,88 +70,140 @@ namespace mog |
|
|
|
|
const float defaultVarThreshold = 2.5f * 2.5f; |
|
|
|
|
const float defaultNoiseSigma = 30.0f * 0.5f; |
|
|
|
|
const float defaultInitialWeight = 0.05f; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
cv::gpu::MOG_GPU::MOG_GPU(int nmixtures) : |
|
|
|
|
frameSize_(0, 0), frameType_(0), nframes_(0) |
|
|
|
|
{ |
|
|
|
|
nmixtures_ = std::min(nmixtures > 0 ? nmixtures : mog::defaultNMixtures, 8); |
|
|
|
|
history = mog::defaultHistory; |
|
|
|
|
varThreshold = mog::defaultVarThreshold; |
|
|
|
|
backgroundRatio = mog::defaultBackgroundRatio; |
|
|
|
|
noiseSigma = mog::defaultNoiseSigma; |
|
|
|
|
} |
|
|
|
|
class MOGImpl : public gpu::BackgroundSubtractorMOG |
|
|
|
|
{ |
|
|
|
|
public: |
|
|
|
|
MOGImpl(int history, int nmixtures, double backgroundRatio, double noiseSigma); |
|
|
|
|
|
|
|
|
|
void cv::gpu::MOG_GPU::initialize(cv::Size frameSize, int frameType) |
|
|
|
|
{ |
|
|
|
|
CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4); |
|
|
|
|
void apply(InputArray image, OutputArray fgmask, double learningRate=-1); |
|
|
|
|
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream); |
|
|
|
|
|
|
|
|
|
frameSize_ = frameSize; |
|
|
|
|
frameType_ = frameType; |
|
|
|
|
void getBackgroundImage(OutputArray backgroundImage) const; |
|
|
|
|
void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const; |
|
|
|
|
|
|
|
|
|
int ch = CV_MAT_CN(frameType); |
|
|
|
|
int work_ch = ch; |
|
|
|
|
int getHistory() const { return history_; } |
|
|
|
|
void setHistory(int nframes) { history_ = nframes; } |
|
|
|
|
|
|
|
|
|
// for each gaussian mixture of each pixel bg model we store
|
|
|
|
|
// the mixture sort key (w/sum_of_variances), the mixture weight (w),
|
|
|
|
|
// the mean (nchannels values) and
|
|
|
|
|
// the diagonal covariance matrix (another nchannels values)
|
|
|
|
|
int getNMixtures() const { return nmixtures_; } |
|
|
|
|
void setNMixtures(int nmix) { nmixtures_ = nmix; } |
|
|
|
|
|
|
|
|
|
weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
|
|
|
|
sortKey_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
|
|
|
|
mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); |
|
|
|
|
var_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); |
|
|
|
|
double getBackgroundRatio() const { return backgroundRatio_; } |
|
|
|
|
void setBackgroundRatio(double backgroundRatio) { backgroundRatio_ = (float) backgroundRatio; } |
|
|
|
|
|
|
|
|
|
weight_.setTo(cv::Scalar::all(0)); |
|
|
|
|
sortKey_.setTo(cv::Scalar::all(0)); |
|
|
|
|
mean_.setTo(cv::Scalar::all(0)); |
|
|
|
|
var_.setTo(cv::Scalar::all(0)); |
|
|
|
|
double getNoiseSigma() const { return noiseSigma_; } |
|
|
|
|
void setNoiseSigma(double noiseSigma) { noiseSigma_ = (float) noiseSigma; } |
|
|
|
|
|
|
|
|
|
nframes_ = 0; |
|
|
|
|
} |
|
|
|
|
private: |
|
|
|
|
//! re-initiaization method
|
|
|
|
|
void initialize(Size frameSize, int frameType); |
|
|
|
|
|
|
|
|
|
void cv::gpu::MOG_GPU::operator()(const cv::gpu::GpuMat& frame, cv::gpu::GpuMat& fgmask, float learningRate, Stream& stream) |
|
|
|
|
{ |
|
|
|
|
using namespace cv::gpu::cudev::mog; |
|
|
|
|
int history_; |
|
|
|
|
int nmixtures_; |
|
|
|
|
float backgroundRatio_; |
|
|
|
|
float noiseSigma_; |
|
|
|
|
|
|
|
|
|
CV_Assert(frame.depth() == CV_8U); |
|
|
|
|
float varThreshold_; |
|
|
|
|
|
|
|
|
|
int ch = frame.channels(); |
|
|
|
|
int work_ch = ch; |
|
|
|
|
Size frameSize_; |
|
|
|
|
int frameType_; |
|
|
|
|
int nframes_; |
|
|
|
|
|
|
|
|
|
if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.channels()) |
|
|
|
|
initialize(frame.size(), frame.type()); |
|
|
|
|
GpuMat weight_; |
|
|
|
|
GpuMat sortKey_; |
|
|
|
|
GpuMat mean_; |
|
|
|
|
GpuMat var_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
fgmask.create(frameSize_, CV_8UC1); |
|
|
|
|
MOGImpl::MOGImpl(int history, int nmixtures, double backgroundRatio, double noiseSigma) : |
|
|
|
|
frameSize_(0, 0), frameType_(0), nframes_(0) |
|
|
|
|
{ |
|
|
|
|
history_ = history > 0 ? history : defaultHistory; |
|
|
|
|
nmixtures_ = std::min(nmixtures > 0 ? nmixtures : defaultNMixtures, 8); |
|
|
|
|
backgroundRatio_ = backgroundRatio > 0 ? (float) backgroundRatio : defaultBackgroundRatio; |
|
|
|
|
noiseSigma_ = noiseSigma > 0 ? (float) noiseSigma : defaultNoiseSigma; |
|
|
|
|
|
|
|
|
|
++nframes_; |
|
|
|
|
learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(nframes_, history); |
|
|
|
|
CV_Assert(learningRate >= 0.0f); |
|
|
|
|
varThreshold_ = defaultVarThreshold; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mog_gpu(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_, |
|
|
|
|
varThreshold, learningRate, backgroundRatio, noiseSigma, |
|
|
|
|
StreamAccessor::getStream(stream)); |
|
|
|
|
} |
|
|
|
|
void MOGImpl::apply(InputArray image, OutputArray fgmask, double learningRate) |
|
|
|
|
{ |
|
|
|
|
apply(image, fgmask, learningRate, Stream::Null()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void cv::gpu::MOG_GPU::getBackgroundImage(GpuMat& backgroundImage, Stream& stream) const |
|
|
|
|
{ |
|
|
|
|
using namespace cv::gpu::cudev::mog; |
|
|
|
|
void MOGImpl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream& stream) |
|
|
|
|
{ |
|
|
|
|
using namespace cv::gpu::cudev::mog; |
|
|
|
|
|
|
|
|
|
GpuMat frame = _frame.getGpuMat(); |
|
|
|
|
|
|
|
|
|
CV_Assert( frame.depth() == CV_8U ); |
|
|
|
|
|
|
|
|
|
int ch = frame.channels(); |
|
|
|
|
int work_ch = ch; |
|
|
|
|
|
|
|
|
|
if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.channels()) |
|
|
|
|
initialize(frame.size(), frame.type()); |
|
|
|
|
|
|
|
|
|
backgroundImage.create(frameSize_, frameType_); |
|
|
|
|
_fgmask.create(frameSize_, CV_8UC1); |
|
|
|
|
GpuMat fgmask = _fgmask.getGpuMat(); |
|
|
|
|
|
|
|
|
|
getBackgroundImage_gpu(backgroundImage.channels(), weight_, mean_, backgroundImage, nmixtures_, backgroundRatio, StreamAccessor::getStream(stream)); |
|
|
|
|
++nframes_; |
|
|
|
|
learningRate = learningRate >= 0 && nframes_ > 1 ? learningRate : 1.0 / std::min(nframes_, history_); |
|
|
|
|
CV_Assert( learningRate >= 0 ); |
|
|
|
|
|
|
|
|
|
mog_gpu(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_, |
|
|
|
|
varThreshold_, (float) learningRate, backgroundRatio_, noiseSigma_, |
|
|
|
|
StreamAccessor::getStream(stream)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void MOGImpl::getBackgroundImage(OutputArray backgroundImage) const |
|
|
|
|
{ |
|
|
|
|
getBackgroundImage(backgroundImage, Stream::Null()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void MOGImpl::getBackgroundImage(OutputArray _backgroundImage, Stream& stream) const |
|
|
|
|
{ |
|
|
|
|
using namespace cv::gpu::cudev::mog; |
|
|
|
|
|
|
|
|
|
_backgroundImage.create(frameSize_, frameType_); |
|
|
|
|
GpuMat backgroundImage = _backgroundImage.getGpuMat(); |
|
|
|
|
|
|
|
|
|
getBackgroundImage_gpu(backgroundImage.channels(), weight_, mean_, backgroundImage, nmixtures_, backgroundRatio_, StreamAccessor::getStream(stream)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void MOGImpl::initialize(Size frameSize, int frameType) |
|
|
|
|
{ |
|
|
|
|
CV_Assert( frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4 ); |
|
|
|
|
|
|
|
|
|
frameSize_ = frameSize; |
|
|
|
|
frameType_ = frameType; |
|
|
|
|
|
|
|
|
|
int ch = CV_MAT_CN(frameType); |
|
|
|
|
int work_ch = ch; |
|
|
|
|
|
|
|
|
|
// for each gaussian mixture of each pixel bg model we store
|
|
|
|
|
// the mixture sort key (w/sum_of_variances), the mixture weight (w),
|
|
|
|
|
// the mean (nchannels values) and
|
|
|
|
|
// the diagonal covariance matrix (another nchannels values)
|
|
|
|
|
|
|
|
|
|
weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
|
|
|
|
sortKey_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1); |
|
|
|
|
mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); |
|
|
|
|
var_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch)); |
|
|
|
|
|
|
|
|
|
weight_.setTo(cv::Scalar::all(0)); |
|
|
|
|
sortKey_.setTo(cv::Scalar::all(0)); |
|
|
|
|
mean_.setTo(cv::Scalar::all(0)); |
|
|
|
|
var_.setTo(cv::Scalar::all(0)); |
|
|
|
|
|
|
|
|
|
nframes_ = 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void cv::gpu::MOG_GPU::release() |
|
|
|
|
Ptr<gpu::BackgroundSubtractorMOG> cv::gpu::createBackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma) |
|
|
|
|
{ |
|
|
|
|
frameSize_ = Size(0, 0); |
|
|
|
|
frameType_ = 0; |
|
|
|
|
nframes_ = 0; |
|
|
|
|
|
|
|
|
|
weight_.release(); |
|
|
|
|
sortKey_.release(); |
|
|
|
|
mean_.release(); |
|
|
|
|
var_.release(); |
|
|
|
|
return new MOGImpl(history, nmixtures, backgroundRatio, noiseSigma); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|