提交 5bf73457 编写于 作者: J jeffeDurand 提交者: Alexander Alekhin

Merge pull request #16090 from jeffeDurand:cuda_mog2_issue_5296

* cuda_mog2_issue_5296
上级 43426577
此差异已折叠。
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_CUDA_MOG2_H
#define OPENCV_CUDA_MOG2_H
#include "opencv2/core/cuda.hpp"
struct CUstream_st;
typedef struct CUstream_st *cudaStream_t;
namespace cv { namespace cuda {
class Stream;
namespace device { namespace mog2 {
typedef struct
{
float Tb_;
float TB_;
float Tg_;
float varInit_;
float varMin_;
float varMax_;
float tau_;
int nmixtures_;
unsigned char shadowVal_;
} Constants;
void mog2_gpu(PtrStepSzb frame, int cn, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, const Constants *const constants, cudaStream_t stream);
void getBackgroundImage2_gpu(int cn, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, const Constants *const constants, cudaStream_t stream);
} } } }
#endif /* OPENCV_CUDA_MOG2_H */
......@@ -41,209 +41,207 @@
//M*/
#include "precomp.hpp"
#include "cuda/mog2.hpp"
using namespace cv;
using namespace cv::cuda;
using namespace cv::cuda::device::mog2;
#if !defined HAVE_CUDA || defined(CUDA_DISABLER)
Ptr<cuda::BackgroundSubtractorMOG2> cv::cuda::createBackgroundSubtractorMOG2(int, double, bool) { throw_no_cuda(); return Ptr<cuda::BackgroundSubtractorMOG2>(); }
Ptr<cuda::BackgroundSubtractorMOG2> cv::cuda::createBackgroundSubtractorMOG2(int, double, bool)
{
throw_no_cuda();
return Ptr<cuda::BackgroundSubtractorMOG2>();
}
#else
namespace cv { namespace cuda { namespace device
namespace
{
namespace mog2
{
void loadConstants(int nmixtures, float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, unsigned char shadowVal);
void mog2_gpu(PtrStepSzb frame, int cn, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, cudaStream_t stream);
void getBackgroundImage2_gpu(int cn, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream);
}
}}}
// default parameters of gaussian background detection algorithm
const int defaultHistory = 500; // Learning rate; alpha = 1/defaultHistory2
const float defaultVarThreshold = 4.0f * 4.0f;
const int defaultNMixtures = 5; // maximal number of Gaussians in mixture
const float defaultBackgroundRatio = 0.9f; // threshold sum of weights for background test
const float defaultVarThresholdGen = 3.0f * 3.0f;
const float defaultVarInit = 15.0f; // initial variance for new components
const float defaultVarMax = 5.0f * defaultVarInit;
const float defaultVarMin = 4.0f;
// additional parameters
const float defaultCT = 0.05f; // complexity reduction prior constant 0 - no reduction of number of components
const unsigned char defaultShadowValue = 127; // value to use in the segmentation mask for shadows, set 0 not to do shadow detection
const float defaultShadowThreshold = 0.5f; // Tau - shadow threshold, see the paper for explanation
class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2
{
public:
MOG2Impl(int history, double varThreshold, bool detectShadows);
~MOG2Impl();
namespace
void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream &stream) CV_OVERRIDE;
void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
void getBackgroundImage(OutputArray backgroundImage, Stream &stream) const CV_OVERRIDE;
int getHistory() const CV_OVERRIDE { return history_; }
void setHistory(int history) CV_OVERRIDE { history_ = history; }
int getNMixtures() const CV_OVERRIDE { return constantsHost_.nmixtures_; }
void setNMixtures(int nmixtures) CV_OVERRIDE { constantsHost_.nmixtures_ = nmixtures; }
double getBackgroundRatio() const CV_OVERRIDE { return constantsHost_.TB_; }
void setBackgroundRatio(double ratio) CV_OVERRIDE { constantsHost_.TB_ = (float)ratio; }
double getVarThreshold() const CV_OVERRIDE { return constantsHost_.Tb_; }
void setVarThreshold(double varThreshold) CV_OVERRIDE { constantsHost_.Tb_ = (float)varThreshold; }
double getVarThresholdGen() const CV_OVERRIDE { return constantsHost_.Tg_; }
void setVarThresholdGen(double varThresholdGen) CV_OVERRIDE { constantsHost_.Tg_ = (float)varThresholdGen; }
double getVarInit() const CV_OVERRIDE { return constantsHost_.varInit_; }
void setVarInit(double varInit) CV_OVERRIDE { constantsHost_.varInit_ = (float)varInit; }
double getVarMin() const CV_OVERRIDE { return constantsHost_.varMin_; }
void setVarMin(double varMin) CV_OVERRIDE { constantsHost_.varMin_ = ::fminf((float)varMin, constantsHost_.varMax_); }
double getVarMax() const CV_OVERRIDE { return constantsHost_.varMax_; }
void setVarMax(double varMax) CV_OVERRIDE { constantsHost_.varMax_ = ::fmaxf(constantsHost_.varMin_, (float)varMax); }
double getComplexityReductionThreshold() const CV_OVERRIDE { return ct_; }
void setComplexityReductionThreshold(double ct) CV_OVERRIDE { ct_ = (float)ct; }
bool getDetectShadows() const CV_OVERRIDE { return detectShadows_; }
void setDetectShadows(bool detectShadows) CV_OVERRIDE { detectShadows_ = detectShadows; }
int getShadowValue() const CV_OVERRIDE { return constantsHost_.shadowVal_; }
void setShadowValue(int value) CV_OVERRIDE { constantsHost_.shadowVal_ = (uchar)value; }
double getShadowThreshold() const CV_OVERRIDE { return constantsHost_.tau_; }
void setShadowThreshold(double threshold) CV_OVERRIDE { constantsHost_.tau_ = (float)threshold; }
private:
void initialize(Size frameSize, int frameType, Stream &stream);
Constants constantsHost_;
Constants *constantsDevice_;
int history_;
float ct_;
bool detectShadows_;
Size frameSize_;
int frameType_;
int nframes_;
GpuMat weight_;
GpuMat variance_;
GpuMat mean_;
//keep track of number of modes per pixel
GpuMat bgmodelUsedModes_;
};
MOG2Impl::MOG2Impl(int history, double varThreshold, bool detectShadows) : frameSize_(0, 0), frameType_(0), nframes_(0)
{
history_ = history > 0 ? history : defaultHistory;
detectShadows_ = detectShadows;
ct_ = defaultCT;
setNMixtures(defaultNMixtures);
setBackgroundRatio(defaultBackgroundRatio);
setVarInit(defaultVarInit);
setVarMin(defaultVarMin);
setVarMax(defaultVarMax);
setVarThreshold(varThreshold > 0 ? (float)varThreshold : defaultVarThreshold);
setVarThresholdGen(defaultVarThresholdGen);
setShadowValue(defaultShadowValue);
setShadowThreshold(defaultShadowThreshold);
cudaSafeCall(cudaMalloc((void **)&constantsDevice_, sizeof(Constants)));
}
MOG2Impl::~MOG2Impl()
{
cudaFree(constantsDevice_);
}
void MOG2Impl::apply(InputArray image, OutputArray fgmask, double learningRate)
{
apply(image, fgmask, learningRate, Stream::Null());
}
void MOG2Impl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream &stream)
{
// default parameters of gaussian background detection algorithm
const int defaultHistory = 500; // Learning rate; alpha = 1/defaultHistory2
const float defaultVarThreshold = 4.0f * 4.0f;
const int defaultNMixtures = 5; // maximal number of Gaussians in mixture
const float defaultBackgroundRatio = 0.9f; // threshold sum of weights for background test
const float defaultVarThresholdGen = 3.0f * 3.0f;
const float defaultVarInit = 15.0f; // initial variance for new components
const float defaultVarMax = 5.0f * defaultVarInit;
const float defaultVarMin = 4.0f;
// additional parameters
const float defaultCT = 0.05f; // complexity reduction prior constant 0 - no reduction of number of components
const unsigned char defaultShadowValue = 127; // value to use in the segmentation mask for shadows, set 0 not to do shadow detection
const float defaultShadowThreshold = 0.5f; // Tau - shadow threshold, see the paper for explanation
class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2
{
public:
MOG2Impl(int history, double varThreshold, bool detectShadows);
void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE;
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE;
void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const CV_OVERRIDE;
int getHistory() const CV_OVERRIDE { return history_; }
void setHistory(int history) CV_OVERRIDE { history_ = history; }
int getNMixtures() const CV_OVERRIDE { return nmixtures_; }
void setNMixtures(int nmixtures) CV_OVERRIDE { nmixtures_ = nmixtures; }
double getBackgroundRatio() const CV_OVERRIDE { return backgroundRatio_; }
void setBackgroundRatio(double ratio) CV_OVERRIDE { backgroundRatio_ = (float) ratio; }
double getVarThreshold() const CV_OVERRIDE { return varThreshold_; }
void setVarThreshold(double varThreshold) CV_OVERRIDE { varThreshold_ = (float) varThreshold; }
double getVarThresholdGen() const CV_OVERRIDE { return varThresholdGen_; }
void setVarThresholdGen(double varThresholdGen) CV_OVERRIDE { varThresholdGen_ = (float) varThresholdGen; }
double getVarInit() const CV_OVERRIDE { return varInit_; }
void setVarInit(double varInit) CV_OVERRIDE { varInit_ = (float) varInit; }
double getVarMin() const CV_OVERRIDE { return varMin_; }
void setVarMin(double varMin) CV_OVERRIDE { varMin_ = (float) varMin; }
double getVarMax() const CV_OVERRIDE { return varMax_; }
void setVarMax(double varMax) CV_OVERRIDE { varMax_ = (float) varMax; }
double getComplexityReductionThreshold() const CV_OVERRIDE { return ct_; }
void setComplexityReductionThreshold(double ct) CV_OVERRIDE { ct_ = (float) ct; }
bool getDetectShadows() const CV_OVERRIDE { return detectShadows_; }
void setDetectShadows(bool detectShadows) CV_OVERRIDE { detectShadows_ = detectShadows; }
int getShadowValue() const CV_OVERRIDE { return shadowValue_; }
void setShadowValue(int value) CV_OVERRIDE { shadowValue_ = (uchar) value; }
using namespace cv::cuda::device::mog2;
GpuMat frame = _frame.getGpuMat();
double getShadowThreshold() const CV_OVERRIDE { return shadowThreshold_; }
void setShadowThreshold(double threshold) CV_OVERRIDE { shadowThreshold_ = (float) threshold; }
private:
void initialize(Size frameSize, int frameType);
int history_;
int nmixtures_;
float backgroundRatio_;
float varThreshold_;
float varThresholdGen_;
float varInit_;
float varMin_;
float varMax_;
float ct_;
bool detectShadows_;
uchar shadowValue_;
float shadowThreshold_;
Size frameSize_;
int frameType_;
int nframes_;
GpuMat weight_;
GpuMat variance_;
GpuMat mean_;
//keep track of number of modes per pixel
GpuMat bgmodelUsedModes_;
};
MOG2Impl::MOG2Impl(int history, double varThreshold, bool detectShadows) :
frameSize_(0, 0), frameType_(0), nframes_(0)
{
history_ = history > 0 ? history : defaultHistory;
varThreshold_ = varThreshold > 0 ? (float) varThreshold : defaultVarThreshold;
detectShadows_ = detectShadows;
nmixtures_ = defaultNMixtures;
backgroundRatio_ = defaultBackgroundRatio;
varInit_ = defaultVarInit;
varMax_ = defaultVarMax;
varMin_ = defaultVarMin;
varThresholdGen_ = defaultVarThresholdGen;
ct_ = defaultCT;
shadowValue_ = defaultShadowValue;
shadowThreshold_ = defaultShadowThreshold;
}
int ch = frame.channels();
int work_ch = ch;
void MOG2Impl::apply(InputArray image, OutputArray fgmask, double learningRate)
{
apply(image, fgmask, learningRate, Stream::Null());
}
if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.channels())
initialize(frame.size(), frame.type(), stream);
void MOG2Impl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream& stream)
{
using namespace cv::cuda::device::mog2;
_fgmask.create(frameSize_, CV_8UC1);
GpuMat fgmask = _fgmask.getGpuMat();
GpuMat frame = _frame.getGpuMat();
fgmask.setTo(Scalar::all(0), stream);
int ch = frame.channels();
int work_ch = ch;
++nframes_;
learningRate = learningRate >= 0 && nframes_ > 1 ? learningRate : 1.0 / std::min(2 * nframes_, history_);
CV_Assert(learningRate >= 0);
if (nframes_ == 0 || learningRate >= 1.0 || frame.size() != frameSize_ || work_ch != mean_.channels())
initialize(frame.size(), frame.type());
mog2_gpu(frame, frame.channels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_,
(float)learningRate, static_cast<float>(-learningRate * ct_), detectShadows_, constantsDevice_, StreamAccessor::getStream(stream));
}
void MOG2Impl::getBackgroundImage(OutputArray backgroundImage) const
{
getBackgroundImage(backgroundImage, Stream::Null());
}
void MOG2Impl::getBackgroundImage(OutputArray _backgroundImage, Stream &stream) const
{
using namespace cv::cuda::device::mog2;
_backgroundImage.create(frameSize_, frameType_);
GpuMat backgroundImage = _backgroundImage.getGpuMat();
getBackgroundImage2_gpu(backgroundImage.channels(), bgmodelUsedModes_, weight_, mean_, backgroundImage, constantsDevice_, StreamAccessor::getStream(stream));
}
void MOG2Impl::initialize(cv::Size frameSize, int frameType, Stream &stream)
{
using namespace cv::cuda::device::mog2;
_fgmask.create(frameSize_, CV_8UC1);
GpuMat fgmask = _fgmask.getGpuMat();
CV_Assert(frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4);
fgmask.setTo(Scalar::all(0), stream);
frameSize_ = frameSize;
frameType_ = frameType;
nframes_ = 0;
++nframes_;
learningRate = learningRate >= 0 && nframes_ > 1 ? learningRate : 1.0 / std::min(2 * nframes_, history_);
CV_Assert( learningRate >= 0 );
const int ch = CV_MAT_CN(frameType);
const int work_ch = ch;
mog2_gpu(frame, frame.channels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_,
(float) learningRate, static_cast<float>(-learningRate * ct_), detectShadows_, StreamAccessor::getStream(stream));
}
// for each gaussian mixture of each pixel bg model we store ...
// the mixture weight (w),
// the mean (nchannels values) and
// the covariance
weight_.create(frameSize.height * getNMixtures(), frameSize_.width, CV_32FC1);
variance_.create(frameSize.height * getNMixtures(), frameSize_.width, CV_32FC1);
mean_.create(frameSize.height * getNMixtures(), frameSize_.width, CV_32FC(work_ch));
void MOG2Impl::getBackgroundImage(OutputArray backgroundImage) const
{
getBackgroundImage(backgroundImage, Stream::Null());
}
void MOG2Impl::getBackgroundImage(OutputArray _backgroundImage, Stream& stream) const
{
using namespace cv::cuda::device::mog2;
_backgroundImage.create(frameSize_, frameType_);
GpuMat backgroundImage = _backgroundImage.getGpuMat();
getBackgroundImage2_gpu(backgroundImage.channels(), bgmodelUsedModes_, weight_, mean_, backgroundImage, StreamAccessor::getStream(stream));
}
void MOG2Impl::initialize(cv::Size frameSize, int frameType)
{
using namespace cv::cuda::device::mog2;
CV_Assert( frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4 );
frameSize_ = frameSize;
frameType_ = frameType;
nframes_ = 0;
int ch = CV_MAT_CN(frameType);
int work_ch = ch;
// for each gaussian mixture of each pixel bg model we store ...
// the mixture weight (w),
// the mean (nchannels values) and
// the covariance
weight_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
variance_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC1);
mean_.create(frameSize.height * nmixtures_, frameSize_.width, CV_32FC(work_ch));
//make the array for keeping track of the used modes per pixel - all zeros at start
bgmodelUsedModes_.create(frameSize_, CV_8UC1);
bgmodelUsedModes_.setTo(Scalar::all(0));
//make the array for keeping track of the used modes per pixel - all zeros at start
bgmodelUsedModes_.create(frameSize_, CV_8UC1);
bgmodelUsedModes_.setTo(Scalar::all(0));
loadConstants(nmixtures_, varThreshold_, backgroundRatio_, varThresholdGen_, varInit_, varMin_, varMax_, shadowThreshold_, shadowValue_);
}
cudaSafeCall(cudaMemcpyAsync(constantsDevice_, &constantsHost_, sizeof(Constants), cudaMemcpyHostToDevice, StreamAccessor::getStream(stream)));
}
} // namespace
Ptr<cuda::BackgroundSubtractorMOG2> cv::cuda::createBackgroundSubtractorMOG2(int history, double varThreshold, bool detectShadows)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册