Merge pull request #1151 from jet47:gpubgsegm-refactoring

This commit is contained in:
Andrey Pavlenko 2013-08-05 10:19:59 +04:00 committed by OpenCV Buildbot
commit 487ff4f3aa
19 changed files with 1700 additions and 1769 deletions

View File

@ -6,4 +6,4 @@ set(the_description "GPU-accelerated Background Segmentation")
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127 /wd4324 /wd4512 -Wundef -Wmissing-declarations) ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127 /wd4324 /wd4512 -Wundef -Wmissing-declarations)
ocv_define_module(gpubgsegm opencv_video opencv_imgproc opencv_legacy opencv_gpuarithm opencv_gpufilters opencv_gpuimgproc) ocv_define_module(gpubgsegm opencv_video OPTIONAL opencv_legacy opencv_imgproc opencv_gpuarithm opencv_gpufilters opencv_gpuimgproc)

View File

@ -5,128 +5,11 @@ Background Segmentation
gpu::FGDStatModel gpu::BackgroundSubtractorMOG
----------------- ----------------------------
.. ocv:class:: gpu::FGDStatModel Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
Class used for background/foreground segmentation. :: .. ocv:class:: gpu::BackgroundSubtractorMOG : public cv::BackgroundSubtractorMOG
class FGDStatModel
{
public:
struct Params
{
...
};
explicit FGDStatModel(int out_cn = 3);
explicit FGDStatModel(const cv::gpu::GpuMat& firstFrame, const Params& params = Params(), int out_cn = 3);
~FGDStatModel();
void create(const cv::gpu::GpuMat& firstFrame, const Params& params = Params());
void release();
int update(const cv::gpu::GpuMat& curFrame);
//8UC3 or 8UC4 reference background image
cv::gpu::GpuMat background;
//8UC1 foreground image
cv::gpu::GpuMat foreground;
std::vector< std::vector<cv::Point> > foreground_regions;
};
The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [FGD2003]_.
The results are available through the class fields:
.. ocv:member:: cv::gpu::GpuMat background
The output background image.
.. ocv:member:: cv::gpu::GpuMat foreground
The output foreground mask as an 8-bit binary image.
.. ocv:member:: cv::gpu::GpuMat foreground_regions
The output foreground regions calculated by :ocv:func:`findContours`.
gpu::FGDStatModel::FGDStatModel
-------------------------------
Constructors.
.. ocv:function:: gpu::FGDStatModel::FGDStatModel(int out_cn = 3)
.. ocv:function:: gpu::FGDStatModel::FGDStatModel(const cv::gpu::GpuMat& firstFrame, const Params& params = Params(), int out_cn = 3)
:param firstFrame: First frame from video stream. Supports 3- and 4-channels input ( ``CV_8UC3`` and ``CV_8UC4`` ).
:param params: Algorithm's parameters. See [FGD2003]_ for explanation.
:param out_cn: Channels count in output result and inner buffers. Can be 3 or 4. 4-channels version requires more memory, but works a bit faster.
.. seealso:: :ocv:func:`gpu::FGDStatModel::create`
gpu::FGDStatModel::create
-------------------------
Initializes background model.
.. ocv:function:: void gpu::FGDStatModel::create(const cv::gpu::GpuMat& firstFrame, const Params& params = Params())
:param firstFrame: First frame from video stream. Supports 3- and 4-channels input ( ``CV_8UC3`` and ``CV_8UC4`` ).
:param params: Algorithm's parameters. See [FGD2003]_ for explanation.
gpu::FGDStatModel::release
--------------------------
Releases all inner buffer's memory.
.. ocv:function:: void gpu::FGDStatModel::release()
gpu::FGDStatModel::update
--------------------------
Updates the background model and returns foreground regions count.
.. ocv:function:: int gpu::FGDStatModel::update(const cv::gpu::GpuMat& curFrame)
:param curFrame: Next video frame.
gpu::MOG_GPU
------------
.. ocv:class:: gpu::MOG_GPU
Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm. ::
class MOG_GPU
{
public:
MOG_GPU(int nmixtures = -1);
void initialize(Size frameSize, int frameType);
void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = 0.0f, Stream& stream = Stream::Null());
void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
void release();
int history;
float varThreshold;
float backgroundRatio;
float noiseSigma;
};
The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [MOG2001]_. The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [MOG2001]_.
@ -134,275 +17,108 @@ The class discriminates between foreground and background pixels by building and
gpu::MOG_GPU::MOG_GPU gpu::createBackgroundSubtractorMOG
--------------------- ----------------------------------
The constructor. Creates mixture-of-gaussian background subtractor
.. ocv:function:: gpu::MOG_GPU::MOG_GPU(int nmixtures = -1) .. ocv:function:: Ptr<gpu::BackgroundSubtractorMOG> gpu::createBackgroundSubtractorMOG(int history=200, int nmixtures=5, double backgroundRatio=0.7, double noiseSigma=0)
:param history: Length of the history.
:param nmixtures: Number of Gaussian mixtures. :param nmixtures: Number of Gaussian mixtures.
Default constructor sets all parameters to default values. :param backgroundRatio: Background ratio.
:param noiseSigma: Noise strength (standard deviation of the brightness or each color channel). 0 means some automatic value.
gpu::MOG_GPU::operator() gpu::BackgroundSubtractorMOG2
------------------------ -----------------------------
Updates the background model and returns the foreground mask. Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
.. ocv:function:: void gpu::MOG_GPU::operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = 0.0f, Stream& stream = Stream::Null()) .. ocv:class:: gpu::BackgroundSubtractorMOG2 : public cv::BackgroundSubtractorMOG2
:param frame: Next video frame.
:param fgmask: The output foreground mask as an 8-bit binary image.
:param stream: Stream for the asynchronous version.
gpu::MOG_GPU::getBackgroundImage
--------------------------------
Computes a background image.
.. ocv:function:: void gpu::MOG_GPU::getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const
:param backgroundImage: The output background image.
:param stream: Stream for the asynchronous version.
gpu::MOG_GPU::release
---------------------
Releases all inner buffer's memory.
.. ocv:function:: void gpu::MOG_GPU::release()
gpu::MOG2_GPU
-------------
.. ocv:class:: gpu::MOG2_GPU
Gaussian Mixture-based Background/Foreground Segmentation Algorithm. ::
class MOG2_GPU
{
public:
MOG2_GPU(int nmixtures = -1);
void initialize(Size frameSize, int frameType);
void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = 0.0f, Stream& stream = Stream::Null());
void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
void release();
// parameters
...
};
The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [MOG2004]_. The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [MOG2004]_.
Here are important members of the class that control the algorithm, which you can set after constructing the class instance:
.. ocv:member:: float backgroundRatio
Threshold defining whether the component is significant enough to be included into the background model ( corresponds to ``TB=1-cf`` from the paper??which paper??). ``cf=0.1 => TB=0.9`` is default. For ``alpha=0.001``, it means that the mode should exist for approximately 105 frames before it is considered foreground.
.. ocv:member:: float varThreshold
Threshold for the squared Mahalanobis distance that helps decide when a sample is close to the existing components (corresponds to ``Tg``). If it is not close to any component, a new component is generated. ``3 sigma => Tg=3*3=9`` is default. A smaller ``Tg`` value generates more components. A higher ``Tg`` value may result in a small number of components but they can grow too large.
.. ocv:member:: float fVarInit
Initial variance for the newly generated components. It affects the speed of adaptation. The parameter value is based on your estimate of the typical standard deviation from the images. OpenCV uses 15 as a reasonable value.
.. ocv:member:: float fVarMin
Parameter used to further control the variance.
.. ocv:member:: float fVarMax
Parameter used to further control the variance.
.. ocv:member:: float fCT
Complexity reduction parameter. This parameter defines the number of samples needed to accept to prove the component exists. ``CT=0.05`` is a default value for all the samples. By setting ``CT=0`` you get an algorithm very similar to the standard Stauffer&Grimson algorithm.
.. ocv:member:: uchar nShadowDetection
The value for marking shadow pixels in the output foreground mask. Default value is 127.
.. ocv:member:: float fTau
Shadow threshold. The shadow is detected if the pixel is a darker version of the background. ``Tau`` is a threshold defining how much darker the shadow can be. ``Tau= 0.5`` means that if a pixel is more than twice darker then it is not shadow. See [ShadowDetect2003]_.
.. ocv:member:: bool bShadowDetection
Parameter defining whether shadow detection should be enabled.
.. seealso:: :ocv:class:`BackgroundSubtractorMOG2` .. seealso:: :ocv:class:`BackgroundSubtractorMOG2`
gpu::MOG2_GPU::MOG2_GPU gpu::createBackgroundSubtractorMOG2
----------------------- -----------------------------------
The constructor. Creates MOG2 Background Subtractor
.. ocv:function:: gpu::MOG2_GPU::MOG2_GPU(int nmixtures = -1) .. ocv:function:: Ptr<gpu::BackgroundSubtractorMOG2> gpu::createBackgroundSubtractorMOG2( int history=500, double varThreshold=16, bool detectShadows=true )
:param nmixtures: Number of Gaussian mixtures. :param history: Length of the history.
Default constructor sets all parameters to default values. :param varThreshold: Threshold on the squared Mahalanobis distance between the pixel and the model to decide whether a pixel is well described by the background model. This parameter does not affect the background update.
:param detectShadows: If true, the algorithm will detect shadows and mark them. It decreases the speed a bit, so if you do not need this feature, set the parameter to false.
gpu::MOG2_GPU::operator() gpu::BackgroundSubtractorGMG
------------------------- ----------------------------
Updates the background model and returns the foreground mask. Background/Foreground Segmentation Algorithm.
.. ocv:function:: void gpu::MOG2_GPU::operator()( const GpuMat& frame, GpuMat& fgmask, float learningRate=-1.0f, Stream& stream=Stream::Null() ) .. ocv:class:: gpu::BackgroundSubtractorGMG : public cv::BackgroundSubtractorGMG
:param frame: Next video frame.
:param fgmask: The output foreground mask as an 8-bit binary image.
:param stream: Stream for the asynchronous version.
gpu::MOG2_GPU::getBackgroundImage
---------------------------------
Computes a background image.
.. ocv:function:: void gpu::MOG2_GPU::getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const
:param backgroundImage: The output background image.
:param stream: Stream for the asynchronous version.
gpu::MOG2_GPU::release
----------------------
Releases all inner buffer's memory.
.. ocv:function:: void gpu::MOG2_GPU::release()
gpu::GMG_GPU
------------
.. ocv:class:: gpu::GMG_GPU
Class used for background/foreground segmentation. ::
class GMG_GPU_GPU
{
public:
GMG_GPU();
void initialize(Size frameSize, float min = 0.0f, float max = 255.0f);
void operator ()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
void release();
int maxFeatures;
float learningRate;
int numInitializationFrames;
int quantizationLevels;
float backgroundPrior;
float decisionThreshold;
int smoothingRadius;
...
};
The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [GMG2012]_. The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [GMG2012]_.
Here are important members of the class that control the algorithm, which you can set after constructing the class instance:
.. ocv:member:: int maxFeatures
Total number of distinct colors to maintain in histogram. gpu::createBackgroundSubtractorGMG
----------------------------------
Creates GMG Background Subtractor
.. ocv:member:: float learningRate .. ocv:function:: Ptr<gpu::BackgroundSubtractorGMG> gpu::createBackgroundSubtractorGMG(int initializationFrames = 120, double decisionThreshold = 0.8)
Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms. :param initializationFrames: Number of frames of video to use to initialize histograms.
.. ocv:member:: int numInitializationFrames :param decisionThreshold: Value above which pixel is determined to be FG.
Number of frames of video to use to initialize histograms.
.. ocv:member:: int quantizationLevels
Number of discrete levels in each channel to be used in histograms.
.. ocv:member:: float backgroundPrior
Prior probability that any given pixel is a background pixel. A sensitivity parameter.
.. ocv:member:: float decisionThreshold
Value above which pixel is determined to be FG.
.. ocv:member:: float smoothingRadius
Smoothing radius, in pixels, for cleaning up FG image.
gpu::GMG_GPU::GMG_GPU gpu::BackgroundSubtractorFGD
--------------------- ----------------------------
The default constructor.
.. ocv:function:: gpu::GMG_GPU::GMG_GPU() .. ocv:class:: gpu::BackgroundSubtractorFGD : public cv::BackgroundSubtractor
Default constructor sets all parameters to default values. The class discriminates between foreground and background pixels by building and maintaining a model of the background. Any pixel which does not fit this model is then deemed to be foreground. The class implements algorithm described in [FGD2003]_. ::
class CV_EXPORTS BackgroundSubtractorFGD : public cv::BackgroundSubtractor
{
public:
virtual void getForegroundRegions(OutputArrayOfArrays foreground_regions) = 0;
};
.. seealso:: :ocv:class:`BackgroundSubtractor`
gpu::GMG_GPU::initialize gpu::BackgroundSubtractorFGD::getForegroundRegions
------------------------ --------------------------------------------------
Initialize background model and allocates all inner buffers. Returns the output foreground regions calculated by :ocv:func:`findContours`.
.. ocv:function:: void gpu::GMG_GPU::initialize(Size frameSize, float min = 0.0f, float max = 255.0f) .. ocv:function:: void gpu::BackgroundSubtractorFGD::getForegroundRegions(OutputArrayOfArrays foreground_regions)
:param frameSize: Input frame size. :params foreground_regions: Output array (CPU memory).
:param min: Minimum value taken on by pixels in image sequence. Usually 0.
:param max: Maximum value taken on by pixels in image sequence, e.g. 1.0 or 255.
gpu::GMG_GPU::operator() gpu::createBackgroundSubtractorFGD
------------------------ ----------------------------------
Updates the background model and returns the foreground mask Creates FGD Background Subtractor
.. ocv:function:: void gpu::GMG_GPU::operator ()( const GpuMat& frame, GpuMat& fgmask, float learningRate=-1.0f, Stream& stream=Stream::Null() ) .. ocv:function:: Ptr<gpu::BackgroundSubtractorGMG> gpu::createBackgroundSubtractorFGD(const FGDParams& params = FGDParams())
:param frame: Next video frame. :param params: Algorithm's parameters. See [FGD2003]_ for explanation.
:param fgmask: The output foreground mask as an 8-bit binary image.
:param stream: Stream for the asynchronous version.
gpu::GMG_GPU::release
---------------------
Releases all inner buffer's memory.
.. ocv:function:: void gpu::GMG_GPU::release()
.. [FGD2003] Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian. *Foreground Object Detection from Videos Containing Complex Background*. ACM MM2003 9p, 2003. .. [FGD2003] Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian. *Foreground Object Detection from Videos Containing Complex Background*. ACM MM2003 9p, 2003.
.. [MOG2001] P. KadewTraKuPong and R. Bowden. *An improved adaptive background mixture model for real-time tracking with shadow detection*. Proc. 2nd European Workshop on Advanced Video-Based Surveillance Systems, 2001 .. [MOG2001] P. KadewTraKuPong and R. Bowden. *An improved adaptive background mixture model for real-time tracking with shadow detection*. Proc. 2nd European Workshop on Advanced Video-Based Surveillance Systems, 2001
.. [MOG2004] Z. Zivkovic. *Improved adaptive Gausian mixture model for background subtraction*. International Conference Pattern Recognition, UK, August, 2004 .. [MOG2004] Z. Zivkovic. *Improved adaptive Gausian mixture model for background subtraction*. International Conference Pattern Recognition, UK, August, 2004
.. [ShadowDetect2003] Prati, Mikic, Trivedi and Cucchiarra. *Detecting Moving Shadows...*. IEEE PAMI, 2003
.. [GMG2012] A. Godbehere, A. Matsukawa and K. Goldberg. *Visual Tracking of Human Visitors under Variable-Lighting Conditions for a Responsive Audio Art Installation*. American Control Conference, Montreal, June 2012 .. [GMG2012] A. Godbehere, A. Matsukawa and K. Goldberg. *Visual Tracking of Human Visitors under Variable-Lighting Conditions for a Responsive Audio Art Installation*. American Control Conference, Montreal, June 2012

View File

@ -47,20 +47,76 @@
# error gpubgsegm.hpp header must be compiled as C++ # error gpubgsegm.hpp header must be compiled as C++
#endif #endif
#include <memory>
#include "opencv2/core/gpu.hpp" #include "opencv2/core/gpu.hpp"
#include "opencv2/gpufilters.hpp" #include "opencv2/video/background_segm.hpp"
namespace cv { namespace gpu { namespace cv { namespace gpu {
// Foreground Object Detection from Videos Containing Complex Background. ////////////////////////////////////////////////////
// Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian. // MOG
// ACM MM2003 9p
class CV_EXPORTS FGDStatModel class CV_EXPORTS BackgroundSubtractorMOG : public cv::BackgroundSubtractorMOG
{ {
public: public:
struct CV_EXPORTS Params using cv::BackgroundSubtractorMOG::apply;
using cv::BackgroundSubtractorMOG::getBackgroundImage;
virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0;
virtual void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const = 0;
};
CV_EXPORTS Ptr<gpu::BackgroundSubtractorMOG>
createBackgroundSubtractorMOG(int history = 200, int nmixtures = 5,
double backgroundRatio = 0.7, double noiseSigma = 0);
////////////////////////////////////////////////////
// MOG2
class CV_EXPORTS BackgroundSubtractorMOG2 : public cv::BackgroundSubtractorMOG2
{
public:
using cv::BackgroundSubtractorMOG2::apply;
using cv::BackgroundSubtractorMOG2::getBackgroundImage;
virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0;
virtual void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const = 0;
};
CV_EXPORTS Ptr<gpu::BackgroundSubtractorMOG2>
createBackgroundSubtractorMOG2(int history = 500, double varThreshold = 16,
bool detectShadows = true);
////////////////////////////////////////////////////
// GMG
class CV_EXPORTS BackgroundSubtractorGMG : public cv::BackgroundSubtractorGMG
{
public:
using cv::BackgroundSubtractorGMG::apply;
virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0;
};
CV_EXPORTS Ptr<gpu::BackgroundSubtractorGMG>
createBackgroundSubtractorGMG(int initializationFrames = 120, double decisionThreshold = 0.8);
////////////////////////////////////////////////////
// FGD
/**
* Foreground Object Detection from Videos Containing Complex Background.
* Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian.
* ACM MM2003 9p
*/
class CV_EXPORTS BackgroundSubtractorFGD : public cv::BackgroundSubtractor
{
public:
virtual void getForegroundRegions(OutputArrayOfArrays foreground_regions) = 0;
};
struct CV_EXPORTS FGDParams
{ {
int Lc; // Quantized levels per 'color' component. Power of two, typically 32, 64 or 128. int Lc; // Quantized levels per 'color' component. Power of two, typically 32, 64 or 128.
int N1c; // Number of color vectors used to model normal background color variation at a given pixel. int N1c; // Number of color vectors used to model normal background color variation at a given pixel.
@ -85,245 +141,11 @@ public:
float minArea; // Discard foreground blobs whose bounding box is smaller than this threshold. float minArea; // Discard foreground blobs whose bounding box is smaller than this threshold.
// default Params // default Params
Params(); FGDParams();
}; };
// out_cn - channels count in output result (can be 3 or 4) CV_EXPORTS Ptr<gpu::BackgroundSubtractorFGD>
// 4-channels require more memory, but a bit faster createBackgroundSubtractorFGD(const FGDParams& params = FGDParams());
explicit FGDStatModel(int out_cn = 3);
explicit FGDStatModel(const cv::gpu::GpuMat& firstFrame, const Params& params = Params(), int out_cn = 3);
~FGDStatModel();
void create(const cv::gpu::GpuMat& firstFrame, const Params& params = Params());
void release();
int update(const cv::gpu::GpuMat& curFrame);
//8UC3 or 8UC4 reference background image
cv::gpu::GpuMat background;
//8UC1 foreground image
cv::gpu::GpuMat foreground;
std::vector< std::vector<cv::Point> > foreground_regions;
private:
FGDStatModel(const FGDStatModel&);
FGDStatModel& operator=(const FGDStatModel&);
class Impl;
std::auto_ptr<Impl> impl_;
};
/*!
Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
The class implements the following algorithm:
"An improved adaptive background mixture model for real-time tracking with shadow detection"
P. KadewTraKuPong and R. Bowden,
Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
*/
class CV_EXPORTS MOG_GPU
{
public:
//! the default constructor
MOG_GPU(int nmixtures = -1);
//! re-initiaization method
void initialize(Size frameSize, int frameType);
//! the update operator
void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = 0.0f, Stream& stream = Stream::Null());
//! computes a background image which are the mean of all background gaussians
void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
//! releases all inner buffers
void release();
int history;
float varThreshold;
float backgroundRatio;
float noiseSigma;
private:
int nmixtures_;
Size frameSize_;
int frameType_;
int nframes_;
GpuMat weight_;
GpuMat sortKey_;
GpuMat mean_;
GpuMat var_;
};
/*!
The class implements the following algorithm:
"Improved adaptive Gausian mixture model for background subtraction"
Z.Zivkovic
International Conference Pattern Recognition, UK, August, 2004.
http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
*/
class CV_EXPORTS MOG2_GPU
{
public:
//! the default constructor
MOG2_GPU(int nmixtures = -1);
//! re-initiaization method
void initialize(Size frameSize, int frameType);
//! the update operator
void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
//! computes a background image which are the mean of all background gaussians
void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
//! releases all inner buffers
void release();
// parameters
// you should call initialize after parameters changes
int history;
//! here it is the maximum allowed number of mixture components.
//! Actual number is determined dynamically per pixel
float varThreshold;
// threshold on the squared Mahalanobis distance to decide if it is well described
// by the background model or not. Related to Cthr from the paper.
// This does not influence the update of the background. A typical value could be 4 sigma
// and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
/////////////////////////
// less important parameters - things you might change but be carefull
////////////////////////
float backgroundRatio;
// corresponds to fTB=1-cf from the paper
// TB - threshold when the component becomes significant enough to be included into
// the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
// For alpha=0.001 it means that the mode should exist for approximately 105 frames before
// it is considered foreground
// float noiseSigma;
float varThresholdGen;
//correspondts to Tg - threshold on the squared Mahalan. dist. to decide
//when a sample is close to the existing components. If it is not close
//to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
//Smaller Tg leads to more generated components and higher Tg might make
//lead to small number of components but they can grow too large
float fVarInit;
float fVarMin;
float fVarMax;
//initial variance for the newly generated components.
//It will will influence the speed of adaptation. A good guess should be made.
//A simple way is to estimate the typical standard deviation from the images.
//I used here 10 as a reasonable value
// min and max can be used to further control the variance
float fCT; //CT - complexity reduction prior
//this is related to the number of samples needed to accept that a component
//actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
//the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
//shadow detection parameters
bool bShadowDetection; //default 1 - do shadow detection
unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
float fTau;
// Tau - shadow threshold. The shadow is detected if the pixel is darker
//version of the background. Tau is a threshold on how much darker the shadow can be.
//Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
//See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
private:
int nmixtures_;
Size frameSize_;
int frameType_;
int nframes_;
GpuMat weight_;
GpuMat variance_;
GpuMat mean_;
GpuMat bgmodelUsedModes_; //keep track of number of modes per pixel
};
/**
* Background Subtractor module. Takes a series of images and returns a sequence of mask (8UC1)
* images of the same size, where 255 indicates Foreground and 0 represents Background.
* This class implements an algorithm described in "Visual Tracking of Human Visitors under
* Variable-Lighting Conditions for a Responsive Audio Art Installation," A. Godbehere,
* A. Matsukawa, K. Goldberg, American Control Conference, Montreal, June 2012.
*/
class CV_EXPORTS GMG_GPU
{
public:
GMG_GPU();
/**
* Validate parameters and set up data structures for appropriate frame size.
* @param frameSize Input frame size
* @param min Minimum value taken on by pixels in image sequence. Usually 0
* @param max Maximum value taken on by pixels in image sequence. e.g. 1.0 or 255
*/
void initialize(Size frameSize, float min = 0.0f, float max = 255.0f);
/**
* Performs single-frame background subtraction and builds up a statistical background image
* model.
* @param frame Input frame
* @param fgmask Output mask image representing foreground and background pixels
* @param stream Stream for the asynchronous version
*/
void operator ()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
//! Releases all inner buffers
void release();
//! Total number of distinct colors to maintain in histogram.
int maxFeatures;
//! Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms.
float learningRate;
//! Number of frames of video to use to initialize histograms.
int numInitializationFrames;
//! Number of discrete levels in each channel to be used in histograms.
int quantizationLevels;
//! Prior probability that any given pixel is a background pixel. A sensitivity parameter.
float backgroundPrior;
//! Value above which pixel is determined to be FG.
float decisionThreshold;
//! Smoothing radius, in pixels, for cleaning up FG image.
int smoothingRadius;
//! Perform background model update.
bool updateBackgroundModel;
private:
float maxVal_, minVal_;
Size frameSize_;
int frameNum_;
GpuMat nfeatures_;
GpuMat colors_;
GpuMat weights_;
Ptr<gpu::Filter> boxFilter_;
GpuMat buf_;
};
}} // namespace cv { namespace gpu { }} // namespace cv { namespace gpu {

View File

@ -41,7 +41,14 @@
//M*/ //M*/
#include "perf_precomp.hpp" #include "perf_precomp.hpp"
#ifdef HAVE_OPENCV_LEGACY
# include "opencv2/legacy.hpp" # include "opencv2/legacy.hpp"
#endif
#ifdef HAVE_OPENCV_GPUIMGPROC
# include "opencv2/gpuimgproc.hpp"
#endif
using namespace std; using namespace std;
using namespace testing; using namespace testing;
@ -59,6 +66,13 @@ using namespace perf;
# define BUILD_WITH_VIDEO_INPUT_SUPPORT 0 # define BUILD_WITH_VIDEO_INPUT_SUPPORT 0
#endif #endif
//////////////////////////////////////////////////////
// FGDStatModel
#if BUILD_WITH_VIDEO_INPUT_SUPPORT
#ifdef HAVE_OPENCV_LEGACY
namespace cv namespace cv
{ {
template<> void Ptr<CvBGStatModel>::delete_obj() template<> void Ptr<CvBGStatModel>::delete_obj()
@ -67,10 +81,7 @@ namespace cv
} }
} }
////////////////////////////////////////////////////// #endif
// FGDStatModel
#if BUILD_WITH_VIDEO_INPUT_SUPPORT
DEF_PARAM_TEST_1(Video, string); DEF_PARAM_TEST_1(Video, string);
@ -90,10 +101,10 @@ PERF_TEST_P(Video, FGDStatModel,
if (PERF_RUN_GPU()) if (PERF_RUN_GPU())
{ {
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame), foreground;
cv::gpu::FGDStatModel d_model(4); cv::Ptr<cv::gpu::BackgroundSubtractorFGD> d_fgd = cv::gpu::createBackgroundSubtractorFGD();
d_model.create(d_frame); d_fgd->apply(d_frame, foreground);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
@ -103,18 +114,22 @@ PERF_TEST_P(Video, FGDStatModel,
d_frame.upload(frame); d_frame.upload(frame);
startTimer(); next(); startTimer(); next();
d_model.update(d_frame); d_fgd->apply(d_frame, foreground);
stopTimer(); stopTimer();
} }
const cv::gpu::GpuMat background = d_model.background;
const cv::gpu::GpuMat foreground = d_model.foreground;
GPU_SANITY_CHECK(background, 1e-2, ERROR_RELATIVE);
GPU_SANITY_CHECK(foreground, 1e-2, ERROR_RELATIVE); GPU_SANITY_CHECK(foreground, 1e-2, ERROR_RELATIVE);
#ifdef HAVE_OPENCV_GPUIMGPROC
cv::gpu::GpuMat background3, background;
d_fgd->getBackgroundImage(background3);
cv::gpu::cvtColor(background3, background, cv::COLOR_BGR2BGRA);
GPU_SANITY_CHECK(background, 1e-2, ERROR_RELATIVE);
#endif
} }
else else
{ {
#ifdef HAVE_OPENCV_LEGACY
IplImage ipl_frame = frame; IplImage ipl_frame = frame;
cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame)); cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame));
@ -135,6 +150,9 @@ PERF_TEST_P(Video, FGDStatModel,
CPU_SANITY_CHECK(background); CPU_SANITY_CHECK(background);
CPU_SANITY_CHECK(foreground); CPU_SANITY_CHECK(foreground);
#else
FAIL_NO_CPU();
#endif
} }
} }
@ -176,11 +194,12 @@ PERF_TEST_P(Video_Cn_LearningRate, MOG,
if (PERF_RUN_GPU()) if (PERF_RUN_GPU())
{ {
cv::Ptr<cv::BackgroundSubtractor> d_mog = cv::gpu::createBackgroundSubtractorMOG();
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame);
cv::gpu::MOG_GPU d_mog;
cv::gpu::GpuMat foreground; cv::gpu::GpuMat foreground;
d_mog(d_frame, foreground, learningRate); d_mog->apply(d_frame, foreground, learningRate);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
@ -200,7 +219,7 @@ PERF_TEST_P(Video_Cn_LearningRate, MOG,
d_frame.upload(frame); d_frame.upload(frame);
startTimer(); next(); startTimer(); next();
d_mog(d_frame, foreground, learningRate); d_mog->apply(d_frame, foreground, learningRate);
stopTimer(); stopTimer();
} }
@ -273,13 +292,13 @@ PERF_TEST_P(Video_Cn, MOG2,
if (PERF_RUN_GPU()) if (PERF_RUN_GPU())
{ {
cv::gpu::MOG2_GPU d_mog2; cv::Ptr<cv::BackgroundSubtractorMOG2> d_mog2 = cv::gpu::createBackgroundSubtractorMOG2();
d_mog2.bShadowDetection = false; d_mog2->setDetectShadows(false);
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame);
cv::gpu::GpuMat foreground; cv::gpu::GpuMat foreground;
d_mog2(d_frame, foreground); d_mog2->apply(d_frame, foreground);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
@ -299,7 +318,7 @@ PERF_TEST_P(Video_Cn, MOG2,
d_frame.upload(frame); d_frame.upload(frame);
startTimer(); next(); startTimer(); next();
d_mog2(d_frame, foreground); d_mog2->apply(d_frame, foreground);
stopTimer(); stopTimer();
} }
@ -307,8 +326,8 @@ PERF_TEST_P(Video_Cn, MOG2,
} }
else else
{ {
cv::Ptr<cv::BackgroundSubtractor> mog2 = cv::createBackgroundSubtractorMOG2(); cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::createBackgroundSubtractorMOG2();
mog2->set("detectShadows", false); mog2->setDetectShadows(false);
cv::Mat foreground; cv::Mat foreground;
@ -359,8 +378,9 @@ PERF_TEST_P(Video_Cn, MOG2GetBackgroundImage,
if (PERF_RUN_GPU()) if (PERF_RUN_GPU())
{ {
cv::Ptr<cv::BackgroundSubtractor> d_mog2 = cv::gpu::createBackgroundSubtractorMOG2();
cv::gpu::GpuMat d_frame; cv::gpu::GpuMat d_frame;
cv::gpu::MOG2_GPU d_mog2;
cv::gpu::GpuMat d_foreground; cv::gpu::GpuMat d_foreground;
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
@ -380,12 +400,12 @@ PERF_TEST_P(Video_Cn, MOG2GetBackgroundImage,
d_frame.upload(frame); d_frame.upload(frame);
d_mog2(d_frame, d_foreground); d_mog2->apply(d_frame, d_foreground);
} }
cv::gpu::GpuMat background; cv::gpu::GpuMat background;
TEST_CYCLE() d_mog2.getBackgroundImage(background); TEST_CYCLE() d_mog2->getBackgroundImage(background);
GPU_SANITY_CHECK(background, 1); GPU_SANITY_CHECK(background, 1);
} }
@ -460,10 +480,10 @@ PERF_TEST_P(Video_Cn_MaxFeatures, GMG,
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame);
cv::gpu::GpuMat foreground; cv::gpu::GpuMat foreground;
cv::gpu::GMG_GPU d_gmg; cv::Ptr<cv::BackgroundSubtractorGMG> d_gmg = cv::gpu::createBackgroundSubtractorGMG();
d_gmg.maxFeatures = maxFeatures; d_gmg->setMaxFeatures(maxFeatures);
d_gmg(d_frame, foreground); d_gmg->apply(d_frame, foreground);
for (int i = 0; i < 150; ++i) for (int i = 0; i < 150; ++i)
{ {
@ -488,7 +508,7 @@ PERF_TEST_P(Video_Cn_MaxFeatures, GMG,
d_frame.upload(frame); d_frame.upload(frame);
startTimer(); next(); startTimer(); next();
d_gmg(d_frame, foreground); d_gmg->apply(d_frame, foreground);
stopTimer(); stopTimer();
} }
@ -499,9 +519,8 @@ PERF_TEST_P(Video_Cn_MaxFeatures, GMG,
cv::Mat foreground; cv::Mat foreground;
cv::Mat zeros(frame.size(), CV_8UC1, cv::Scalar::all(0)); cv::Mat zeros(frame.size(), CV_8UC1, cv::Scalar::all(0));
cv::Ptr<cv::BackgroundSubtractor> gmg = cv::createBackgroundSubtractorGMG(); cv::Ptr<cv::BackgroundSubtractorGMG> gmg = cv::createBackgroundSubtractorGMG();
gmg->set("maxFeatures", maxFeatures); gmg->setMaxFeatures(maxFeatures);
//gmg.initialize(frame.size(), 0.0, 255.0);
gmg->apply(frame, foreground); gmg->apply(frame, foreground);

View File

@ -57,6 +57,8 @@
#include "opencv2/gpubgsegm.hpp" #include "opencv2/gpubgsegm.hpp"
#include "opencv2/video.hpp" #include "opencv2/video.hpp"
#include "opencv2/opencv_modules.hpp"
#ifdef GTEST_CREATE_SHARED_LIBRARY #ifdef GTEST_CREATE_SHARED_LIBRARY
#error no modules except ts should have GTEST_CREATE_SHARED_LIBRARY defined #error no modules except ts should have GTEST_CREATE_SHARED_LIBRARY defined
#endif #endif

View File

@ -53,7 +53,7 @@
using namespace cv::gpu; using namespace cv::gpu;
using namespace cv::gpu::cudev; using namespace cv::gpu::cudev;
namespace bgfg namespace fgd
{ {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// calcDiffHistogram // calcDiffHistogram

View File

@ -45,7 +45,7 @@
#include "opencv2/core/gpu_types.hpp" #include "opencv2/core/gpu_types.hpp"
namespace bgfg namespace fgd
{ {
struct BGPixelStat struct BGPixelStat
{ {

View File

@ -47,7 +47,7 @@
#include "opencv2/core/cuda/limits.hpp" #include "opencv2/core/cuda/limits.hpp"
namespace cv { namespace gpu { namespace cudev { namespace cv { namespace gpu { namespace cudev {
namespace bgfg_gmg namespace gmg
{ {
__constant__ int c_width; __constant__ int c_width;
__constant__ int c_height; __constant__ int c_height;

View File

@ -111,14 +111,6 @@ namespace cv { namespace gpu { namespace cudev
0.0f); 0.0f);
} }
template <class Ptr2D>
__device__ __forceinline__ void swap(Ptr2D& ptr, int x, int y, int k, int rows)
{
typename Ptr2D::elem_type val = ptr(k * rows + y, x);
ptr(k * rows + y, x) = ptr((k + 1) * rows + y, x);
ptr((k + 1) * rows + y, x) = val;
}
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
// MOG without learning // MOG without learning
@ -426,337 +418,6 @@ namespace cv { namespace gpu { namespace cudev
funcs[cn](weight, mean, dst, nmixtures, backgroundRatio, stream); funcs[cn](weight, mean, dst, nmixtures, backgroundRatio, stream);
} }
///////////////////////////////////////////////////////////////
// MOG2
__constant__ int c_nmixtures;
__constant__ float c_Tb;
__constant__ float c_TB;
__constant__ float c_Tg;
__constant__ float c_varInit;
__constant__ float c_varMin;
__constant__ float c_varMax;
__constant__ float c_tau;
__constant__ unsigned char c_shadowVal;
void loadConstants(int nmixtures, float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, unsigned char shadowVal)
{
varMin = ::fminf(varMin, varMax);
varMax = ::fmaxf(varMin, varMax);
cudaSafeCall( cudaMemcpyToSymbol(c_nmixtures, &nmixtures, sizeof(int)) );
cudaSafeCall( cudaMemcpyToSymbol(c_Tb, &Tb, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_TB, &TB, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_Tg, &Tg, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_varInit, &varInit, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_varMin, &varMin, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_varMax, &varMax, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_tau, &tau, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_shadowVal, &shadowVal, sizeof(unsigned char)) );
}
template <bool detectShadows, typename SrcT, typename WorkT>
__global__ void mog2(const PtrStepSz<SrcT> frame, PtrStepb fgmask, PtrStepb modesUsed,
PtrStepf gmm_weight, PtrStepf gmm_variance, PtrStep<WorkT> gmm_mean,
const float alphaT, const float alpha1, const float prune)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= frame.cols || y >= frame.rows)
return;
WorkT pix = cvt(frame(y, x));
//calculate distances to the modes (+ sort)
//here we need to go in descending order!!!
bool background = false; // true - the pixel classified as background
//internal:
bool fitsPDF = false; //if it remains zero a new GMM mode will be added
int nmodes = modesUsed(y, x);
int nNewModes = nmodes; //current number of modes in GMM
float totalWeight = 0.0f;
//go through all modes
for (int mode = 0; mode < nmodes; ++mode)
{
//need only weight if fit is found
float weight = alpha1 * gmm_weight(mode * frame.rows + y, x) + prune;
//fit not found yet
if (!fitsPDF)
{
//check if it belongs to some of the remaining modes
float var = gmm_variance(mode * frame.rows + y, x);
WorkT mean = gmm_mean(mode * frame.rows + y, x);
//calculate difference and distance
WorkT diff = mean - pix;
float dist2 = sqr(diff);
//background? - Tb - usually larger than Tg
if (totalWeight < c_TB && dist2 < c_Tb * var)
background = true;
//check fit
if (dist2 < c_Tg * var)
{
//belongs to the mode
fitsPDF = true;
//update distribution
//update weight
weight += alphaT;
float k = alphaT / weight;
//update mean
gmm_mean(mode * frame.rows + y, x) = mean - k * diff;
//update variance
float varnew = var + k * (dist2 - var);
//limit the variance
varnew = ::fmaxf(varnew, c_varMin);
varnew = ::fminf(varnew, c_varMax);
gmm_variance(mode * frame.rows + y, x) = varnew;
//sort
//all other weights are at the same place and
//only the matched (iModes) is higher -> just find the new place for it
for (int i = mode; i > 0; --i)
{
//check one up
if (weight < gmm_weight((i - 1) * frame.rows + y, x))
break;
//swap one up
swap(gmm_weight, x, y, i - 1, frame.rows);
swap(gmm_variance, x, y, i - 1, frame.rows);
swap(gmm_mean, x, y, i - 1, frame.rows);
}
//belongs to the mode - bFitsPDF becomes 1
}
} // !fitsPDF
//check prune
if (weight < -prune)
{
weight = 0.0;
nmodes--;
}
gmm_weight(mode * frame.rows + y, x) = weight; //update weight by the calculated value
totalWeight += weight;
}
//renormalize weights
totalWeight = 1.f / totalWeight;
for (int mode = 0; mode < nmodes; ++mode)
gmm_weight(mode * frame.rows + y, x) *= totalWeight;
nmodes = nNewModes;
//make new mode if needed and exit
if (!fitsPDF)
{
// replace the weakest or add a new one
int mode = nmodes == c_nmixtures ? c_nmixtures - 1 : nmodes++;
if (nmodes == 1)
gmm_weight(mode * frame.rows + y, x) = 1.f;
else
{
gmm_weight(mode * frame.rows + y, x) = alphaT;
// renormalize all other weights
for (int i = 0; i < nmodes - 1; ++i)
gmm_weight(i * frame.rows + y, x) *= alpha1;
}
// init
gmm_mean(mode * frame.rows + y, x) = pix;
gmm_variance(mode * frame.rows + y, x) = c_varInit;
//sort
//find the new place for it
for (int i = nmodes - 1; i > 0; --i)
{
// check one up
if (alphaT < gmm_weight((i - 1) * frame.rows + y, x))
break;
//swap one up
swap(gmm_weight, x, y, i - 1, frame.rows);
swap(gmm_variance, x, y, i - 1, frame.rows);
swap(gmm_mean, x, y, i - 1, frame.rows);
}
}
//set the number of modes
modesUsed(y, x) = nmodes;
bool isShadow = false;
if (detectShadows && !background)
{
float tWeight = 0.0f;
// check all the components marked as background:
for (int mode = 0; mode < nmodes; ++mode)
{
WorkT mean = gmm_mean(mode * frame.rows + y, x);
WorkT pix_mean = pix * mean;
float numerator = sum(pix_mean);
float denominator = sqr(mean);
// no division by zero allowed
if (denominator == 0)
break;
// if tau < a < 1 then also check the color distortion
if (numerator <= denominator && numerator >= c_tau * denominator)
{
float a = numerator / denominator;
WorkT dD = a * mean - pix;
if (sqr(dD) < c_Tb * gmm_variance(mode * frame.rows + y, x) * a * a)
{
isShadow = true;
break;
}
};
tWeight += gmm_weight(mode * frame.rows + y, x);
if (tWeight > c_TB)
break;
}
}
fgmask(y, x) = background ? 0 : isShadow ? c_shadowVal : 255;
}
template <typename SrcT, typename WorkT>
void mog2_caller(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean,
float alphaT, float prune, bool detectShadows, cudaStream_t stream)
{
dim3 block(32, 8);
dim3 grid(divUp(frame.cols, block.x), divUp(frame.rows, block.y));
const float alpha1 = 1.0f - alphaT;
if (detectShadows)
{
cudaSafeCall( cudaFuncSetCacheConfig(mog2<true, SrcT, WorkT>, cudaFuncCachePreferL1) );
mog2<true, SrcT, WorkT><<<grid, block, 0, stream>>>((PtrStepSz<SrcT>) frame, fgmask, modesUsed,
weight, variance, (PtrStepSz<WorkT>) mean,
alphaT, alpha1, prune);
}
else
{
cudaSafeCall( cudaFuncSetCacheConfig(mog2<false, SrcT, WorkT>, cudaFuncCachePreferL1) );
mog2<false, SrcT, WorkT><<<grid, block, 0, stream>>>((PtrStepSz<SrcT>) frame, fgmask, modesUsed,
weight, variance, (PtrStepSz<WorkT>) mean,
alphaT, alpha1, prune);
}
cudaSafeCall( cudaGetLastError() );
if (stream == 0)
cudaSafeCall( cudaDeviceSynchronize() );
}
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)
{
typedef void (*func_t)(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, cudaStream_t stream);
static const func_t funcs[] =
{
0, mog2_caller<uchar, float>, 0, mog2_caller<uchar3, float3>, mog2_caller<uchar4, float4>
};
funcs[cn](frame, fgmask, modesUsed, weight, variance, mean, alphaT, prune, detectShadows, stream);
}
template <typename WorkT, typename OutT>
__global__ void getBackgroundImage2(const PtrStepSzb modesUsed, const PtrStepf gmm_weight, const PtrStep<WorkT> gmm_mean, PtrStep<OutT> dst)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= modesUsed.cols || y >= modesUsed.rows)
return;
int nmodes = modesUsed(y, x);
WorkT meanVal = VecTraits<WorkT>::all(0.0f);
float totalWeight = 0.0f;
for (int mode = 0; mode < nmodes; ++mode)
{
float weight = gmm_weight(mode * modesUsed.rows + y, x);
WorkT mean = gmm_mean(mode * modesUsed.rows + y, x);
meanVal = meanVal + weight * mean;
totalWeight += weight;
if(totalWeight > c_TB)
break;
}
meanVal = meanVal * (1.f / totalWeight);
dst(y, x) = saturate_cast<OutT>(meanVal);
}
template <typename WorkT, typename OutT>
void getBackgroundImage2_caller(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream)
{
dim3 block(32, 8);
dim3 grid(divUp(modesUsed.cols, block.x), divUp(modesUsed.rows, block.y));
cudaSafeCall( cudaFuncSetCacheConfig(getBackgroundImage2<WorkT, OutT>, cudaFuncCachePreferL1) );
getBackgroundImage2<WorkT, OutT><<<grid, block, 0, stream>>>(modesUsed, weight, (PtrStepSz<WorkT>) mean, (PtrStepSz<OutT>) dst);
cudaSafeCall( cudaGetLastError() );
if (stream == 0)
cudaSafeCall( cudaDeviceSynchronize() );
}
void getBackgroundImage2_gpu(int cn, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream)
{
typedef void (*func_t)(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream);
static const func_t funcs[] =
{
0, getBackgroundImage2_caller<float, uchar>, 0, getBackgroundImage2_caller<float3, uchar3>, getBackgroundImage2_caller<float4, uchar4>
};
funcs[cn](modesUsed, weight, mean, dst, stream);
}
} }
}}} }}}

View File

@ -0,0 +1,438 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#if !defined CUDA_DISABLER
#include "opencv2/core/cuda/common.hpp"
#include "opencv2/core/cuda/vec_traits.hpp"
#include "opencv2/core/cuda/vec_math.hpp"
#include "opencv2/core/cuda/limits.hpp"
namespace cv { namespace gpu { namespace cudev
{
namespace mog2
{
///////////////////////////////////////////////////////////////
// Utility
__device__ __forceinline__ float cvt(uchar val)
{
return val;
}
__device__ __forceinline__ float3 cvt(const uchar3& val)
{
return make_float3(val.x, val.y, val.z);
}
__device__ __forceinline__ float4 cvt(const uchar4& val)
{
return make_float4(val.x, val.y, val.z, val.w);
}
__device__ __forceinline__ float sqr(float val)
{
return val * val;
}
__device__ __forceinline__ float sqr(const float3& val)
{
return val.x * val.x + val.y * val.y + val.z * val.z;
}
__device__ __forceinline__ float sqr(const float4& val)
{
return val.x * val.x + val.y * val.y + val.z * val.z;
}
__device__ __forceinline__ float sum(float val)
{
return val;
}
__device__ __forceinline__ float sum(const float3& val)
{
return val.x + val.y + val.z;
}
__device__ __forceinline__ float sum(const float4& val)
{
return val.x + val.y + val.z;
}
template <class Ptr2D>
__device__ __forceinline__ void swap(Ptr2D& ptr, int x, int y, int k, int rows)
{
typename Ptr2D::elem_type val = ptr(k * rows + y, x);
ptr(k * rows + y, x) = ptr((k + 1) * rows + y, x);
ptr((k + 1) * rows + y, x) = val;
}
///////////////////////////////////////////////////////////////
// MOG2
__constant__ int c_nmixtures;
__constant__ float c_Tb;
__constant__ float c_TB;
__constant__ float c_Tg;
__constant__ float c_varInit;
__constant__ float c_varMin;
__constant__ float c_varMax;
__constant__ float c_tau;
__constant__ unsigned char c_shadowVal;
void loadConstants(int nmixtures, float Tb, float TB, float Tg, float varInit, float varMin, float varMax, float tau, unsigned char shadowVal)
{
varMin = ::fminf(varMin, varMax);
varMax = ::fmaxf(varMin, varMax);
cudaSafeCall( cudaMemcpyToSymbol(c_nmixtures, &nmixtures, sizeof(int)) );
cudaSafeCall( cudaMemcpyToSymbol(c_Tb, &Tb, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_TB, &TB, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_Tg, &Tg, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_varInit, &varInit, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_varMin, &varMin, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_varMax, &varMax, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_tau, &tau, sizeof(float)) );
cudaSafeCall( cudaMemcpyToSymbol(c_shadowVal, &shadowVal, sizeof(unsigned char)) );
}
template <bool detectShadows, typename SrcT, typename WorkT>
__global__ void mog2(const PtrStepSz<SrcT> frame, PtrStepb fgmask, PtrStepb modesUsed,
PtrStepf gmm_weight, PtrStepf gmm_variance, PtrStep<WorkT> gmm_mean,
const float alphaT, const float alpha1, const float prune)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= frame.cols || y >= frame.rows)
return;
WorkT pix = cvt(frame(y, x));
//calculate distances to the modes (+ sort)
//here we need to go in descending order!!!
bool background = false; // true - the pixel classified as background
//internal:
bool fitsPDF = false; //if it remains zero a new GMM mode will be added
int nmodes = modesUsed(y, x);
int nNewModes = nmodes; //current number of modes in GMM
float totalWeight = 0.0f;
//go through all modes
for (int mode = 0; mode < nmodes; ++mode)
{
//need only weight if fit is found
float weight = alpha1 * gmm_weight(mode * frame.rows + y, x) + prune;
//fit not found yet
if (!fitsPDF)
{
//check if it belongs to some of the remaining modes
float var = gmm_variance(mode * frame.rows + y, x);
WorkT mean = gmm_mean(mode * frame.rows + y, x);
//calculate difference and distance
WorkT diff = mean - pix;
float dist2 = sqr(diff);
//background? - Tb - usually larger than Tg
if (totalWeight < c_TB && dist2 < c_Tb * var)
background = true;
//check fit
if (dist2 < c_Tg * var)
{
//belongs to the mode
fitsPDF = true;
//update distribution
//update weight
weight += alphaT;
float k = alphaT / weight;
//update mean
gmm_mean(mode * frame.rows + y, x) = mean - k * diff;
//update variance
float varnew = var + k * (dist2 - var);
//limit the variance
varnew = ::fmaxf(varnew, c_varMin);
varnew = ::fminf(varnew, c_varMax);
gmm_variance(mode * frame.rows + y, x) = varnew;
//sort
//all other weights are at the same place and
//only the matched (iModes) is higher -> just find the new place for it
for (int i = mode; i > 0; --i)
{
//check one up
if (weight < gmm_weight((i - 1) * frame.rows + y, x))
break;
//swap one up
swap(gmm_weight, x, y, i - 1, frame.rows);
swap(gmm_variance, x, y, i - 1, frame.rows);
swap(gmm_mean, x, y, i - 1, frame.rows);
}
//belongs to the mode - bFitsPDF becomes 1
}
} // !fitsPDF
//check prune
if (weight < -prune)
{
weight = 0.0;
nmodes--;
}
gmm_weight(mode * frame.rows + y, x) = weight; //update weight by the calculated value
totalWeight += weight;
}
//renormalize weights
totalWeight = 1.f / totalWeight;
for (int mode = 0; mode < nmodes; ++mode)
gmm_weight(mode * frame.rows + y, x) *= totalWeight;
nmodes = nNewModes;
//make new mode if needed and exit
if (!fitsPDF)
{
// replace the weakest or add a new one
int mode = nmodes == c_nmixtures ? c_nmixtures - 1 : nmodes++;
if (nmodes == 1)
gmm_weight(mode * frame.rows + y, x) = 1.f;
else
{
gmm_weight(mode * frame.rows + y, x) = alphaT;
// renormalize all other weights
for (int i = 0; i < nmodes - 1; ++i)
gmm_weight(i * frame.rows + y, x) *= alpha1;
}
// init
gmm_mean(mode * frame.rows + y, x) = pix;
gmm_variance(mode * frame.rows + y, x) = c_varInit;
//sort
//find the new place for it
for (int i = nmodes - 1; i > 0; --i)
{
// check one up
if (alphaT < gmm_weight((i - 1) * frame.rows + y, x))
break;
//swap one up
swap(gmm_weight, x, y, i - 1, frame.rows);
swap(gmm_variance, x, y, i - 1, frame.rows);
swap(gmm_mean, x, y, i - 1, frame.rows);
}
}
//set the number of modes
modesUsed(y, x) = nmodes;
bool isShadow = false;
if (detectShadows && !background)
{
float tWeight = 0.0f;
// check all the components marked as background:
for (int mode = 0; mode < nmodes; ++mode)
{
WorkT mean = gmm_mean(mode * frame.rows + y, x);
WorkT pix_mean = pix * mean;
float numerator = sum(pix_mean);
float denominator = sqr(mean);
// no division by zero allowed
if (denominator == 0)
break;
// if tau < a < 1 then also check the color distortion
if (numerator <= denominator && numerator >= c_tau * denominator)
{
float a = numerator / denominator;
WorkT dD = a * mean - pix;
if (sqr(dD) < c_Tb * gmm_variance(mode * frame.rows + y, x) * a * a)
{
isShadow = true;
break;
}
};
tWeight += gmm_weight(mode * frame.rows + y, x);
if (tWeight > c_TB)
break;
}
}
fgmask(y, x) = background ? 0 : isShadow ? c_shadowVal : 255;
}
template <typename SrcT, typename WorkT>
void mog2_caller(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean,
float alphaT, float prune, bool detectShadows, cudaStream_t stream)
{
dim3 block(32, 8);
dim3 grid(divUp(frame.cols, block.x), divUp(frame.rows, block.y));
const float alpha1 = 1.0f - alphaT;
if (detectShadows)
{
cudaSafeCall( cudaFuncSetCacheConfig(mog2<true, SrcT, WorkT>, cudaFuncCachePreferL1) );
mog2<true, SrcT, WorkT><<<grid, block, 0, stream>>>((PtrStepSz<SrcT>) frame, fgmask, modesUsed,
weight, variance, (PtrStepSz<WorkT>) mean,
alphaT, alpha1, prune);
}
else
{
cudaSafeCall( cudaFuncSetCacheConfig(mog2<false, SrcT, WorkT>, cudaFuncCachePreferL1) );
mog2<false, SrcT, WorkT><<<grid, block, 0, stream>>>((PtrStepSz<SrcT>) frame, fgmask, modesUsed,
weight, variance, (PtrStepSz<WorkT>) mean,
alphaT, alpha1, prune);
}
cudaSafeCall( cudaGetLastError() );
if (stream == 0)
cudaSafeCall( cudaDeviceSynchronize() );
}
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)
{
typedef void (*func_t)(PtrStepSzb frame, PtrStepSzb fgmask, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzf variance, PtrStepSzb mean, float alphaT, float prune, bool detectShadows, cudaStream_t stream);
static const func_t funcs[] =
{
0, mog2_caller<uchar, float>, 0, mog2_caller<uchar3, float3>, mog2_caller<uchar4, float4>
};
funcs[cn](frame, fgmask, modesUsed, weight, variance, mean, alphaT, prune, detectShadows, stream);
}
template <typename WorkT, typename OutT>
__global__ void getBackgroundImage2(const PtrStepSzb modesUsed, const PtrStepf gmm_weight, const PtrStep<WorkT> gmm_mean, PtrStep<OutT> dst)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= modesUsed.cols || y >= modesUsed.rows)
return;
int nmodes = modesUsed(y, x);
WorkT meanVal = VecTraits<WorkT>::all(0.0f);
float totalWeight = 0.0f;
for (int mode = 0; mode < nmodes; ++mode)
{
float weight = gmm_weight(mode * modesUsed.rows + y, x);
WorkT mean = gmm_mean(mode * modesUsed.rows + y, x);
meanVal = meanVal + weight * mean;
totalWeight += weight;
if(totalWeight > c_TB)
break;
}
meanVal = meanVal * (1.f / totalWeight);
dst(y, x) = saturate_cast<OutT>(meanVal);
}
template <typename WorkT, typename OutT>
void getBackgroundImage2_caller(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream)
{
dim3 block(32, 8);
dim3 grid(divUp(modesUsed.cols, block.x), divUp(modesUsed.rows, block.y));
cudaSafeCall( cudaFuncSetCacheConfig(getBackgroundImage2<WorkT, OutT>, cudaFuncCachePreferL1) );
getBackgroundImage2<WorkT, OutT><<<grid, block, 0, stream>>>(modesUsed, weight, (PtrStepSz<WorkT>) mean, (PtrStepSz<OutT>) dst);
cudaSafeCall( cudaGetLastError() );
if (stream == 0)
cudaSafeCall( cudaDeviceSynchronize() );
}
void getBackgroundImage2_gpu(int cn, PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream)
{
typedef void (*func_t)(PtrStepSzb modesUsed, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, cudaStream_t stream);
static const func_t funcs[] =
{
0, getBackgroundImage2_caller<float, uchar>, 0, getBackgroundImage2_caller<float3, uchar3>, getBackgroundImage2_caller<float4, uchar4>
};
funcs[cn](modesUsed, weight, mean, dst, stream);
}
}
}}}
#endif /* CUDA_DISABLER */

View File

@ -42,329 +42,150 @@
#include "precomp.hpp" #include "precomp.hpp"
#if !defined HAVE_CUDA || defined(CUDA_DISABLER) using namespace cv;
using namespace cv::gpu;
class cv::gpu::FGDStatModel::Impl #if !defined(HAVE_CUDA) || defined(CUDA_DISABLER) || !defined(HAVE_OPENCV_IMGPROC) || !defined(HAVE_OPENCV_GPUARITHM) || !defined(HAVE_OPENCV_GPUIMGPROC)
{
};
cv::gpu::FGDStatModel::Params::Params() { throw_no_cuda(); } cv::gpu::FGDParams::FGDParams() { throw_no_cuda(); }
cv::gpu::FGDStatModel::FGDStatModel(int) { throw_no_cuda(); } Ptr<gpu::BackgroundSubtractorFGD> cv::gpu::createBackgroundSubtractorFGD(const FGDParams&) { throw_no_cuda(); return Ptr<gpu::BackgroundSubtractorFGD>(); }
cv::gpu::FGDStatModel::FGDStatModel(const cv::gpu::GpuMat&, const Params&, int) { throw_no_cuda(); }
cv::gpu::FGDStatModel::~FGDStatModel() {}
void cv::gpu::FGDStatModel::create(const cv::gpu::GpuMat&, const Params&) { throw_no_cuda(); }
void cv::gpu::FGDStatModel::release() {}
int cv::gpu::FGDStatModel::update(const cv::gpu::GpuMat&) { throw_no_cuda(); return 0; }
#else #else
#include "cuda/fgd.hpp" #include "cuda/fgd.hpp"
#include "opencv2/imgproc/imgproc_c.h" #include "opencv2/imgproc/imgproc_c.h"
namespace /////////////////////////////////////////////////////////////////////////
{ // FGDParams
class BGPixelStat
{
public:
void create(cv::Size size, const cv::gpu::FGDStatModel::Params& params, int out_cn);
void release();
void setTrained();
operator bgfg::BGPixelStat();
private:
cv::gpu::GpuMat Pbc_;
cv::gpu::GpuMat Pbcc_;
cv::gpu::GpuMat is_trained_st_model_;
cv::gpu::GpuMat is_trained_dyn_model_;
cv::gpu::GpuMat ctable_Pv_;
cv::gpu::GpuMat ctable_Pvb_;
cv::gpu::GpuMat ctable_v_;
cv::gpu::GpuMat cctable_Pv_;
cv::gpu::GpuMat cctable_Pvb_;
cv::gpu::GpuMat cctable_v1_;
cv::gpu::GpuMat cctable_v2_;
};
void BGPixelStat::create(cv::Size size, const cv::gpu::FGDStatModel::Params& params, int out_cn)
{
cv::gpu::ensureSizeIsEnough(size, CV_32FC1, Pbc_);
Pbc_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(size, CV_32FC1, Pbcc_);
Pbcc_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(size, CV_8UC1, is_trained_st_model_);
is_trained_st_model_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(size, CV_8UC1, is_trained_dyn_model_);
is_trained_dyn_model_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2c * size.height, size.width, CV_32FC1, ctable_Pv_);
ctable_Pv_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2c * size.height, size.width, CV_32FC1, ctable_Pvb_);
ctable_Pvb_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2c * size.height, size.width, CV_8UC(out_cn), ctable_v_);
ctable_v_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_32FC1, cctable_Pv_);
cctable_Pv_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_32FC1, cctable_Pvb_);
cctable_Pvb_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_8UC(out_cn), cctable_v1_);
cctable_v1_.setTo(cv::Scalar::all(0));
cv::gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_8UC(out_cn), cctable_v2_);
cctable_v2_.setTo(cv::Scalar::all(0));
}
void BGPixelStat::release()
{
Pbc_.release();
Pbcc_.release();
is_trained_st_model_.release();
is_trained_dyn_model_.release();
ctable_Pv_.release();
ctable_Pvb_.release();
ctable_v_.release();
cctable_Pv_.release();
cctable_Pvb_.release();
cctable_v1_.release();
cctable_v2_.release();
}
void BGPixelStat::setTrained()
{
is_trained_st_model_.setTo(cv::Scalar::all(1));
is_trained_dyn_model_.setTo(cv::Scalar::all(1));
}
BGPixelStat::operator bgfg::BGPixelStat()
{
bgfg::BGPixelStat stat;
stat.rows_ = Pbc_.rows;
stat.Pbc_data_ = Pbc_.data;
stat.Pbc_step_ = Pbc_.step;
stat.Pbcc_data_ = Pbcc_.data;
stat.Pbcc_step_ = Pbcc_.step;
stat.is_trained_st_model_data_ = is_trained_st_model_.data;
stat.is_trained_st_model_step_ = is_trained_st_model_.step;
stat.is_trained_dyn_model_data_ = is_trained_dyn_model_.data;
stat.is_trained_dyn_model_step_ = is_trained_dyn_model_.step;
stat.ctable_Pv_data_ = ctable_Pv_.data;
stat.ctable_Pv_step_ = ctable_Pv_.step;
stat.ctable_Pvb_data_ = ctable_Pvb_.data;
stat.ctable_Pvb_step_ = ctable_Pvb_.step;
stat.ctable_v_data_ = ctable_v_.data;
stat.ctable_v_step_ = ctable_v_.step;
stat.cctable_Pv_data_ = cctable_Pv_.data;
stat.cctable_Pv_step_ = cctable_Pv_.step;
stat.cctable_Pvb_data_ = cctable_Pvb_.data;
stat.cctable_Pvb_step_ = cctable_Pvb_.step;
stat.cctable_v1_data_ = cctable_v1_.data;
stat.cctable_v1_step_ = cctable_v1_.step;
stat.cctable_v2_data_ = cctable_v2_.data;
stat.cctable_v2_step_ = cctable_v2_.step;
return stat;
}
}
class cv::gpu::FGDStatModel::Impl
{
public:
Impl(cv::gpu::GpuMat& background, cv::gpu::GpuMat& foreground, std::vector< std::vector<cv::Point> >& foreground_regions, int out_cn);
~Impl();
void create(const cv::gpu::GpuMat& firstFrame, const cv::gpu::FGDStatModel::Params& params);
void release();
int update(const cv::gpu::GpuMat& curFrame);
private:
Impl(const Impl&);
Impl& operator=(const Impl&);
int out_cn_;
cv::gpu::FGDStatModel::Params params_;
cv::gpu::GpuMat& background_;
cv::gpu::GpuMat& foreground_;
std::vector< std::vector<cv::Point> >& foreground_regions_;
cv::Mat h_foreground_;
cv::gpu::GpuMat prevFrame_;
cv::gpu::GpuMat Ftd_;
cv::gpu::GpuMat Fbd_;
BGPixelStat stat_;
cv::gpu::GpuMat hist_;
cv::gpu::GpuMat histBuf_;
cv::gpu::GpuMat countBuf_;
cv::gpu::GpuMat buf_;
cv::gpu::GpuMat filterBrd_;
cv::Ptr<cv::gpu::Filter> dilateFilter_;
cv::Ptr<cv::gpu::Filter> erodeFilter_;
CvMemStorage* storage_;
};
cv::gpu::FGDStatModel::Impl::Impl(cv::gpu::GpuMat& background, cv::gpu::GpuMat& foreground, std::vector< std::vector<cv::Point> >& foreground_regions, int out_cn) :
out_cn_(out_cn), background_(background), foreground_(foreground), foreground_regions_(foreground_regions)
{
CV_Assert( out_cn_ == 3 || out_cn_ == 4 );
storage_ = cvCreateMemStorage();
CV_Assert( storage_ != 0 );
}
cv::gpu::FGDStatModel::Impl::~Impl()
{
cvReleaseMemStorage(&storage_);
}
namespace namespace
{ {
void copyChannels(const cv::gpu::GpuMat& src, cv::gpu::GpuMat& dst, int dst_cn = -1) // Default parameters of foreground detection algorithm:
const int BGFG_FGD_LC = 128;
const int BGFG_FGD_N1C = 15;
const int BGFG_FGD_N2C = 25;
const int BGFG_FGD_LCC = 64;
const int BGFG_FGD_N1CC = 25;
const int BGFG_FGD_N2CC = 40;
// Background reference image update parameter:
const float BGFG_FGD_ALPHA_1 = 0.1f;
// stat model update parameter
// 0.002f ~ 1K frame(~45sec), 0.005 ~ 18sec (if 25fps and absolutely static BG)
const float BGFG_FGD_ALPHA_2 = 0.005f;
// start value for alpha parameter (to fast initiate statistic model)
const float BGFG_FGD_ALPHA_3 = 0.1f;
const float BGFG_FGD_DELTA = 2.0f;
const float BGFG_FGD_T = 0.9f;
const float BGFG_FGD_MINAREA= 15.0f;
}
cv::gpu::FGDParams::FGDParams()
{
Lc = BGFG_FGD_LC;
N1c = BGFG_FGD_N1C;
N2c = BGFG_FGD_N2C;
Lcc = BGFG_FGD_LCC;
N1cc = BGFG_FGD_N1CC;
N2cc = BGFG_FGD_N2CC;
delta = BGFG_FGD_DELTA;
alpha1 = BGFG_FGD_ALPHA_1;
alpha2 = BGFG_FGD_ALPHA_2;
alpha3 = BGFG_FGD_ALPHA_3;
T = BGFG_FGD_T;
minArea = BGFG_FGD_MINAREA;
is_obj_without_holes = true;
perform_morphing = 1;
}
/////////////////////////////////////////////////////////////////////////
// copyChannels
namespace
{
void copyChannels(const GpuMat& src, GpuMat& dst, int dst_cn = -1)
{ {
const int src_cn = src.channels(); const int src_cn = src.channels();
if (dst_cn < 0) if (dst_cn < 0)
dst_cn = src_cn; dst_cn = src_cn;
cv::gpu::ensureSizeIsEnough(src.size(), CV_MAKE_TYPE(src.depth(), dst_cn), dst); gpu::ensureSizeIsEnough(src.size(), CV_MAKE_TYPE(src.depth(), dst_cn), dst);
if (src_cn == dst_cn) if (src_cn == dst_cn)
{
src.copyTo(dst); src.copyTo(dst);
}
else else
{ {
static const int cvt_codes[4][4] = static const int cvt_codes[4][4] =
{ {
{-1, -1, cv::COLOR_GRAY2BGR, cv::COLOR_GRAY2BGRA}, {-1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA},
{-1, -1, -1, -1}, {-1, -1, -1, -1},
{cv::COLOR_BGR2GRAY, -1, -1, cv::COLOR_BGR2BGRA}, {COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA},
{cv::COLOR_BGRA2GRAY, -1, cv::COLOR_BGRA2BGR, -1} {COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1}
}; };
const int cvt_code = cvt_codes[src_cn - 1][dst_cn - 1]; const int cvt_code = cvt_codes[src_cn - 1][dst_cn - 1];
CV_DbgAssert( cvt_code >= 0 ); CV_DbgAssert( cvt_code >= 0 );
cv::gpu::cvtColor(src, dst, cvt_code, dst_cn); gpu::cvtColor(src, dst, cvt_code, dst_cn);
} }
} }
} }
void cv::gpu::FGDStatModel::Impl::create(const cv::gpu::GpuMat& firstFrame, const cv::gpu::FGDStatModel::Params& params)
{
CV_Assert(firstFrame.type() == CV_8UC3 || firstFrame.type() == CV_8UC4);
params_ = params;
cv::gpu::ensureSizeIsEnough(firstFrame.size(), CV_8UC1, foreground_);
copyChannels(firstFrame, background_, out_cn_);
copyChannels(firstFrame, prevFrame_);
cv::gpu::ensureSizeIsEnough(firstFrame.size(), CV_8UC1, Ftd_);
cv::gpu::ensureSizeIsEnough(firstFrame.size(), CV_8UC1, Fbd_);
stat_.create(firstFrame.size(), params_, out_cn_);
bgfg::setBGPixelStat(stat_);
if (params_.perform_morphing > 0)
{
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1 + params_.perform_morphing * 2, 1 + params_.perform_morphing * 2));
cv::Point anchor(params_.perform_morphing, params_.perform_morphing);
dilateFilter_ = cv::gpu::createMorphologyFilter(cv::MORPH_DILATE, CV_8UC1, kernel, anchor);
erodeFilter_ = cv::gpu::createMorphologyFilter(cv::MORPH_ERODE, CV_8UC1, kernel, anchor);
}
}
void cv::gpu::FGDStatModel::Impl::release()
{
background_.release();
foreground_.release();
prevFrame_.release();
Ftd_.release();
Fbd_.release();
stat_.release();
hist_.release();
histBuf_.release();
countBuf_.release();
buf_.release();
filterBrd_.release();
}
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// changeDetection // changeDetection
namespace namespace
{ {
void calcDiffHistogram(const cv::gpu::GpuMat& prevFrame, const cv::gpu::GpuMat& curFrame, cv::gpu::GpuMat& hist, cv::gpu::GpuMat& histBuf) void calcDiffHistogram(const GpuMat& prevFrame, const GpuMat& curFrame, GpuMat& hist, GpuMat& histBuf)
{ {
typedef void (*func_t)(cv::gpu::PtrStepSzb prevFrame, cv::gpu::PtrStepSzb curFrame, unsigned int* hist0, unsigned int* hist1, unsigned int* hist2, unsigned int* partialBuf0, unsigned int* partialBuf1, unsigned int* partialBuf2, bool cc20, cudaStream_t stream); typedef void (*func_t)(PtrStepSzb prevFrame, PtrStepSzb curFrame,
unsigned int* hist0, unsigned int* hist1, unsigned int* hist2,
unsigned int* partialBuf0, unsigned int* partialBuf1, unsigned int* partialBuf2,
bool cc20, cudaStream_t stream);
static const func_t funcs[4][4] = static const func_t funcs[4][4] =
{ {
{0,0,0,0}, {0,0,0,0},
{0,0,0,0}, {0,0,0,0},
{0,0,bgfg::calcDiffHistogram_gpu<uchar3, uchar3>,bgfg::calcDiffHistogram_gpu<uchar3, uchar4>}, {0,0,fgd::calcDiffHistogram_gpu<uchar3, uchar3>,fgd::calcDiffHistogram_gpu<uchar3, uchar4>},
{0,0,bgfg::calcDiffHistogram_gpu<uchar4, uchar3>,bgfg::calcDiffHistogram_gpu<uchar4, uchar4>} {0,0,fgd::calcDiffHistogram_gpu<uchar4, uchar3>,fgd::calcDiffHistogram_gpu<uchar4, uchar4>}
}; };
hist.create(3, 256, CV_32SC1); hist.create(3, 256, CV_32SC1);
histBuf.create(3, bgfg::PARTIAL_HISTOGRAM_COUNT * bgfg::HISTOGRAM_BIN_COUNT, CV_32SC1); histBuf.create(3, fgd::PARTIAL_HISTOGRAM_COUNT * fgd::HISTOGRAM_BIN_COUNT, CV_32SC1);
funcs[prevFrame.channels() - 1][curFrame.channels() - 1]( funcs[prevFrame.channels() - 1][curFrame.channels() - 1](
prevFrame, curFrame, prevFrame, curFrame,
hist.ptr<unsigned int>(0), hist.ptr<unsigned int>(1), hist.ptr<unsigned int>(2), hist.ptr<unsigned int>(0), hist.ptr<unsigned int>(1), hist.ptr<unsigned int>(2),
histBuf.ptr<unsigned int>(0), histBuf.ptr<unsigned int>(1), histBuf.ptr<unsigned int>(2), histBuf.ptr<unsigned int>(0), histBuf.ptr<unsigned int>(1), histBuf.ptr<unsigned int>(2),
cv::gpu::deviceSupports(cv::gpu::FEATURE_SET_COMPUTE_20), 0); deviceSupports(FEATURE_SET_COMPUTE_20), 0);
} }
void calcRelativeVariance(unsigned int hist[3 * 256], double relativeVariance[3][bgfg::HISTOGRAM_BIN_COUNT]) void calcRelativeVariance(unsigned int hist[3 * 256], double relativeVariance[3][fgd::HISTOGRAM_BIN_COUNT])
{ {
std::memset(relativeVariance, 0, 3 * bgfg::HISTOGRAM_BIN_COUNT * sizeof(double)); std::memset(relativeVariance, 0, 3 * fgd::HISTOGRAM_BIN_COUNT * sizeof(double));
for (int thres = bgfg::HISTOGRAM_BIN_COUNT - 2; thres >= 0; --thres) for (int thres = fgd::HISTOGRAM_BIN_COUNT - 2; thres >= 0; --thres)
{ {
cv::Vec3d sum(0.0, 0.0, 0.0); Vec3d sum(0.0, 0.0, 0.0);
cv::Vec3d sqsum(0.0, 0.0, 0.0); Vec3d sqsum(0.0, 0.0, 0.0);
cv::Vec3i count(0, 0, 0); Vec3i count(0, 0, 0);
for (int j = thres; j < bgfg::HISTOGRAM_BIN_COUNT; ++j) for (int j = thres; j < fgd::HISTOGRAM_BIN_COUNT; ++j)
{ {
sum[0] += static_cast<double>(j) * hist[j]; sum[0] += static_cast<double>(j) * hist[j];
sqsum[0] += static_cast<double>(j * j) * hist[j]; sqsum[0] += static_cast<double>(j * j) * hist[j];
@ -383,7 +204,7 @@ namespace
count[1] = std::max(count[1], 1); count[1] = std::max(count[1], 1);
count[2] = std::max(count[2], 1); count[2] = std::max(count[2], 1);
cv::Vec3d my( Vec3d my(
sum[0] / count[0], sum[0] / count[0],
sum[1] / count[1], sum[1] / count[1],
sum[2] / count[2] sum[2] / count[2]
@ -395,37 +216,39 @@ namespace
} }
} }
void calcDiffThreshMask(const cv::gpu::GpuMat& prevFrame, const cv::gpu::GpuMat& curFrame, cv::Vec3d bestThres, cv::gpu::GpuMat& changeMask) void calcDiffThreshMask(const GpuMat& prevFrame, const GpuMat& curFrame, Vec3d bestThres, GpuMat& changeMask)
{ {
typedef void (*func_t)(cv::gpu::PtrStepSzb prevFrame, cv::gpu::PtrStepSzb curFrame, uchar3 bestThres, cv::gpu::PtrStepSzb changeMask, cudaStream_t stream); typedef void (*func_t)(PtrStepSzb prevFrame, PtrStepSzb curFrame, uchar3 bestThres, PtrStepSzb changeMask, cudaStream_t stream);
static const func_t funcs[4][4] = static const func_t funcs[4][4] =
{ {
{0,0,0,0}, {0,0,0,0},
{0,0,0,0}, {0,0,0,0},
{0,0,bgfg::calcDiffThreshMask_gpu<uchar3, uchar3>,bgfg::calcDiffThreshMask_gpu<uchar3, uchar4>}, {0,0,fgd::calcDiffThreshMask_gpu<uchar3, uchar3>,fgd::calcDiffThreshMask_gpu<uchar3, uchar4>},
{0,0,bgfg::calcDiffThreshMask_gpu<uchar4, uchar3>,bgfg::calcDiffThreshMask_gpu<uchar4, uchar4>} {0,0,fgd::calcDiffThreshMask_gpu<uchar4, uchar3>,fgd::calcDiffThreshMask_gpu<uchar4, uchar4>}
}; };
changeMask.setTo(cv::Scalar::all(0)); changeMask.setTo(Scalar::all(0));
funcs[prevFrame.channels() - 1][curFrame.channels() - 1](prevFrame, curFrame, make_uchar3((uchar)bestThres[0], (uchar)bestThres[1], (uchar)bestThres[2]), changeMask, 0); funcs[prevFrame.channels() - 1][curFrame.channels() - 1](prevFrame, curFrame,
make_uchar3((uchar)bestThres[0], (uchar)bestThres[1], (uchar)bestThres[2]),
changeMask, 0);
} }
// performs change detection for Foreground detection algorithm // performs change detection for Foreground detection algorithm
void changeDetection(const cv::gpu::GpuMat& prevFrame, const cv::gpu::GpuMat& curFrame, cv::gpu::GpuMat& changeMask, cv::gpu::GpuMat& hist, cv::gpu::GpuMat& histBuf) void changeDetection(const GpuMat& prevFrame, const GpuMat& curFrame, GpuMat& changeMask, GpuMat& hist, GpuMat& histBuf)
{ {
calcDiffHistogram(prevFrame, curFrame, hist, histBuf); calcDiffHistogram(prevFrame, curFrame, hist, histBuf);
unsigned int histData[3 * 256]; unsigned int histData[3 * 256];
cv::Mat h_hist(3, 256, CV_32SC1, histData); Mat h_hist(3, 256, CV_32SC1, histData);
hist.download(h_hist); hist.download(h_hist);
double relativeVariance[3][bgfg::HISTOGRAM_BIN_COUNT]; double relativeVariance[3][fgd::HISTOGRAM_BIN_COUNT];
calcRelativeVariance(histData, relativeVariance); calcRelativeVariance(histData, relativeVariance);
// Find maximum: // Find maximum:
cv::Vec3d bestThres(10.0, 10.0, 10.0); Vec3d bestThres(10.0, 10.0, 10.0);
for (int i = 0; i < bgfg::HISTOGRAM_BIN_COUNT; ++i) for (int i = 0; i < fgd::HISTOGRAM_BIN_COUNT; ++i)
{ {
bestThres[0] = std::max(bestThres[0], relativeVariance[0][i]); bestThres[0] = std::max(bestThres[0], relativeVariance[0][i]);
bestThres[1] = std::max(bestThres[1], relativeVariance[1][i]); bestThres[1] = std::max(bestThres[1], relativeVariance[1][i]);
@ -441,12 +264,12 @@ namespace
namespace namespace
{ {
int bgfgClassification(const cv::gpu::GpuMat& prevFrame, const cv::gpu::GpuMat& curFrame, int bgfgClassification(const GpuMat& prevFrame, const GpuMat& curFrame,
const cv::gpu::GpuMat& Ftd, const cv::gpu::GpuMat& Fbd, const GpuMat& Ftd, const GpuMat& Fbd,
cv::gpu::GpuMat& foreground, cv::gpu::GpuMat& countBuf, GpuMat& foreground, GpuMat& countBuf,
const cv::gpu::FGDStatModel::Params& params, int out_cn) const FGDParams& params, int out_cn)
{ {
typedef void (*func_t)(cv::gpu::PtrStepSzb prevFrame, cv::gpu::PtrStepSzb curFrame, cv::gpu::PtrStepSzb Ftd, cv::gpu::PtrStepSzb Fbd, cv::gpu::PtrStepSzb foreground, typedef void (*func_t)(PtrStepSzb prevFrame, PtrStepSzb curFrame, PtrStepSzb Ftd, PtrStepSzb Fbd, PtrStepSzb foreground,
int deltaC, int deltaCC, float alpha2, int N1c, int N1cc, cudaStream_t stream); int deltaC, int deltaCC, float alpha2, int N1c, int N1cc, cudaStream_t stream);
static const func_t funcs[4][4][4] = static const func_t funcs[4][4][4] =
{ {
@ -458,24 +281,26 @@ namespace
}, },
{ {
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
{0,0,bgfg::bgfgClassification_gpu<uchar3, uchar3, uchar3>,bgfg::bgfgClassification_gpu<uchar3, uchar3, uchar4>}, {0,0,fgd::bgfgClassification_gpu<uchar3, uchar3, uchar3>,fgd::bgfgClassification_gpu<uchar3, uchar3, uchar4>},
{0,0,bgfg::bgfgClassification_gpu<uchar3, uchar4, uchar3>,bgfg::bgfgClassification_gpu<uchar3, uchar4, uchar4>} {0,0,fgd::bgfgClassification_gpu<uchar3, uchar4, uchar3>,fgd::bgfgClassification_gpu<uchar3, uchar4, uchar4>}
}, },
{ {
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
{0,0,bgfg::bgfgClassification_gpu<uchar4, uchar3, uchar3>,bgfg::bgfgClassification_gpu<uchar4, uchar3, uchar4>}, {0,0,fgd::bgfgClassification_gpu<uchar4, uchar3, uchar3>,fgd::bgfgClassification_gpu<uchar4, uchar3, uchar4>},
{0,0,bgfg::bgfgClassification_gpu<uchar4, uchar4, uchar3>,bgfg::bgfgClassification_gpu<uchar4, uchar4, uchar4>} {0,0,fgd::bgfgClassification_gpu<uchar4, uchar4, uchar3>,fgd::bgfgClassification_gpu<uchar4, uchar4, uchar4>}
} }
}; };
const int deltaC = cvRound(params.delta * 256 / params.Lc); const int deltaC = cvRound(params.delta * 256 / params.Lc);
const int deltaCC = cvRound(params.delta * 256 / params.Lcc); const int deltaCC = cvRound(params.delta * 256 / params.Lcc);
funcs[prevFrame.channels() - 1][curFrame.channels() - 1][out_cn - 1](prevFrame, curFrame, Ftd, Fbd, foreground, deltaC, deltaCC, params.alpha2, params.N1c, params.N1cc, 0); funcs[prevFrame.channels() - 1][curFrame.channels() - 1][out_cn - 1](prevFrame, curFrame, Ftd, Fbd, foreground,
deltaC, deltaCC, params.alpha2,
params.N1c, params.N1cc, 0);
int count = cv::gpu::countNonZero(foreground, countBuf); int count = gpu::countNonZero(foreground, countBuf);
cv::gpu::multiply(foreground, cv::Scalar::all(255), foreground); gpu::multiply(foreground, Scalar::all(255), foreground);
return count; return count;
} }
@ -484,22 +309,24 @@ namespace
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// smoothForeground // smoothForeground
#ifdef HAVE_OPENCV_GPUFILTERS
namespace namespace
{ {
void morphology(const cv::gpu::GpuMat& src, cv::gpu::GpuMat& dst, cv::gpu::GpuMat& filterBrd, int brd, cv::Ptr<cv::gpu::Filter>& filter, cv::Scalar brdVal) void morphology(const GpuMat& src, GpuMat& dst, GpuMat& filterBrd, int brd, Ptr<gpu::Filter>& filter, Scalar brdVal)
{ {
cv::gpu::copyMakeBorder(src, filterBrd, brd, brd, brd, brd, cv::BORDER_CONSTANT, brdVal); gpu::copyMakeBorder(src, filterBrd, brd, brd, brd, brd, BORDER_CONSTANT, brdVal);
filter->apply(filterBrd(cv::Rect(brd, brd, src.cols, src.rows)), dst); filter->apply(filterBrd(Rect(brd, brd, src.cols, src.rows)), dst);
} }
void smoothForeground(cv::gpu::GpuMat& foreground, cv::gpu::GpuMat& filterBrd, cv::gpu::GpuMat& buf, void smoothForeground(GpuMat& foreground, GpuMat& filterBrd, GpuMat& buf,
cv::Ptr<cv::gpu::Filter>& erodeFilter, cv::Ptr<cv::gpu::Filter>& dilateFilter, Ptr<gpu::Filter>& erodeFilter, Ptr<gpu::Filter>& dilateFilter,
const cv::gpu::FGDStatModel::Params& params) const FGDParams& params)
{ {
const int brd = params.perform_morphing; const int brd = params.perform_morphing;
const cv::Scalar erodeBrdVal = cv::Scalar::all(UCHAR_MAX); const Scalar erodeBrdVal = Scalar::all(UCHAR_MAX);
const cv::Scalar dilateBrdVal = cv::Scalar::all(0); const Scalar dilateBrdVal = Scalar::all(0);
// MORPH_OPEN // MORPH_OPEN
morphology(foreground, buf, filterBrd, brd, erodeFilter, erodeBrdVal); morphology(foreground, buf, filterBrd, brd, erodeFilter, erodeBrdVal);
@ -511,33 +338,35 @@ namespace
} }
} }
#endif
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// findForegroundRegions // findForegroundRegions
namespace namespace
{ {
void seqToContours(CvSeq* _ccontours, CvMemStorage* storage, cv::OutputArrayOfArrays _contours) void seqToContours(CvSeq* _ccontours, CvMemStorage* storage, OutputArrayOfArrays _contours)
{ {
cv::Seq<CvSeq*> all_contours(cvTreeToNodeSeq(_ccontours, sizeof(CvSeq), storage)); Seq<CvSeq*> all_contours(cvTreeToNodeSeq(_ccontours, sizeof(CvSeq), storage));
size_t total = all_contours.size(); size_t total = all_contours.size();
_contours.create((int) total, 1, 0, -1, true); _contours.create((int) total, 1, 0, -1, true);
cv::SeqIterator<CvSeq*> it = all_contours.begin(); SeqIterator<CvSeq*> it = all_contours.begin();
for (size_t i = 0; i < total; ++i, ++it) for (size_t i = 0; i < total; ++i, ++it)
{ {
CvSeq* c = *it; CvSeq* c = *it;
((CvContour*)c)->color = (int)i; ((CvContour*)c)->color = (int)i;
_contours.create((int)c->total, 1, CV_32SC2, (int)i, true); _contours.create((int)c->total, 1, CV_32SC2, (int)i, true);
cv::Mat ci = _contours.getMat((int)i); Mat ci = _contours.getMat((int)i);
CV_Assert( ci.isContinuous() ); CV_Assert( ci.isContinuous() );
cvCvtSeqToArray(c, ci.data); cvCvtSeqToArray(c, ci.data);
} }
} }
int findForegroundRegions(cv::gpu::GpuMat& d_foreground, cv::Mat& h_foreground, std::vector< std::vector<cv::Point> >& foreground_regions, int findForegroundRegions(GpuMat& d_foreground, Mat& h_foreground, std::vector< std::vector<Point> >& foreground_regions,
CvMemStorage* storage, const cv::gpu::FGDStatModel::Params& params) CvMemStorage* storage, const FGDParams& params)
{ {
int region_count = 0; int region_count = 0;
@ -581,7 +410,7 @@ namespace
seqToContours(first_seq, storage, foreground_regions); seqToContours(first_seq, storage, foreground_regions);
h_foreground.setTo(0); h_foreground.setTo(0);
cv::drawContours(h_foreground, foreground_regions, -1, cv::Scalar::all(255), -1); drawContours(h_foreground, foreground_regions, -1, Scalar::all(255), -1);
d_foreground.upload(h_foreground); d_foreground.upload(h_foreground);
@ -594,12 +423,12 @@ namespace
namespace namespace
{ {
void updateBackgroundModel(const cv::gpu::GpuMat& prevFrame, const cv::gpu::GpuMat& curFrame, const cv::gpu::GpuMat& Ftd, const cv::gpu::GpuMat& Fbd, void updateBackgroundModel(const GpuMat& prevFrame, const GpuMat& curFrame, const GpuMat& Ftd, const GpuMat& Fbd,
const cv::gpu::GpuMat& foreground, cv::gpu::GpuMat& background, const GpuMat& foreground, GpuMat& background,
const cv::gpu::FGDStatModel::Params& params) const FGDParams& params)
{ {
typedef void (*func_t)(cv::gpu::PtrStepSzb prevFrame, cv::gpu::PtrStepSzb curFrame, cv::gpu::PtrStepSzb Ftd, cv::gpu::PtrStepSzb Fbd, typedef void (*func_t)(PtrStepSzb prevFrame, PtrStepSzb curFrame, PtrStepSzb Ftd, PtrStepSzb Fbd,
cv::gpu::PtrStepSzb foreground, cv::gpu::PtrStepSzb background, PtrStepSzb foreground, PtrStepSzb background,
int deltaC, int deltaCC, float alpha1, float alpha2, float alpha3, int N1c, int N1cc, int N2c, int N2cc, float T, cudaStream_t stream); int deltaC, int deltaCC, float alpha1, float alpha2, float alpha3, int N1c, int N1cc, int N2c, int N2cc, float T, cudaStream_t stream);
static const func_t funcs[4][4][4] = static const func_t funcs[4][4][4] =
{ {
@ -611,13 +440,13 @@ namespace
}, },
{ {
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
{0,0,bgfg::updateBackgroundModel_gpu<uchar3, uchar3, uchar3>,bgfg::updateBackgroundModel_gpu<uchar3, uchar3, uchar4>}, {0,0,fgd::updateBackgroundModel_gpu<uchar3, uchar3, uchar3>,fgd::updateBackgroundModel_gpu<uchar3, uchar3, uchar4>},
{0,0,bgfg::updateBackgroundModel_gpu<uchar3, uchar4, uchar3>,bgfg::updateBackgroundModel_gpu<uchar3, uchar4, uchar4>} {0,0,fgd::updateBackgroundModel_gpu<uchar3, uchar4, uchar3>,fgd::updateBackgroundModel_gpu<uchar3, uchar4, uchar4>}
}, },
{ {
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
{0,0,bgfg::updateBackgroundModel_gpu<uchar4, uchar3, uchar3>,bgfg::updateBackgroundModel_gpu<uchar4, uchar3, uchar4>}, {0,0,fgd::updateBackgroundModel_gpu<uchar4, uchar3, uchar3>,fgd::updateBackgroundModel_gpu<uchar4, uchar3, uchar4>},
{0,0,bgfg::updateBackgroundModel_gpu<uchar4, uchar4, uchar3>,bgfg::updateBackgroundModel_gpu<uchar4, uchar4, uchar4>} {0,0,fgd::updateBackgroundModel_gpu<uchar4, uchar4, uchar3>,fgd::updateBackgroundModel_gpu<uchar4, uchar4, uchar4>}
} }
}; };
@ -626,34 +455,209 @@ namespace
funcs[prevFrame.channels() - 1][curFrame.channels() - 1][background.channels() - 1]( funcs[prevFrame.channels() - 1][curFrame.channels() - 1][background.channels() - 1](
prevFrame, curFrame, Ftd, Fbd, foreground, background, prevFrame, curFrame, Ftd, Fbd, foreground, background,
deltaC, deltaCC, params.alpha1, params.alpha2, params.alpha3, params.N1c, params.N1cc, params.N2c, params.N2cc, params.T, deltaC, deltaCC, params.alpha1, params.alpha2, params.alpha3,
params.N1c, params.N1cc, params.N2c, params.N2cc, params.T,
0); 0);
} }
} }
/////////////////////////////////////////////////////////////////////////
// Impl::update
int cv::gpu::FGDStatModel::Impl::update(const cv::gpu::GpuMat& curFrame) namespace
{ {
class BGPixelStat
{
public:
void create(Size size, const FGDParams& params);
void setTrained();
operator fgd::BGPixelStat();
private:
GpuMat Pbc_;
GpuMat Pbcc_;
GpuMat is_trained_st_model_;
GpuMat is_trained_dyn_model_;
GpuMat ctable_Pv_;
GpuMat ctable_Pvb_;
GpuMat ctable_v_;
GpuMat cctable_Pv_;
GpuMat cctable_Pvb_;
GpuMat cctable_v1_;
GpuMat cctable_v2_;
};
void BGPixelStat::create(Size size, const FGDParams& params)
{
gpu::ensureSizeIsEnough(size, CV_32FC1, Pbc_);
Pbc_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(size, CV_32FC1, Pbcc_);
Pbcc_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(size, CV_8UC1, is_trained_st_model_);
is_trained_st_model_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(size, CV_8UC1, is_trained_dyn_model_);
is_trained_dyn_model_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2c * size.height, size.width, CV_32FC1, ctable_Pv_);
ctable_Pv_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2c * size.height, size.width, CV_32FC1, ctable_Pvb_);
ctable_Pvb_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2c * size.height, size.width, CV_8UC4, ctable_v_);
ctable_v_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_32FC1, cctable_Pv_);
cctable_Pv_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_32FC1, cctable_Pvb_);
cctable_Pvb_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_8UC4, cctable_v1_);
cctable_v1_.setTo(Scalar::all(0));
gpu::ensureSizeIsEnough(params.N2cc * size.height, size.width, CV_8UC4, cctable_v2_);
cctable_v2_.setTo(Scalar::all(0));
}
void BGPixelStat::setTrained()
{
is_trained_st_model_.setTo(Scalar::all(1));
is_trained_dyn_model_.setTo(Scalar::all(1));
}
BGPixelStat::operator fgd::BGPixelStat()
{
fgd::BGPixelStat stat;
stat.rows_ = Pbc_.rows;
stat.Pbc_data_ = Pbc_.data;
stat.Pbc_step_ = Pbc_.step;
stat.Pbcc_data_ = Pbcc_.data;
stat.Pbcc_step_ = Pbcc_.step;
stat.is_trained_st_model_data_ = is_trained_st_model_.data;
stat.is_trained_st_model_step_ = is_trained_st_model_.step;
stat.is_trained_dyn_model_data_ = is_trained_dyn_model_.data;
stat.is_trained_dyn_model_step_ = is_trained_dyn_model_.step;
stat.ctable_Pv_data_ = ctable_Pv_.data;
stat.ctable_Pv_step_ = ctable_Pv_.step;
stat.ctable_Pvb_data_ = ctable_Pvb_.data;
stat.ctable_Pvb_step_ = ctable_Pvb_.step;
stat.ctable_v_data_ = ctable_v_.data;
stat.ctable_v_step_ = ctable_v_.step;
stat.cctable_Pv_data_ = cctable_Pv_.data;
stat.cctable_Pv_step_ = cctable_Pv_.step;
stat.cctable_Pvb_data_ = cctable_Pvb_.data;
stat.cctable_Pvb_step_ = cctable_Pvb_.step;
stat.cctable_v1_data_ = cctable_v1_.data;
stat.cctable_v1_step_ = cctable_v1_.step;
stat.cctable_v2_data_ = cctable_v2_.data;
stat.cctable_v2_step_ = cctable_v2_.step;
return stat;
}
class FGDImpl : public gpu::BackgroundSubtractorFGD
{
public:
explicit FGDImpl(const FGDParams& params);
~FGDImpl();
void apply(InputArray image, OutputArray fgmask, double learningRate=-1);
void getBackgroundImage(OutputArray backgroundImage) const;
void getForegroundRegions(OutputArrayOfArrays foreground_regions);
private:
void initialize(const GpuMat& firstFrame);
FGDParams params_;
Size frameSize_;
GpuMat background_;
GpuMat foreground_;
std::vector< std::vector<Point> > foreground_regions_;
Mat h_foreground_;
GpuMat prevFrame_;
GpuMat Ftd_;
GpuMat Fbd_;
BGPixelStat stat_;
GpuMat hist_;
GpuMat histBuf_;
GpuMat countBuf_;
GpuMat buf_;
GpuMat filterBrd_;
#ifdef HAVE_OPENCV_GPUFILTERS
Ptr<gpu::Filter> dilateFilter_;
Ptr<gpu::Filter> erodeFilter_;
#endif
CvMemStorage* storage_;
};
FGDImpl::FGDImpl(const FGDParams& params) : params_(params), frameSize_(0, 0)
{
storage_ = cvCreateMemStorage();
CV_Assert( storage_ != 0 );
}
FGDImpl::~FGDImpl()
{
cvReleaseMemStorage(&storage_);
}
void FGDImpl::apply(InputArray _frame, OutputArray fgmask, double)
{
GpuMat curFrame = _frame.getGpuMat();
if (curFrame.size() != frameSize_)
{
initialize(curFrame);
return;
}
CV_Assert( curFrame.type() == CV_8UC3 || curFrame.type() == CV_8UC4 ); CV_Assert( curFrame.type() == CV_8UC3 || curFrame.type() == CV_8UC4 );
CV_Assert( curFrame.size() == prevFrame_.size() ); CV_Assert( curFrame.size() == prevFrame_.size() );
cvClearMemStorage(storage_); cvClearMemStorage(storage_);
foreground_regions_.clear(); foreground_regions_.clear();
foreground_.setTo(cv::Scalar::all(0)); foreground_.setTo(Scalar::all(0));
changeDetection(prevFrame_, curFrame, Ftd_, hist_, histBuf_); changeDetection(prevFrame_, curFrame, Ftd_, hist_, histBuf_);
changeDetection(background_, curFrame, Fbd_, hist_, histBuf_); changeDetection(background_, curFrame, Fbd_, hist_, histBuf_);
int FG_pixels_count = bgfgClassification(prevFrame_, curFrame, Ftd_, Fbd_, foreground_, countBuf_, params_, out_cn_); int FG_pixels_count = bgfgClassification(prevFrame_, curFrame, Ftd_, Fbd_, foreground_, countBuf_, params_, 4);
#ifdef HAVE_OPENCV_GPUFILTERS
if (params_.perform_morphing > 0) if (params_.perform_morphing > 0)
smoothForeground(foreground_, filterBrd_, buf_, erodeFilter_, dilateFilter_, params_); smoothForeground(foreground_, filterBrd_, buf_, erodeFilter_, dilateFilter_, params_);
#endif
int region_count = 0;
if (params_.minArea > 0 || params_.is_obj_without_holes) if (params_.minArea > 0 || params_.is_obj_without_holes)
region_count = findForegroundRegions(foreground_, h_foreground_, foreground_regions_, storage_, params_); findForegroundRegions(foreground_, h_foreground_, foreground_regions_, storage_, params_);
// Check ALL BG update condition: // Check ALL BG update condition:
const double BGFG_FGD_BG_UPDATE_TRESH = 0.5; const double BGFG_FGD_BG_UPDATE_TRESH = 0.5;
@ -662,90 +666,66 @@ int cv::gpu::FGDStatModel::Impl::update(const cv::gpu::GpuMat& curFrame)
updateBackgroundModel(prevFrame_, curFrame, Ftd_, Fbd_, foreground_, background_, params_); updateBackgroundModel(prevFrame_, curFrame, Ftd_, Fbd_, foreground_, background_, params_);
copyChannels(curFrame, prevFrame_); copyChannels(curFrame, prevFrame_, 4);
return region_count; foreground_.copyTo(fgmask);
} }
namespace void FGDImpl::getBackgroundImage(OutputArray backgroundImage) const
{ {
// Default parameters of foreground detection algorithm: gpu::cvtColor(background_, backgroundImage, COLOR_BGRA2BGR);
const int BGFG_FGD_LC = 128;
const int BGFG_FGD_N1C = 15;
const int BGFG_FGD_N2C = 25;
const int BGFG_FGD_LCC = 64;
const int BGFG_FGD_N1CC = 25;
const int BGFG_FGD_N2CC = 40;
// Background reference image update parameter:
const float BGFG_FGD_ALPHA_1 = 0.1f;
// stat model update parameter
// 0.002f ~ 1K frame(~45sec), 0.005 ~ 18sec (if 25fps and absolutely static BG)
const float BGFG_FGD_ALPHA_2 = 0.005f;
// start value for alpha parameter (to fast initiate statistic model)
const float BGFG_FGD_ALPHA_3 = 0.1f;
const float BGFG_FGD_DELTA = 2.0f;
const float BGFG_FGD_T = 0.9f;
const float BGFG_FGD_MINAREA= 15.0f;
} }
cv::gpu::FGDStatModel::Params::Params() void FGDImpl::getForegroundRegions(OutputArrayOfArrays dst)
{ {
Lc = BGFG_FGD_LC; size_t total = foreground_regions_.size();
N1c = BGFG_FGD_N1C;
N2c = BGFG_FGD_N2C;
Lcc = BGFG_FGD_LCC; dst.create((int) total, 1, 0, -1, true);
N1cc = BGFG_FGD_N1CC;
N2cc = BGFG_FGD_N2CC;
delta = BGFG_FGD_DELTA; for (size_t i = 0; i < total; ++i)
{
std::vector<Point>& c = foreground_regions_[i];
alpha1 = BGFG_FGD_ALPHA_1; dst.create((int) c.size(), 1, CV_32SC2, (int) i, true);
alpha2 = BGFG_FGD_ALPHA_2; Mat ci = dst.getMat((int) i);
alpha3 = BGFG_FGD_ALPHA_3;
T = BGFG_FGD_T; Mat(ci.size(), ci.type(), &c[0]).copyTo(ci);
minArea = BGFG_FGD_MINAREA; }
is_obj_without_holes = true;
perform_morphing = 1;
} }
cv::gpu::FGDStatModel::FGDStatModel(int out_cn) void FGDImpl::initialize(const GpuMat& firstFrame)
{ {
impl_.reset(new Impl(background, foreground, foreground_regions, out_cn)); CV_Assert( firstFrame.type() == CV_8UC3 || firstFrame.type() == CV_8UC4 );
frameSize_ = firstFrame.size();
gpu::ensureSizeIsEnough(firstFrame.size(), CV_8UC1, foreground_);
copyChannels(firstFrame, background_, 4);
copyChannels(firstFrame, prevFrame_, 4);
gpu::ensureSizeIsEnough(firstFrame.size(), CV_8UC1, Ftd_);
gpu::ensureSizeIsEnough(firstFrame.size(), CV_8UC1, Fbd_);
stat_.create(firstFrame.size(), params_);
fgd::setBGPixelStat(stat_);
#ifdef HAVE_OPENCV_GPUFILTERS
if (params_.perform_morphing > 0)
{
Mat kernel = getStructuringElement(MORPH_RECT, Size(1 + params_.perform_morphing * 2, 1 + params_.perform_morphing * 2));
Point anchor(params_.perform_morphing, params_.perform_morphing);
dilateFilter_ = gpu::createMorphologyFilter(MORPH_DILATE, CV_8UC1, kernel, anchor);
erodeFilter_ = gpu::createMorphologyFilter(MORPH_ERODE, CV_8UC1, kernel, anchor);
}
#endif
}
} }
cv::gpu::FGDStatModel::FGDStatModel(const cv::gpu::GpuMat& firstFrame, const Params& params, int out_cn) Ptr<gpu::BackgroundSubtractorFGD> cv::gpu::createBackgroundSubtractorFGD(const FGDParams& params)
{ {
impl_.reset(new Impl(background, foreground, foreground_regions, out_cn)); return new FGDImpl(params);
create(firstFrame, params);
}
cv::gpu::FGDStatModel::~FGDStatModel()
{
}
void cv::gpu::FGDStatModel::create(const cv::gpu::GpuMat& firstFrame, const Params& params)
{
impl_->create(firstFrame, params);
}
void cv::gpu::FGDStatModel::release()
{
impl_->release();
}
int cv::gpu::FGDStatModel::update(const cv::gpu::GpuMat& curFrame)
{
return impl_->update(curFrame);
} }
#endif // HAVE_CUDA #endif // HAVE_CUDA

View File

@ -42,17 +42,17 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace cv;
using namespace cv::gpu;
#if !defined HAVE_CUDA || defined(CUDA_DISABLER) #if !defined HAVE_CUDA || defined(CUDA_DISABLER)
cv::gpu::GMG_GPU::GMG_GPU() { throw_no_cuda(); } Ptr<gpu::BackgroundSubtractorGMG> cv::gpu::createBackgroundSubtractorGMG(int, double) { throw_no_cuda(); return Ptr<gpu::BackgroundSubtractorGMG>(); }
void cv::gpu::GMG_GPU::initialize(cv::Size, float, float) { throw_no_cuda(); }
void cv::gpu::GMG_GPU::operator ()(const cv::gpu::GpuMat&, cv::gpu::GpuMat&, float, cv::gpu::Stream&) { throw_no_cuda(); }
void cv::gpu::GMG_GPU::release() {}
#else #else
namespace cv { namespace gpu { namespace cudev { namespace cv { namespace gpu { namespace cudev {
namespace bgfg_gmg namespace gmg
{ {
void loadConstants(int width, int height, float minVal, float maxVal, int quantizationLevels, float backgroundPrior, void loadConstants(int width, int height, float minVal, float maxVal, int quantizationLevels, float backgroundPrior,
float decisionThreshold, int maxFeatures, int numInitializationFrames); float decisionThreshold, int maxFeatures, int numInitializationFrames);
@ -63,51 +63,111 @@ namespace cv { namespace gpu { namespace cudev {
} }
}}} }}}
cv::gpu::GMG_GPU::GMG_GPU() namespace
{ {
maxFeatures = 64; class GMGImpl : public gpu::BackgroundSubtractorGMG
learningRate = 0.025f; {
numInitializationFrames = 120; public:
quantizationLevels = 16; GMGImpl(int initializationFrames, double decisionThreshold);
backgroundPrior = 0.8f;
decisionThreshold = 0.8f; void apply(InputArray image, OutputArray fgmask, double learningRate=-1);
smoothingRadius = 7; void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream);
updateBackgroundModel = true;
void getBackgroundImage(OutputArray backgroundImage) const;
int getMaxFeatures() const { return maxFeatures_; }
void setMaxFeatures(int maxFeatures) { maxFeatures_ = maxFeatures; }
double getDefaultLearningRate() const { return learningRate_; }
void setDefaultLearningRate(double lr) { learningRate_ = (float) lr; }
int getNumFrames() const { return numInitializationFrames_; }
void setNumFrames(int nframes) { numInitializationFrames_ = nframes; }
int getQuantizationLevels() const { return quantizationLevels_; }
void setQuantizationLevels(int nlevels) { quantizationLevels_ = nlevels; }
double getBackgroundPrior() const { return backgroundPrior_; }
void setBackgroundPrior(double bgprior) { backgroundPrior_ = (float) bgprior; }
int getSmoothingRadius() const { return smoothingRadius_; }
void setSmoothingRadius(int radius) { smoothingRadius_ = radius; }
double getDecisionThreshold() const { return decisionThreshold_; }
void setDecisionThreshold(double thresh) { decisionThreshold_ = (float) thresh; }
bool getUpdateBackgroundModel() const { return updateBackgroundModel_; }
void setUpdateBackgroundModel(bool update) { updateBackgroundModel_ = update; }
double getMinVal() const { return minVal_; }
void setMinVal(double val) { minVal_ = (float) val; }
double getMaxVal() const { return maxVal_; }
void setMaxVal(double val) { maxVal_ = (float) val; }
private:
void initialize(Size frameSize, float min, float max);
//! Total number of distinct colors to maintain in histogram.
int maxFeatures_;
//! Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms.
float learningRate_;
//! Number of frames of video to use to initialize histograms.
int numInitializationFrames_;
//! Number of discrete levels in each channel to be used in histograms.
int quantizationLevels_;
//! Prior probability that any given pixel is a background pixel. A sensitivity parameter.
float backgroundPrior_;
//! Smoothing radius, in pixels, for cleaning up FG image.
int smoothingRadius_;
//! Value above which pixel is determined to be FG.
float decisionThreshold_;
//! Perform background model update.
bool updateBackgroundModel_;
float minVal_, maxVal_;
Size frameSize_;
int frameNum_;
GpuMat nfeatures_;
GpuMat colors_;
GpuMat weights_;
#if defined(HAVE_OPENCV_GPUFILTERS) && defined(HAVE_OPENCV_GPUARITHM)
Ptr<gpu::Filter> boxFilter_;
GpuMat buf_;
#endif
};
GMGImpl::GMGImpl(int initializationFrames, double decisionThreshold)
{
maxFeatures_ = 64;
learningRate_ = 0.025f;
numInitializationFrames_ = initializationFrames;
quantizationLevels_ = 16;
backgroundPrior_ = 0.8f;
decisionThreshold_ = (float) decisionThreshold;
smoothingRadius_ = 7;
updateBackgroundModel_ = true;
minVal_ = maxVal_ = 0;
} }
void cv::gpu::GMG_GPU::initialize(cv::Size frameSize, float min, float max) void GMGImpl::apply(InputArray image, OutputArray fgmask, double learningRate)
{ {
using namespace cv::gpu::cudev::bgfg_gmg; apply(image, fgmask, learningRate, Stream::Null());
CV_Assert(min < max);
CV_Assert(maxFeatures > 0);
CV_Assert(learningRate >= 0.0f && learningRate <= 1.0f);
CV_Assert(numInitializationFrames >= 1);
CV_Assert(quantizationLevels >= 1 && quantizationLevels <= 255);
CV_Assert(backgroundPrior >= 0.0f && backgroundPrior <= 1.0f);
minVal_ = min;
maxVal_ = max;
frameSize_ = frameSize;
frameNum_ = 0;
nfeatures_.create(frameSize_, CV_32SC1);
colors_.create(maxFeatures * frameSize_.height, frameSize_.width, CV_32SC1);
weights_.create(maxFeatures * frameSize_.height, frameSize_.width, CV_32FC1);
nfeatures_.setTo(cv::Scalar::all(0));
if (smoothingRadius > 0)
boxFilter_ = cv::gpu::createBoxFilter(CV_8UC1, -1, cv::Size(smoothingRadius, smoothingRadius));
loadConstants(frameSize_.width, frameSize_.height, minVal_, maxVal_, quantizationLevels, backgroundPrior, decisionThreshold, maxFeatures, numInitializationFrames);
} }
void cv::gpu::GMG_GPU::operator ()(const cv::gpu::GpuMat& frame, cv::gpu::GpuMat& fgmask, float newLearningRate, cv::gpu::Stream& stream) void GMGImpl::apply(InputArray _frame, OutputArray _fgmask, double newLearningRate, Stream& stream)
{ {
using namespace cv::gpu::cudev::bgfg_gmg; using namespace cv::gpu::cudev::gmg;
typedef void (*func_t)(PtrStepSzb frame, PtrStepb fgmask, PtrStepSzi colors, PtrStepf weights, PtrStepi nfeatures, typedef void (*func_t)(PtrStepSzb frame, PtrStepb fgmask, PtrStepSzi colors, PtrStepf weights, PtrStepi nfeatures,
int frameNum, float learningRate, bool updateBackgroundModel, cudaStream_t stream); int frameNum, float learningRate, bool updateBackgroundModel, cudaStream_t stream);
@ -121,45 +181,97 @@ void cv::gpu::GMG_GPU::operator ()(const cv::gpu::GpuMat& frame, cv::gpu::GpuMat
{update_gpu<float>, 0, update_gpu<float3>, update_gpu<float4>} {update_gpu<float>, 0, update_gpu<float3>, update_gpu<float4>}
}; };
GpuMat frame = _frame.getGpuMat();
CV_Assert( frame.depth() == CV_8U || frame.depth() == CV_16U || frame.depth() == CV_32F ); CV_Assert( frame.depth() == CV_8U || frame.depth() == CV_16U || frame.depth() == CV_32F );
CV_Assert( frame.channels() == 1 || frame.channels() == 3 || frame.channels() == 4 ); CV_Assert( frame.channels() == 1 || frame.channels() == 3 || frame.channels() == 4 );
if (newLearningRate != -1.0f) if (newLearningRate != -1.0)
{ {
CV_Assert(newLearningRate >= 0.0f && newLearningRate <= 1.0f); CV_Assert( newLearningRate >= 0.0 && newLearningRate <= 1.0 );
learningRate = newLearningRate; learningRate_ = (float) newLearningRate;
} }
if (frame.size() != frameSize_) if (frame.size() != frameSize_)
initialize(frame.size(), 0.0f, frame.depth() == CV_8U ? 255.0f : frame.depth() == CV_16U ? std::numeric_limits<ushort>::max() : 1.0f); {
double minVal = minVal_;
double maxVal = maxVal_;
fgmask.create(frameSize_, CV_8UC1); if (minVal_ == 0 && maxVal_ == 0)
fgmask.setTo(cv::Scalar::all(0), stream); {
minVal = 0;
maxVal = frame.depth() == CV_8U ? 255.0 : frame.depth() == CV_16U ? std::numeric_limits<ushort>::max() : 1.0;
}
funcs[frame.depth()][frame.channels() - 1](frame, fgmask, colors_, weights_, nfeatures_, frameNum_, learningRate, updateBackgroundModel, cv::gpu::StreamAccessor::getStream(stream)); initialize(frame.size(), (float) minVal, (float) maxVal);
}
_fgmask.create(frameSize_, CV_8UC1);
GpuMat fgmask = _fgmask.getGpuMat();
fgmask.setTo(Scalar::all(0), stream);
funcs[frame.depth()][frame.channels() - 1](frame, fgmask, colors_, weights_, nfeatures_, frameNum_,
learningRate_, updateBackgroundModel_, StreamAccessor::getStream(stream));
#if defined(HAVE_OPENCV_GPUFILTERS) && defined(HAVE_OPENCV_GPUARITHM)
// medianBlur // medianBlur
if (smoothingRadius > 0) if (smoothingRadius_ > 0)
{ {
boxFilter_->apply(fgmask, buf_, stream); boxFilter_->apply(fgmask, buf_, stream);
int minCount = (smoothingRadius * smoothingRadius + 1) / 2; const int minCount = (smoothingRadius_ * smoothingRadius_ + 1) / 2;
double thresh = 255.0 * minCount / (smoothingRadius * smoothingRadius); const double thresh = 255.0 * minCount / (smoothingRadius_ * smoothingRadius_);
cv::gpu::threshold(buf_, fgmask, thresh, 255.0, cv::THRESH_BINARY, stream); gpu::threshold(buf_, fgmask, thresh, 255.0, THRESH_BINARY, stream);
} }
#endif
// keep track of how many frames we have processed // keep track of how many frames we have processed
++frameNum_; ++frameNum_;
} }
void cv::gpu::GMG_GPU::release() void GMGImpl::getBackgroundImage(OutputArray backgroundImage) const
{ {
frameSize_ = Size(); (void) backgroundImage;
CV_Error(Error::StsNotImplemented, "Not implemented");
}
nfeatures_.release(); void GMGImpl::initialize(Size frameSize, float min, float max)
colors_.release(); {
weights_.release(); using namespace cv::gpu::cudev::gmg;
boxFilter_.release();
buf_.release(); CV_Assert( maxFeatures_ > 0 );
CV_Assert( learningRate_ >= 0.0f && learningRate_ <= 1.0f);
CV_Assert( numInitializationFrames_ >= 1);
CV_Assert( quantizationLevels_ >= 1 && quantizationLevels_ <= 255);
CV_Assert( backgroundPrior_ >= 0.0f && backgroundPrior_ <= 1.0f);
minVal_ = min;
maxVal_ = max;
CV_Assert( minVal_ < maxVal_ );
frameSize_ = frameSize;
frameNum_ = 0;
nfeatures_.create(frameSize_, CV_32SC1);
colors_.create(maxFeatures_ * frameSize_.height, frameSize_.width, CV_32SC1);
weights_.create(maxFeatures_ * frameSize_.height, frameSize_.width, CV_32FC1);
nfeatures_.setTo(Scalar::all(0));
#if defined(HAVE_OPENCV_GPUFILTERS) && defined(HAVE_OPENCV_GPUARITHM)
if (smoothingRadius_ > 0)
boxFilter_ = gpu::createBoxFilter(CV_8UC1, -1, Size(smoothingRadius_, smoothingRadius_));
#endif
loadConstants(frameSize_.width, frameSize_.height, minVal_, maxVal_,
quantizationLevels_, backgroundPrior_, decisionThreshold_, maxFeatures_, numInitializationFrames_);
}
}
Ptr<gpu::BackgroundSubtractorGMG> cv::gpu::createBackgroundSubtractorGMG(int initializationFrames, double decisionThreshold)
{
return new GMGImpl(initializationFrames, decisionThreshold);
} }
#endif #endif

View File

@ -42,19 +42,12 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace cv;
using namespace cv::gpu;
#if !defined HAVE_CUDA || defined(CUDA_DISABLER) #if !defined HAVE_CUDA || defined(CUDA_DISABLER)
cv::gpu::MOG_GPU::MOG_GPU(int) { throw_no_cuda(); } Ptr<gpu::BackgroundSubtractorMOG> cv::gpu::createBackgroundSubtractorMOG(int, int, double, double) { throw_no_cuda(); return Ptr<gpu::BackgroundSubtractorMOG>(); }
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() {}
cv::gpu::MOG2_GPU::MOG2_GPU(int) { throw_no_cuda(); }
void cv::gpu::MOG2_GPU::initialize(cv::Size, int) { throw_no_cuda(); }
void cv::gpu::MOG2_GPU::operator()(const GpuMat&, GpuMat&, float, Stream&) { throw_no_cuda(); }
void cv::gpu::MOG2_GPU::getBackgroundImage(GpuMat&, Stream&) const { throw_no_cuda(); }
void cv::gpu::MOG2_GPU::release() {}
#else #else
@ -66,14 +59,10 @@ namespace cv { namespace gpu { namespace cudev
int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma, int nmixtures, float varThreshold, float learningRate, float backgroundRatio, float noiseSigma,
cudaStream_t stream); cudaStream_t stream);
void getBackgroundImage_gpu(int cn, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, int nmixtures, float backgroundRatio, cudaStream_t stream); void getBackgroundImage_gpu(int cn, PtrStepSzf weight, PtrStepSzb mean, PtrStepSzb dst, int nmixtures, float backgroundRatio, cudaStream_t stream);
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);
} }
}}} }}}
namespace mog namespace
{ {
const int defaultNMixtures = 5; const int defaultNMixtures = 5;
const int defaultHistory = 200; const int defaultHistory = 200;
@ -81,19 +70,109 @@ namespace mog
const float defaultVarThreshold = 2.5f * 2.5f; const float defaultVarThreshold = 2.5f * 2.5f;
const float defaultNoiseSigma = 30.0f * 0.5f; const float defaultNoiseSigma = 30.0f * 0.5f;
const float defaultInitialWeight = 0.05f; const float defaultInitialWeight = 0.05f;
}
cv::gpu::MOG_GPU::MOG_GPU(int nmixtures) : class MOGImpl : public gpu::BackgroundSubtractorMOG
{
public:
MOGImpl(int history, int nmixtures, double backgroundRatio, double noiseSigma);
void apply(InputArray image, OutputArray fgmask, double learningRate=-1);
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream);
void getBackgroundImage(OutputArray backgroundImage) const;
void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const;
int getHistory() const { return history_; }
void setHistory(int nframes) { history_ = nframes; }
int getNMixtures() const { return nmixtures_; }
void setNMixtures(int nmix) { nmixtures_ = nmix; }
double getBackgroundRatio() const { return backgroundRatio_; }
void setBackgroundRatio(double backgroundRatio) { backgroundRatio_ = (float) backgroundRatio; }
double getNoiseSigma() const { return noiseSigma_; }
void setNoiseSigma(double noiseSigma) { noiseSigma_ = (float) noiseSigma; }
private:
//! re-initiaization method
void initialize(Size frameSize, int frameType);
int history_;
int nmixtures_;
float backgroundRatio_;
float noiseSigma_;
float varThreshold_;
Size frameSize_;
int frameType_;
int nframes_;
GpuMat weight_;
GpuMat sortKey_;
GpuMat mean_;
GpuMat var_;
};
MOGImpl::MOGImpl(int history, int nmixtures, double backgroundRatio, double noiseSigma) :
frameSize_(0, 0), frameType_(0), nframes_(0) frameSize_(0, 0), frameType_(0), nframes_(0)
{ {
nmixtures_ = std::min(nmixtures > 0 ? nmixtures : mog::defaultNMixtures, 8); history_ = history > 0 ? history : defaultHistory;
history = mog::defaultHistory; nmixtures_ = std::min(nmixtures > 0 ? nmixtures : defaultNMixtures, 8);
varThreshold = mog::defaultVarThreshold; backgroundRatio_ = backgroundRatio > 0 ? (float) backgroundRatio : defaultBackgroundRatio;
backgroundRatio = mog::defaultBackgroundRatio; noiseSigma_ = noiseSigma > 0 ? (float) noiseSigma : defaultNoiseSigma;
noiseSigma = mog::defaultNoiseSigma;
varThreshold_ = defaultVarThreshold;
} }
void cv::gpu::MOG_GPU::initialize(cv::Size frameSize, int frameType) void MOGImpl::apply(InputArray image, OutputArray fgmask, double learningRate)
{
apply(image, fgmask, learningRate, Stream::Null());
}
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());
_fgmask.create(frameSize_, CV_8UC1);
GpuMat fgmask = _fgmask.getGpuMat();
++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 ); CV_Assert( frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_8UC4 );
@ -120,160 +199,11 @@ void cv::gpu::MOG_GPU::initialize(cv::Size frameSize, int frameType)
nframes_ = 0; nframes_ = 0;
} }
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;
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());
fgmask.create(frameSize_, CV_8UC1);
++nframes_;
learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(nframes_, history);
CV_Assert(learningRate >= 0.0f);
mog_gpu(frame, ch, fgmask, weight_, sortKey_, mean_, var_, nmixtures_,
varThreshold, learningRate, backgroundRatio, noiseSigma,
StreamAccessor::getStream(stream));
} }
void cv::gpu::MOG_GPU::getBackgroundImage(GpuMat& backgroundImage, Stream& stream) const Ptr<gpu::BackgroundSubtractorMOG> cv::gpu::createBackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma)
{ {
using namespace cv::gpu::cudev::mog; return new MOGImpl(history, nmixtures, backgroundRatio, noiseSigma);
backgroundImage.create(frameSize_, frameType_);
getBackgroundImage_gpu(backgroundImage.channels(), weight_, mean_, backgroundImage, nmixtures_, backgroundRatio, StreamAccessor::getStream(stream));
}
void cv::gpu::MOG_GPU::release()
{
frameSize_ = Size(0, 0);
frameType_ = 0;
nframes_ = 0;
weight_.release();
sortKey_.release();
mean_.release();
var_.release();
}
/////////////////////////////////////////////////////////////////
// MOG2
namespace mog2
{
// 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 defaultfCT = 0.05f; // complexity reduction prior constant 0 - no reduction of number of components
const unsigned char defaultnShadowDetection = 127; // value to use in the segmentation mask for shadows, set 0 not to do shadow detection
const float defaultfTau = 0.5f; // Tau - shadow threshold, see the paper for explanation
}
cv::gpu::MOG2_GPU::MOG2_GPU(int nmixtures) :
frameSize_(0, 0), frameType_(0), nframes_(0)
{
nmixtures_ = nmixtures > 0 ? nmixtures : mog2::defaultNMixtures;
history = mog2::defaultHistory;
varThreshold = mog2::defaultVarThreshold;
bShadowDetection = true;
backgroundRatio = mog2::defaultBackgroundRatio;
fVarInit = mog2::defaultVarInit;
fVarMax = mog2::defaultVarMax;
fVarMin = mog2::defaultVarMin;
varThresholdGen = mog2::defaultVarThresholdGen;
fCT = mog2::defaultfCT;
nShadowDetection = mog2::defaultnShadowDetection;
fTau = mog2::defaultfTau;
}
void cv::gpu::MOG2_GPU::initialize(cv::Size frameSize, int frameType)
{
using namespace cv::gpu::cudev::mog;
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(cv::Scalar::all(0));
loadConstants(nmixtures_, varThreshold, backgroundRatio, varThresholdGen, fVarInit, fVarMin, fVarMax, fTau, nShadowDetection);
}
void cv::gpu::MOG2_GPU::operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate, Stream& stream)
{
using namespace cv::gpu::cudev::mog;
int ch = frame.channels();
int work_ch = ch;
if (nframes_ == 0 || learningRate >= 1.0f || frame.size() != frameSize_ || work_ch != mean_.channels())
initialize(frame.size(), frame.type());
fgmask.create(frameSize_, CV_8UC1);
fgmask.setTo(cv::Scalar::all(0));
++nframes_;
learningRate = learningRate >= 0.0f && nframes_ > 1 ? learningRate : 1.0f / std::min(2 * nframes_, history);
CV_Assert(learningRate >= 0.0f);
mog2_gpu(frame, frame.channels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_, learningRate, -learningRate * fCT, bShadowDetection, StreamAccessor::getStream(stream));
}
void cv::gpu::MOG2_GPU::getBackgroundImage(GpuMat& backgroundImage, Stream& stream) const
{
using namespace cv::gpu::cudev::mog;
backgroundImage.create(frameSize_, frameType_);
getBackgroundImage2_gpu(backgroundImage.channels(), bgmodelUsedModes_, weight_, mean_, backgroundImage, StreamAccessor::getStream(stream));
}
void cv::gpu::MOG2_GPU::release()
{
frameSize_ = Size(0, 0);
frameType_ = 0;
nframes_ = 0;
weight_.release();
variance_.release();
mean_.release();
bgmodelUsedModes_.release();
} }
#endif #endif

View File

@ -0,0 +1,253 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
using namespace cv;
using namespace cv::gpu;
#if !defined HAVE_CUDA || defined(CUDA_DISABLER)
Ptr<gpu::BackgroundSubtractorMOG2> cv::gpu::createBackgroundSubtractorMOG2(int, double, bool) { throw_no_cuda(); return Ptr<gpu::BackgroundSubtractorMOG2>(); }
#else
namespace cv { namespace gpu { namespace cudev
{
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);
}
}}}
namespace
{
// 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 : public gpu::BackgroundSubtractorMOG2
{
public:
MOG2Impl(int history, double varThreshold, bool detectShadows);
void apply(InputArray image, OutputArray fgmask, double learningRate=-1);
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream);
void getBackgroundImage(OutputArray backgroundImage) const;
void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const;
int getHistory() const { return history_; }
void setHistory(int history) { history_ = history; }
int getNMixtures() const { return nmixtures_; }
void setNMixtures(int nmixtures) { nmixtures_ = nmixtures; }
double getBackgroundRatio() const { return backgroundRatio_; }
void setBackgroundRatio(double ratio) { backgroundRatio_ = (float) ratio; }
double getVarThreshold() const { return varThreshold_; }
void setVarThreshold(double varThreshold) { varThreshold_ = (float) varThreshold; }
double getVarThresholdGen() const { return varThresholdGen_; }
void setVarThresholdGen(double varThresholdGen) { varThresholdGen_ = (float) varThresholdGen; }
double getVarInit() const { return varInit_; }
void setVarInit(double varInit) { varInit_ = (float) varInit; }
double getVarMin() const { return varMin_; }
void setVarMin(double varMin) { varMin_ = (float) varMin; }
double getVarMax() const { return varMax_; }
void setVarMax(double varMax) { varMax_ = (float) varMax; }
double getComplexityReductionThreshold() const { return ct_; }
void setComplexityReductionThreshold(double ct) { ct_ = (float) ct; }
bool getDetectShadows() const { return detectShadows_; }
void setDetectShadows(bool detectShadows) { detectShadows_ = detectShadows; }
int getShadowValue() const { return shadowValue_; }
void setShadowValue(int value) { shadowValue_ = (uchar) value; }
double getShadowThreshold() const { return shadowThreshold_; }
void setShadowThreshold(double threshold) { 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;
}
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)
{
using namespace cv::gpu::cudev::mog2;
GpuMat frame = _frame.getGpuMat();
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());
_fgmask.create(frameSize_, CV_8UC1);
GpuMat fgmask = _fgmask.getGpuMat();
fgmask.setTo(Scalar::all(0), stream);
++nframes_;
learningRate = learningRate >= 0 && nframes_ > 1 ? learningRate : 1.0 / std::min(2 * nframes_, history_);
CV_Assert( learningRate >= 0 );
mog2_gpu(frame, frame.channels(), fgmask, bgmodelUsedModes_, weight_, variance_, mean_,
(float) learningRate, static_cast<float>(-learningRate * ct_), detectShadows_, StreamAccessor::getStream(stream));
}
void MOG2Impl::getBackgroundImage(OutputArray backgroundImage) const
{
getBackgroundImage(backgroundImage, Stream::Null());
}
void MOG2Impl::getBackgroundImage(OutputArray _backgroundImage, Stream& stream) const
{
using namespace cv::gpu::cudev::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::gpu::cudev::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));
loadConstants(nmixtures_, varThreshold_, backgroundRatio_, varThresholdGen_, varInit_, varMin_, varMax_, shadowThreshold_, shadowValue_);
}
}
Ptr<gpu::BackgroundSubtractorMOG2> cv::gpu::createBackgroundSubtractorMOG2(int history, double varThreshold, bool detectShadows)
{
return new MOG2Impl(history, varThreshold, detectShadows);
}
#endif

View File

@ -46,10 +46,21 @@
#include <limits> #include <limits>
#include "opencv2/gpubgsegm.hpp" #include "opencv2/gpubgsegm.hpp"
#include "opencv2/gpuarithm.hpp"
#include "opencv2/gpufilters.hpp"
#include "opencv2/gpuimgproc.hpp"
#include "opencv2/core/private.gpu.hpp" #include "opencv2/core/private.gpu.hpp"
#include "opencv2/opencv_modules.hpp"
#ifdef HAVE_OPENCV_GPUARITHM
# include "opencv2/gpuarithm.hpp"
#endif
#ifdef HAVE_OPENCV_GPUFILTERS
# include "opencv2/gpufilters.hpp"
#endif
#ifdef HAVE_OPENCV_GPUIMGPROC
# include "opencv2/gpuimgproc.hpp"
#endif
#endif /* __OPENCV_PRECOMP_H__ */ #endif /* __OPENCV_PRECOMP_H__ */

View File

@ -41,7 +41,10 @@
//M*/ //M*/
#include "test_precomp.hpp" #include "test_precomp.hpp"
#ifdef HAVE_OPENCV_LEGACY
# include "opencv2/legacy.hpp" # include "opencv2/legacy.hpp"
#endif
#ifdef HAVE_CUDA #ifdef HAVE_CUDA
@ -62,7 +65,7 @@ using namespace cvtest;
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// FGDStatModel // FGDStatModel
#if BUILD_WITH_VIDEO_INPUT_SUPPORT #if BUILD_WITH_VIDEO_INPUT_SUPPORT && defined(HAVE_OPENCV_LEGACY)
namespace cv namespace cv
{ {
@ -72,11 +75,10 @@ namespace cv
} }
} }
PARAM_TEST_CASE(FGDStatModel, cv::gpu::DeviceInfo, std::string, Channels) PARAM_TEST_CASE(FGDStatModel, cv::gpu::DeviceInfo, std::string)
{ {
cv::gpu::DeviceInfo devInfo; cv::gpu::DeviceInfo devInfo;
std::string inputFile; std::string inputFile;
int out_cn;
virtual void SetUp() virtual void SetUp()
{ {
@ -84,8 +86,6 @@ PARAM_TEST_CASE(FGDStatModel, cv::gpu::DeviceInfo, std::string, Channels)
cv::gpu::setDevice(devInfo.deviceID()); cv::gpu::setDevice(devInfo.deviceID());
inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "video/" + GET_PARAM(1); inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "video/" + GET_PARAM(1);
out_cn = GET_PARAM(2);
} }
}; };
@ -102,15 +102,10 @@ GPU_TEST_P(FGDStatModel, Update)
cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame)); cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame));
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame);
cv::gpu::FGDStatModel d_model(out_cn); cv::Ptr<cv::gpu::BackgroundSubtractorFGD> d_fgd = cv::gpu::createBackgroundSubtractorFGD();
d_model.create(d_frame); cv::gpu::GpuMat d_foreground, d_background;
std::vector< std::vector<cv::Point> > foreground_regions;
cv::Mat h_background; d_fgd->apply(d_frame, d_foreground);
cv::Mat h_foreground;
cv::Mat h_background3;
cv::Mat backgroundDiff;
cv::Mat foregroundDiff;
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
{ {
@ -121,32 +116,23 @@ GPU_TEST_P(FGDStatModel, Update)
int gold_count = cvUpdateBGStatModel(&ipl_frame, model); int gold_count = cvUpdateBGStatModel(&ipl_frame, model);
d_frame.upload(frame); d_frame.upload(frame);
d_fgd->apply(d_frame, d_foreground);
int count = d_model.update(d_frame); d_fgd->getBackgroundImage(d_background);
d_fgd->getForegroundRegions(foreground_regions);
ASSERT_EQ(gold_count, count); int count = (int) foreground_regions.size();
cv::Mat gold_background = cv::cvarrToMat(model->background); cv::Mat gold_background = cv::cvarrToMat(model->background);
cv::Mat gold_foreground = cv::cvarrToMat(model->foreground); cv::Mat gold_foreground = cv::cvarrToMat(model->foreground);
if (out_cn == 3) ASSERT_MAT_NEAR(gold_background, d_background, 1.0);
d_model.background.download(h_background3); ASSERT_MAT_NEAR(gold_foreground, d_foreground, 0.0);
else ASSERT_EQ(gold_count, count);
{
d_model.background.download(h_background);
cv::cvtColor(h_background, h_background3, cv::COLOR_BGRA2BGR);
}
d_model.foreground.download(h_foreground);
ASSERT_MAT_NEAR(gold_background, h_background3, 1.0);
ASSERT_MAT_NEAR(gold_foreground, h_foreground, 0.0);
} }
} }
INSTANTIATE_TEST_CASE_P(GPU_BgSegm, FGDStatModel, testing::Combine( INSTANTIATE_TEST_CASE_P(GPU_BgSegm, FGDStatModel, testing::Combine(
ALL_DEVICES, ALL_DEVICES,
testing::Values(std::string("768x576.avi")), testing::Values(std::string("768x576.avi"))));
testing::Values(Channels(3), Channels(4))));
#endif #endif
@ -193,7 +179,7 @@ GPU_TEST_P(MOG, Update)
cap >> frame; cap >> frame;
ASSERT_FALSE(frame.empty()); ASSERT_FALSE(frame.empty());
cv::gpu::MOG_GPU mog; cv::Ptr<cv::BackgroundSubtractorMOG> mog = cv::gpu::createBackgroundSubtractorMOG();
cv::gpu::GpuMat foreground = createMat(frame.size(), CV_8UC1, useRoi); cv::gpu::GpuMat foreground = createMat(frame.size(), CV_8UC1, useRoi);
cv::Ptr<cv::BackgroundSubtractorMOG> mog_gold = cv::createBackgroundSubtractorMOG(); cv::Ptr<cv::BackgroundSubtractorMOG> mog_gold = cv::createBackgroundSubtractorMOG();
@ -211,7 +197,7 @@ GPU_TEST_P(MOG, Update)
cv::swap(temp, frame); cv::swap(temp, frame);
} }
mog(loadMat(frame, useRoi), foreground, (float)learningRate); mog->apply(loadMat(frame, useRoi), foreground, learningRate);
mog_gold->apply(frame, foreground_gold, learningRate); mog_gold->apply(frame, foreground_gold, learningRate);
@ -267,8 +253,8 @@ GPU_TEST_P(MOG2, Update)
cap >> frame; cap >> frame;
ASSERT_FALSE(frame.empty()); ASSERT_FALSE(frame.empty());
cv::gpu::MOG2_GPU mog2; cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::gpu::createBackgroundSubtractorMOG2();
mog2.bShadowDetection = detectShadow; mog2->setDetectShadows(detectShadow);
cv::gpu::GpuMat foreground = createMat(frame.size(), CV_8UC1, useRoi); cv::gpu::GpuMat foreground = createMat(frame.size(), CV_8UC1, useRoi);
cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2(); cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2();
@ -287,7 +273,7 @@ GPU_TEST_P(MOG2, Update)
cv::swap(temp, frame); cv::swap(temp, frame);
} }
mog2(loadMat(frame, useRoi), foreground); mog2->apply(loadMat(frame, useRoi), foreground);
mog2_gold->apply(frame, foreground_gold); mog2_gold->apply(frame, foreground_gold);
@ -312,8 +298,8 @@ GPU_TEST_P(MOG2, getBackgroundImage)
cv::Mat frame; cv::Mat frame;
cv::gpu::MOG2_GPU mog2; cv::Ptr<cv::BackgroundSubtractorMOG2> mog2 = cv::gpu::createBackgroundSubtractorMOG2();
mog2.bShadowDetection = detectShadow; mog2->setDetectShadows(detectShadow);
cv::gpu::GpuMat foreground; cv::gpu::GpuMat foreground;
cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2(); cv::Ptr<cv::BackgroundSubtractorMOG2> mog2_gold = cv::createBackgroundSubtractorMOG2();
@ -325,13 +311,13 @@ GPU_TEST_P(MOG2, getBackgroundImage)
cap >> frame; cap >> frame;
ASSERT_FALSE(frame.empty()); ASSERT_FALSE(frame.empty());
mog2(loadMat(frame, useRoi), foreground); mog2->apply(loadMat(frame, useRoi), foreground);
mog2_gold->apply(frame, foreground_gold); mog2_gold->apply(frame, foreground_gold);
} }
cv::gpu::GpuMat background = createMat(frame.size(), frame.type(), useRoi); cv::gpu::GpuMat background = createMat(frame.size(), frame.type(), useRoi);
mog2.getBackgroundImage(background); mog2->getBackgroundImage(background);
cv::Mat background_gold; cv::Mat background_gold;
mog2_gold->getBackgroundImage(background_gold); mog2_gold->getBackgroundImage(background_gold);
@ -372,16 +358,15 @@ GPU_TEST_P(GMG, Accuracy)
cv::Mat frame = randomMat(size, type, 0, 100); cv::Mat frame = randomMat(size, type, 0, 100);
cv::gpu::GpuMat d_frame = loadMat(frame, useRoi); cv::gpu::GpuMat d_frame = loadMat(frame, useRoi);
cv::gpu::GMG_GPU gmg; cv::Ptr<cv::BackgroundSubtractorGMG> gmg = cv::gpu::createBackgroundSubtractorGMG();
gmg.numInitializationFrames = 5; gmg->setNumFrames(5);
gmg.smoothingRadius = 0; gmg->setSmoothingRadius(0);
gmg.initialize(d_frame.size(), 0, 255);
cv::gpu::GpuMat d_fgmask = createMat(size, CV_8UC1, useRoi); cv::gpu::GpuMat d_fgmask = createMat(size, CV_8UC1, useRoi);
for (int i = 0; i < gmg.numInitializationFrames; ++i) for (int i = 0; i < gmg->getNumFrames(); ++i)
{ {
gmg(d_frame, d_fgmask); gmg->apply(d_frame, d_fgmask);
// fgmask should be entirely background during training // fgmask should be entirely background during training
ASSERT_MAT_NEAR(zeros, d_fgmask, 0); ASSERT_MAT_NEAR(zeros, d_fgmask, 0);
@ -389,7 +374,7 @@ GPU_TEST_P(GMG, Accuracy)
frame = randomMat(size, type, 160, 255); frame = randomMat(size, type, 160, 255);
d_frame = loadMat(frame, useRoi); d_frame = loadMat(frame, useRoi);
gmg(d_frame, d_fgmask); gmg->apply(d_frame, d_fgmask);
// now fgmask should be entirely foreground // now fgmask should be entirely foreground
ASSERT_MAT_NEAR(fullfg, d_fgmask, 0); ASSERT_MAT_NEAR(fullfg, d_fgmask, 0);

View File

@ -59,4 +59,6 @@
#include "opencv2/gpubgsegm.hpp" #include "opencv2/gpubgsegm.hpp"
#include "opencv2/video.hpp" #include "opencv2/video.hpp"
#include "opencv2/opencv_modules.hpp"
#endif #endif

View File

@ -18,10 +18,10 @@ using namespace cv::gpu;
enum Method enum Method
{ {
FGD_STAT,
MOG, MOG,
MOG2, MOG2,
GMG GMG,
FGD_STAT
}; };
int main(int argc, const char** argv) int main(int argc, const char** argv)
@ -29,7 +29,7 @@ int main(int argc, const char** argv)
cv::CommandLineParser cmd(argc, argv, cv::CommandLineParser cmd(argc, argv,
"{ c camera | | use camera }" "{ c camera | | use camera }"
"{ f file | 768x576.avi | input video file }" "{ f file | 768x576.avi | input video file }"
"{ m method | mog | method (fgd, mog, mog2, gmg) }" "{ m method | mog | method (mog, mog2, gmg, fgd) }"
"{ h help | | print help message }"); "{ h help | | print help message }");
if (cmd.has("help") || !cmd.check()) if (cmd.has("help") || !cmd.check())
@ -43,18 +43,18 @@ int main(int argc, const char** argv)
string file = cmd.get<string>("file"); string file = cmd.get<string>("file");
string method = cmd.get<string>("method"); string method = cmd.get<string>("method");
if (method != "fgd" if (method != "mog"
&& method != "mog"
&& method != "mog2" && method != "mog2"
&& method != "gmg") && method != "gmg"
&& method != "fgd")
{ {
cerr << "Incorrect method" << endl; cerr << "Incorrect method" << endl;
return -1; return -1;
} }
Method m = method == "fgd" ? FGD_STAT : Method m = method == "mog" ? MOG :
method == "mog" ? MOG :
method == "mog2" ? MOG2 : method == "mog2" ? MOG2 :
method == "fgd" ? FGD_STAT :
GMG; GMG;
VideoCapture cap; VideoCapture cap;
@ -75,11 +75,10 @@ int main(int argc, const char** argv)
GpuMat d_frame(frame); GpuMat d_frame(frame);
FGDStatModel fgd_stat; Ptr<BackgroundSubtractor> mog = gpu::createBackgroundSubtractorMOG();
MOG_GPU mog; Ptr<BackgroundSubtractor> mog2 = gpu::createBackgroundSubtractorMOG2();
MOG2_GPU mog2; Ptr<BackgroundSubtractor> gmg = gpu::createBackgroundSubtractorGMG(40);
GMG_GPU gmg; Ptr<BackgroundSubtractor> fgd = gpu::createBackgroundSubtractorFGD();
gmg.numInitializationFrames = 40;
GpuMat d_fgmask; GpuMat d_fgmask;
GpuMat d_fgimg; GpuMat d_fgimg;
@ -91,20 +90,20 @@ int main(int argc, const char** argv)
switch (m) switch (m)
{ {
case FGD_STAT:
fgd_stat.create(d_frame);
break;
case MOG: case MOG:
mog(d_frame, d_fgmask, 0.01f); mog->apply(d_frame, d_fgmask, 0.01);
break; break;
case MOG2: case MOG2:
mog2(d_frame, d_fgmask); mog2->apply(d_frame, d_fgmask);
break; break;
case GMG: case GMG:
gmg.initialize(d_frame.size()); gmg->apply(d_frame, d_fgmask);
break;
case FGD_STAT:
fgd->apply(d_frame, d_fgmask);
break; break;
} }
@ -128,24 +127,23 @@ int main(int argc, const char** argv)
//update the model //update the model
switch (m) switch (m)
{ {
case FGD_STAT:
fgd_stat.update(d_frame);
d_fgmask = fgd_stat.foreground;
d_bgimg = fgd_stat.background;
break;
case MOG: case MOG:
mog(d_frame, d_fgmask, 0.01f); mog->apply(d_frame, d_fgmask, 0.01);
mog.getBackgroundImage(d_bgimg); mog->getBackgroundImage(d_bgimg);
break; break;
case MOG2: case MOG2:
mog2(d_frame, d_fgmask); mog2->apply(d_frame, d_fgmask);
mog2.getBackgroundImage(d_bgimg); mog2->getBackgroundImage(d_bgimg);
break; break;
case GMG: case GMG:
gmg(d_frame, d_fgmask); gmg->apply(d_frame, d_fgmask);
break;
case FGD_STAT:
fgd->apply(d_frame, d_fgmask);
fgd->getBackgroundImage(d_bgimg);
break; break;
} }

View File

@ -1271,14 +1271,14 @@ TEST(FGDStatModel)
{ {
const std::string inputFile = abspath("768x576.avi"); const std::string inputFile = abspath("768x576.avi");
cv::VideoCapture cap(inputFile); VideoCapture cap(inputFile);
if (!cap.isOpened()) throw runtime_error("can't open 768x576.avi"); if (!cap.isOpened()) throw runtime_error("can't open 768x576.avi");
cv::Mat frame; Mat frame;
cap >> frame; cap >> frame;
IplImage ipl_frame = frame; IplImage ipl_frame = frame;
cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame)); Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame));
while (!TestSystem::instance().stop()) while (!TestSystem::instance().stop())
{ {
@ -1297,8 +1297,10 @@ TEST(FGDStatModel)
cap >> frame; cap >> frame;
cv::gpu::GpuMat d_frame(frame); gpu::GpuMat d_frame(frame), d_fgmask;
cv::gpu::FGDStatModel d_model(d_frame); Ptr<BackgroundSubtractor> d_fgd = gpu::createBackgroundSubtractorFGD();
d_fgd->apply(d_frame, d_fgmask);
while (!TestSystem::instance().stop()) while (!TestSystem::instance().stop())
{ {
@ -1307,7 +1309,7 @@ TEST(FGDStatModel)
TestSystem::instance().gpuOn(); TestSystem::instance().gpuOn();
d_model.update(d_frame); d_fgd->apply(d_frame, d_fgmask);
TestSystem::instance().gpuOff(); TestSystem::instance().gpuOff();
} }
@ -1346,10 +1348,10 @@ TEST(MOG)
cap >> frame; cap >> frame;
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame);
cv::gpu::MOG_GPU d_mog; cv::Ptr<cv::BackgroundSubtractor> d_mog = cv::gpu::createBackgroundSubtractorMOG();
cv::gpu::GpuMat d_foreground; cv::gpu::GpuMat d_foreground;
d_mog(d_frame, d_foreground, 0.01f); d_mog->apply(d_frame, d_foreground, 0.01);
while (!TestSystem::instance().stop()) while (!TestSystem::instance().stop())
{ {
@ -1358,7 +1360,7 @@ TEST(MOG)
TestSystem::instance().gpuOn(); TestSystem::instance().gpuOn();
d_mog(d_frame, d_foreground, 0.01f); d_mog->apply(d_frame, d_foreground, 0.01);
TestSystem::instance().gpuOff(); TestSystem::instance().gpuOff();
} }
@ -1399,13 +1401,13 @@ TEST(MOG2)
cap >> frame; cap >> frame;
cv::Ptr<cv::BackgroundSubtractor> d_mog2 = cv::gpu::createBackgroundSubtractorMOG2();
cv::gpu::GpuMat d_frame(frame); cv::gpu::GpuMat d_frame(frame);
cv::gpu::MOG2_GPU d_mog2;
cv::gpu::GpuMat d_foreground; cv::gpu::GpuMat d_foreground;
cv::gpu::GpuMat d_background; cv::gpu::GpuMat d_background;
d_mog2(d_frame, d_foreground); d_mog2->apply(d_frame, d_foreground);
d_mog2.getBackgroundImage(d_background); d_mog2->getBackgroundImage(d_background);
while (!TestSystem::instance().stop()) while (!TestSystem::instance().stop())
{ {
@ -1414,8 +1416,8 @@ TEST(MOG2)
TestSystem::instance().gpuOn(); TestSystem::instance().gpuOn();
d_mog2(d_frame, d_foreground); d_mog2->apply(d_frame, d_foreground);
d_mog2.getBackgroundImage(d_background); d_mog2->getBackgroundImage(d_background);
TestSystem::instance().gpuOff(); TestSystem::instance().gpuOff();
} }