background_segm.hpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_BACKGROUND_SEGM_HPP__
44 #define __OPENCV_BACKGROUND_SEGM_HPP__
45 
46 #include "opencv2/core/core.hpp"
47 #include <list>
48 namespace cv
49 {
50 
57 class CV_EXPORTS_W BackgroundSubtractor : public Algorithm
58 {
59 public:
61  virtual ~BackgroundSubtractor();
63  CV_WRAP_AS(apply) virtual void operator()(InputArray image, OutputArray fgmask,
64  double learningRate=0);
65 
67  virtual void getBackgroundImage(OutputArray backgroundImage) const;
68 };
69 
70 
81 class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor
82 {
83 public:
85  CV_WRAP BackgroundSubtractorMOG();
87  CV_WRAP BackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma=0);
89  virtual ~BackgroundSubtractorMOG();
91  virtual void operator()(InputArray image, OutputArray fgmask, double learningRate=0);
92 
94  virtual void initialize(Size frameSize, int frameType);
95 
96  virtual AlgorithmInfo* info() const;
97 
98 protected:
102  int nframes;
103  int history;
105  double varThreshold;
107  double noiseSigma;
108 };
109 
110 
119 {
120 public:
122  CV_WRAP BackgroundSubtractorMOG2();
124  CV_WRAP BackgroundSubtractorMOG2(int history, float varThreshold, bool bShadowDetection=true);
126  virtual ~BackgroundSubtractorMOG2();
128  virtual void operator()(InputArray image, OutputArray fgmask, double learningRate=-1);
129 
131  virtual void getBackgroundImage(OutputArray backgroundImage) const;
132 
134  virtual void initialize(Size frameSize, int frameType);
135 
136  virtual AlgorithmInfo* info() const;
137 
138 protected:
142  Mat bgmodelUsedModes;//keep track of number of modes per pixel
143  int nframes;
144  int history;
148  double varThreshold;
149  // threshold on the squared Mahalanobis distance to decide if it is well described
150  // by the background model or not. Related to Cthr from the paper.
151  // This does not influence the update of the background. A typical value could be 4 sigma
152  // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
153 
155  // less important parameters - things you might change but be carefull
158  // corresponds to fTB=1-cf from the paper
159  // TB - threshold when the component becomes significant enough to be included into
160  // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
161  // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
162  // it is considered foreground
163  // float noiseSigma;
165  //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
166  //when a sample is close to the existing components. If it is not close
167  //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
168  //Smaller Tg leads to more generated components and higher Tg might make
169  //lead to small number of components but they can grow too large
170  float fVarInit;
171  float fVarMin;
172  float fVarMax;
173  //initial variance for the newly generated components.
174  //It will will influence the speed of adaptation. A good guess should be made.
175  //A simple way is to estimate the typical standard deviation from the images.
176  //I used here 10 as a reasonable value
177  // min and max can be used to further control the variance
178  float fCT;//CT - complexity reduction prior
179  //this is related to the number of samples needed to accept that a component
180  //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
181  //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
182 
183  //shadow detection parameters
184  bool bShadowDetection;//default 1 - do shadow detection
185  unsigned char nShadowDetection;//do shadow detection - insert this value as the detection result - 127 default value
186  float fTau;
187  // Tau - shadow threshold. The shadow is detected if the pixel is darker
188  //version of the background. Tau is a threshold on how much darker the shadow can be.
189  //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
190  //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
191 };
192 
201 {
202 public:
204  virtual ~BackgroundSubtractorGMG();
205  virtual AlgorithmInfo* info() const;
206 
214  void initialize(cv::Size frameSize, double min, double max);
215 
222  virtual void operator()(InputArray image, OutputArray fgmask, double learningRate=-1.0);
223 
227  void release();
228 
232  double learningRate;
245 
246 private:
247  double maxVal_;
248  double minVal_;
249 
250  cv::Size frameSize_;
251  int frameNum_;
252 
253  cv::Mat_<int> nfeatures_;
254  cv::Mat_<unsigned int> colors_;
255  cv::Mat_<float> weights_;
256 
257  cv::Mat buf_;
258 };
259 
260 }
261 
262 #endif
int nframes
Definition: background_segm.hpp:102
unsigned char nShadowDetection
Definition: background_segm.hpp:185
float backgroundRatio
Definition: background_segm.hpp:157
double learningRate
Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms.
Definition: background_segm.hpp:232
bool bShadowDetection
Definition: background_segm.hpp:184
int frameType
Definition: background_segm.hpp:100
double varThreshold
Definition: background_segm.hpp:148
const CvArr CvArr * fgmask
Definition: legacy.hpp:3418
The Base Class for Background/Foreground Segmentation.
Definition: background_segm.hpp:57
double varThreshold
Definition: background_segm.hpp:105
Mat bgmodelUsedModes
Definition: background_segm.hpp:142
int nmixtures
Definition: background_segm.hpp:104
float fVarInit
Definition: background_segm.hpp:170
Mat bgmodel
Definition: background_segm.hpp:101
The class implements the following algorithm: "Improved adaptive Gausian mixture model for background...
Definition: background_segm.hpp:118
double backgroundPrior
Prior probability that any given pixel is a background pixel. A sensitivity parameter.
Definition: background_segm.hpp:238
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: highgui_c.h:230
double backgroundRatio
Definition: background_segm.hpp:106
int quantizationLevels
Number of discrete levels in each channel to be used in histograms.
Definition: background_segm.hpp:236
Proxy datatype for passing Mat's and vector<>'s as input parameters.
Definition: core.hpp:1312
The 2D size class.
Definition: core.hpp:81
int nmixtures
Definition: background_segm.hpp:145
int frameType
Definition: background_segm.hpp:140
float fVarMin
Definition: background_segm.hpp:171
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
computes per-element minimum of two arrays (dst = min(src1, src2))
Size frameSize
Definition: background_segm.hpp:99
Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm.
Definition: background_segm.hpp:81
int nframes
Definition: background_segm.hpp:143
float fVarMax
Definition: background_segm.hpp:172
Definition: background_segm.hpp:200
bool updateBackgroundModel
Perform background model update.
Definition: background_segm.hpp:244
Mat bgmodel
Definition: background_segm.hpp:141
Size frameSize
Definition: background_segm.hpp:139
int numInitializationFrames
Number of frames of video to use to initialize histograms.
Definition: background_segm.hpp:234
int history
Definition: background_segm.hpp:103
The Core Functionality.
The n-dimensional matrix class.
Definition: core.hpp:1688
Definition: core.hpp:4465
float fCT
Definition: background_segm.hpp:178
int smoothingRadius
Smoothing radius, in pixels, for cleaning up FG image.
Definition: background_segm.hpp:242
int history
Definition: background_segm.hpp:144
Base class for high-level OpenCV algorithms.
Definition: core.hpp:4390
float fTau
Definition: background_segm.hpp:186
int maxFeatures
Total number of distinct colors to maintain in histogram.
Definition: background_segm.hpp:230
float varThresholdGen
Definition: background_segm.hpp:164
double decisionThreshold
Value above which pixel is determined to be FG.
Definition: background_segm.hpp:240
Proxy datatype for passing Mat's and vector<>'s as input parameters.
Definition: core.hpp:1400
double noiseSigma
Definition: background_segm.hpp:107
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst)
computes per-element maximum of two arrays (dst = max(src1, src2))