00001 /*M/////////////////////////////////////////////////////////////////////////////////////// 00002 // 00003 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 00004 // 00005 // By downloading, copying, installing or using the software you agree to this license. 00006 // If you do not agree to this license, do not download, install, 00007 // copy or use the software. 00008 // 00009 // 00010 // License Agreement 00011 // For Open Source Computer Vision Library 00012 // 00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 00015 // Third party copyrights are property of their respective owners. 00016 // 00017 // Redistribution and use in source and binary forms, with or without modification, 00018 // are permitted provided that the following conditions are met: 00019 // 00020 // * Redistribution's of source code must retain the above copyright notice, 00021 // this list of conditions and the following disclaimer. 00022 // 00023 // * Redistribution's in binary form must reproduce the above copyright notice, 00024 // this list of conditions and the following disclaimer in the documentation 00025 // and/or other materials provided with the distribution. 00026 // 00027 // * The name of the copyright holders may not be used to endorse or promote products 00028 // derived from this software without specific prior written permission. 00029 // 00030 // This software is provided by the copyright holders and contributors "as is" and 00031 // any express or implied warranties, including, but not limited to, the implied 00032 // warranties of merchantability and fitness for a particular purpose are disclaimed. 00033 // In no event shall the Intel Corporation or contributors be liable for any direct, 00034 // indirect, incidental, special, exemplary, or consequential damages 00035 // (including, but not limited to, procurement of substitute goods or services; 00036 // loss of use, data, or profits; or business interruption) however caused 00037 // and on any theory of liability, whether in contract, strict liability, 00038 // or tort (including negligence or otherwise) arising in any way out of 00039 // the use of this software, even if advised of the possibility of such damage. 00040 // 00041 //M*/ 00042 00043 #ifndef __OPENCV_BACKGROUND_SEGM_HPP__ 00044 #define __OPENCV_BACKGROUND_SEGM_HPP__ 00045 00046 #include "opencv2/core/core.hpp" 00047 #include <list> 00048 namespace cv 00049 { 00050 00057 class CV_EXPORTS_W BackgroundSubtractor : public Algorithm 00058 { 00059 public: 00061 virtual ~BackgroundSubtractor(); 00063 CV_WRAP_AS(apply) virtual void operator()(InputArray image, OutputArray fgmask, 00064 double learningRate=0); 00065 00067 virtual void getBackgroundImage(OutputArray backgroundImage) const; 00068 }; 00069 00070 00081 class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor 00082 { 00083 public: 00085 CV_WRAP BackgroundSubtractorMOG(); 00087 CV_WRAP BackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma=0); 00089 virtual ~BackgroundSubtractorMOG(); 00091 virtual void operator()(InputArray image, OutputArray fgmask, double learningRate=0); 00092 00094 virtual void initialize(Size frameSize, int frameType); 00095 00096 virtual AlgorithmInfo* info() const; 00097 00098 protected: 00099 Size frameSize; 00100 int frameType; 00101 Mat bgmodel; 00102 int nframes; 00103 int history; 00104 int nmixtures; 00105 double varThreshold; 00106 double backgroundRatio; 00107 double noiseSigma; 00108 }; 00109 00110 00118 class CV_EXPORTS BackgroundSubtractorMOG2 : public BackgroundSubtractor 00119 { 00120 public: 00122 BackgroundSubtractorMOG2(); 00124 BackgroundSubtractorMOG2(int history, float varThreshold, bool bShadowDetection=true); 00126 virtual ~BackgroundSubtractorMOG2(); 00128 virtual void operator()(InputArray image, OutputArray fgmask, double learningRate=-1); 00129 00131 virtual void getBackgroundImage(OutputArray backgroundImage) const; 00132 00134 virtual void initialize(Size frameSize, int frameType); 00135 00136 virtual AlgorithmInfo* info() const; 00137 00138 protected: 00139 Size frameSize; 00140 int frameType; 00141 Mat bgmodel; 00142 Mat bgmodelUsedModes;//keep track of number of modes per pixel 00143 int nframes; 00144 int history; 00145 int nmixtures; 00148 double varThreshold; 00149 // threshold on the squared Mahalanobis distance to decide if it is well described 00150 // by the background model or not. Related to Cthr from the paper. 00151 // This does not influence the update of the background. A typical value could be 4 sigma 00152 // and that is varThreshold=4*4=16; Corresponds to Tb in the paper. 00153 00155 // less important parameters - things you might change but be carefull 00157 float backgroundRatio; 00158 // corresponds to fTB=1-cf from the paper 00159 // TB - threshold when the component becomes significant enough to be included into 00160 // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0. 00161 // For alpha=0.001 it means that the mode should exist for approximately 105 frames before 00162 // it is considered foreground 00163 // float noiseSigma; 00164 float varThresholdGen; 00165 //correspondts to Tg - threshold on the squared Mahalan. dist. to decide 00166 //when a sample is close to the existing components. If it is not close 00167 //to any a new component will be generated. I use 3 sigma => Tg=3*3=9. 00168 //Smaller Tg leads to more generated components and higher Tg might make 00169 //lead to small number of components but they can grow too large 00170 float fVarInit; 00171 float fVarMin; 00172 float fVarMax; 00173 //initial variance for the newly generated components. 00174 //It will will influence the speed of adaptation. A good guess should be made. 00175 //A simple way is to estimate the typical standard deviation from the images. 00176 //I used here 10 as a reasonable value 00177 // min and max can be used to further control the variance 00178 float fCT;//CT - complexity reduction prior 00179 //this is related to the number of samples needed to accept that a component 00180 //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get 00181 //the standard Stauffer&Grimson algorithm (maybe not exact but very similar) 00182 00183 //shadow detection parameters 00184 bool bShadowDetection;//default 1 - do shadow detection 00185 unsigned char nShadowDetection;//do shadow detection - insert this value as the detection result - 127 default value 00186 float fTau; 00187 // Tau - shadow threshold. The shadow is detected if the pixel is darker 00188 //version of the background. Tau is a threshold on how much darker the shadow can be. 00189 //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow 00190 //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003. 00191 }; 00192 00200 class CV_EXPORTS BackgroundSubtractorGMG: public cv::BackgroundSubtractor 00201 { 00202 public: 00203 BackgroundSubtractorGMG(); 00204 virtual ~BackgroundSubtractorGMG(); 00205 virtual AlgorithmInfo* info() const; 00206 00214 void initialize(cv::Size frameSize, double min, double max); 00215 00222 virtual void operator()(InputArray image, OutputArray fgmask, double learningRate=-1.0); 00223 00227 void release(); 00228 00230 int maxFeatures; 00232 double learningRate; 00234 int numInitializationFrames; 00236 int quantizationLevels; 00238 double backgroundPrior; 00240 double decisionThreshold; 00242 int smoothingRadius; 00244 bool updateBackgroundModel; 00245 00246 private: 00247 double maxVal_; 00248 double minVal_; 00249 00250 cv::Size frameSize_; 00251 int frameNum_; 00252 00253 cv::Mat_<int> nfeatures_; 00254 cv::Mat_<unsigned int> colors_; 00255 cv::Mat_<float> weights_; 00256 00257 cv::Mat buf_; 00258 }; 00259 00260 } 00261 00262 #endif