opencv 2.2.0
/usr/src/RPM/BUILD/libopencv2.2-2.2.0/modules/video/include/opencv2/video/background_segm.hpp
Go to the documentation of this file.
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 
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #endif
00051 
00052 /****************************************************************************************\
00053 *                           Background/foreground segmentation                           *
00054 \****************************************************************************************/
00055 
00056 /* We discriminate between foreground and background pixels
00057  * by building and maintaining a model of the background.
00058  * Any pixel which does not fit this model is then deemed
00059  * to be foreground.
00060  *
00061  * At present we support two core background models,
00062  * one of which has two variations:
00063  *
00064  *  o CV_BG_MODEL_FGD: latest and greatest algorithm, described in
00065  *    
00066  *       Foreground Object Detection from Videos Containing Complex Background.
00067  *       Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian. 
00068  *       ACM MM2003 9p
00069  *
00070  *  o CV_BG_MODEL_FGD_SIMPLE:
00071  *       A code comment describes this as a simplified version of the above,
00072  *       but the code is in fact currently identical
00073  *
00074  *  o CV_BG_MODEL_MOG: "Mixture of Gaussians", older algorithm, described in
00075  *
00076  *       Moving target classification and tracking from real-time video.
00077  *       A Lipton, H Fujijoshi, R Patil
00078  *       Proceedings IEEE Workshop on Application of Computer Vision pp 8-14 1998
00079  *
00080  *       Learning patterns of activity using real-time tracking
00081  *       C Stauffer and W Grimson  August 2000
00082  *       IEEE Transactions on Pattern Analysis and Machine Intelligence 22(8):747-757
00083  */
00084 
00085 
00086 #define CV_BG_MODEL_FGD         0
00087 #define CV_BG_MODEL_MOG         1                       /* "Mixture of Gaussians".      */
00088 #define CV_BG_MODEL_FGD_SIMPLE  2
00089 
00090 struct CvBGStatModel;
00091 
00092 typedef void (CV_CDECL * CvReleaseBGStatModel)( struct CvBGStatModel** bg_model );
00093 typedef int (CV_CDECL * CvUpdateBGStatModel)( IplImage* curr_frame, struct CvBGStatModel* bg_model,
00094                                               double learningRate );
00095 
00096 #define CV_BG_STAT_MODEL_FIELDS()                                                   \
00097     int             type; /*type of BG model*/                                      \
00098     CvReleaseBGStatModel release;                                                   \
00099     CvUpdateBGStatModel update;                                                     \
00100     IplImage*       background;   /*8UC3 reference background image*/               \
00101     IplImage*       foreground;   /*8UC1 foreground image*/                         \
00102     IplImage**      layers;       /*8UC3 reference background image, can be null */ \
00103     int             layer_count;  /* can be zero */                                 \
00104     CvMemStorage*   storage;      /*storage for foreground_regions*/                \
00105     CvSeq*          foreground_regions /*foreground object contours*/
00106 
00107 typedef struct CvBGStatModel
00108 {
00109     CV_BG_STAT_MODEL_FIELDS();
00110 } CvBGStatModel;
00111 
00112 // 
00113 
00114 // Releases memory used by BGStatModel
00115 CVAPI(void) cvReleaseBGStatModel( CvBGStatModel** bg_model );
00116 
00117 // Updates statistical model and returns number of found foreground regions
00118 CVAPI(int) cvUpdateBGStatModel( IplImage* current_frame, CvBGStatModel*  bg_model,
00119                                 double learningRate CV_DEFAULT(-1));
00120 
00121 // Performs FG post-processing using segmentation
00122 // (all pixels of a region will be classified as foreground if majority of pixels of the region are FG).
00123 // parameters:
00124 //      segments - pointer to result of segmentation (for example MeanShiftSegmentation)
00125 //      bg_model - pointer to CvBGStatModel structure
00126 CVAPI(void) cvRefineForegroundMaskBySegm( CvSeq* segments, CvBGStatModel*  bg_model );
00127 
00128 /* Common use change detection function */
00129 CVAPI(int)  cvChangeDetection( IplImage*  prev_frame,
00130                                IplImage*  curr_frame,
00131                                IplImage*  change_mask );
00132 
00133 /*
00134   Interface of ACM MM2003 algorithm
00135 */
00136 
00137 /* Default parameters of foreground detection algorithm: */
00138 #define  CV_BGFG_FGD_LC              128
00139 #define  CV_BGFG_FGD_N1C             15
00140 #define  CV_BGFG_FGD_N2C             25
00141 
00142 #define  CV_BGFG_FGD_LCC             64
00143 #define  CV_BGFG_FGD_N1CC            25
00144 #define  CV_BGFG_FGD_N2CC            40
00145 
00146 /* Background reference image update parameter: */
00147 #define  CV_BGFG_FGD_ALPHA_1         0.1f
00148 
00149 /* stat model update parameter
00150  * 0.002f ~ 1K frame(~45sec), 0.005 ~ 18sec (if 25fps and absolutely static BG)
00151  */
00152 #define  CV_BGFG_FGD_ALPHA_2         0.005f
00153 
00154 /* start value for alpha parameter (to fast initiate statistic model) */
00155 #define  CV_BGFG_FGD_ALPHA_3         0.1f
00156 
00157 #define  CV_BGFG_FGD_DELTA           2
00158 
00159 #define  CV_BGFG_FGD_T               0.9f
00160 
00161 #define  CV_BGFG_FGD_MINAREA         15.f
00162 
00163 #define  CV_BGFG_FGD_BG_UPDATE_TRESH 0.5f
00164 
00165 /* See the above-referenced Li/Huang/Gu/Tian paper
00166  * for a full description of these background-model
00167  * tuning parameters.
00168  *
00169  * Nomenclature:  'c'  == "color", a three-component red/green/blue vector.
00170  *                         We use histograms of these to model the range of
00171  *                         colors we've seen at a given background pixel.
00172  *
00173  *                'cc' == "color co-occurrence", a six-component vector giving
00174  *                         RGB color for both this frame and preceding frame.
00175  *                             We use histograms of these to model the range of
00176  *                         color CHANGES we've seen at a given background pixel.
00177  */
00178 typedef struct CvFGDStatModelParams
00179 {
00180     int    Lc;                  /* Quantized levels per 'color' component. Power of two, typically 32, 64 or 128.                               */
00181     int    N1c;                 /* Number of color vectors used to model normal background color variation at a given pixel.                    */
00182     int    N2c;                 /* Number of color vectors retained at given pixel.  Must be > N1c, typically ~ 5/3 of N1c.                     */
00183                                 /* Used to allow the first N1c vectors to adapt over time to changing background.                               */
00184 
00185     int    Lcc;                 /* Quantized levels per 'color co-occurrence' component.  Power of two, typically 16, 32 or 64.                 */
00186     int    N1cc;                /* Number of color co-occurrence vectors used to model normal background color variation at a given pixel.      */
00187     int    N2cc;                /* Number of color co-occurrence vectors retained at given pixel.  Must be > N1cc, typically ~ 5/3 of N1cc.     */
00188                                 /* Used to allow the first N1cc vectors to adapt over time to changing background.                              */
00189 
00190     int    is_obj_without_holes;/* If TRUE we ignore holes within foreground blobs. Defaults to TRUE.                                           */
00191     int    perform_morphing;    /* Number of erode-dilate-erode foreground-blob cleanup iterations.                                             */
00192                                 /* These erase one-pixel junk blobs and merge almost-touching blobs. Default value is 1.                        */
00193 
00194     float  alpha1;              /* How quickly we forget old background pixel values seen.  Typically set to 0.1                                */
00195     float  alpha2;              /* "Controls speed of feature learning". Depends on T. Typical value circa 0.005.                               */
00196     float  alpha3;              /* Alternate to alpha2, used (e.g.) for quicker initial convergence. Typical value 0.1.                         */
00197 
00198     float  delta;               /* Affects color and color co-occurrence quantization, typically set to 2.                                      */
00199     float  T;                   /* "A percentage value which determines when new features can be recognized as new background." (Typically 0.9).*/
00200     float  minArea;             /* Discard foreground blobs whose bounding box is smaller than this threshold.                                  */
00201 } CvFGDStatModelParams;
00202 
00203 typedef struct CvBGPixelCStatTable
00204 {
00205     float          Pv, Pvb;
00206     uchar          v[3];
00207 } CvBGPixelCStatTable;
00208 
00209 typedef struct CvBGPixelCCStatTable
00210 {
00211     float          Pv, Pvb;
00212     uchar          v[6];
00213 } CvBGPixelCCStatTable;
00214 
00215 typedef struct CvBGPixelStat
00216 {
00217     float                 Pbc;
00218     float                 Pbcc;
00219     CvBGPixelCStatTable*  ctable;
00220     CvBGPixelCCStatTable* cctable;
00221     uchar                 is_trained_st_model;
00222     uchar                 is_trained_dyn_model;
00223 } CvBGPixelStat;
00224 
00225 
00226 typedef struct CvFGDStatModel
00227 {
00228     CV_BG_STAT_MODEL_FIELDS();
00229     CvBGPixelStat*         pixel_stat;
00230     IplImage*              Ftd;
00231     IplImage*              Fbd;
00232     IplImage*              prev_frame;
00233     CvFGDStatModelParams   params;
00234 } CvFGDStatModel;
00235 
00236 /* Creates FGD model */
00237 CVAPI(CvBGStatModel*) cvCreateFGDStatModel( IplImage* first_frame,
00238                     CvFGDStatModelParams* parameters CV_DEFAULT(NULL));
00239 
00240 /* 
00241    Interface of Gaussian mixture algorithm
00242 
00243    "An improved adaptive background mixture model for real-time tracking with shadow detection"
00244    P. KadewTraKuPong and R. Bowden,
00245    Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
00246    http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
00247 */
00248 
00249 /* Note:  "MOG" == "Mixture Of Gaussians": */
00250 
00251 #define CV_BGFG_MOG_MAX_NGAUSSIANS 500
00252 
00253 /* default parameters of gaussian background detection algorithm */
00254 #define CV_BGFG_MOG_BACKGROUND_THRESHOLD     0.7     /* threshold sum of weights for background test */
00255 #define CV_BGFG_MOG_STD_THRESHOLD            2.5     /* lambda=2.5 is 99% */
00256 #define CV_BGFG_MOG_WINDOW_SIZE              200     /* Learning rate; alpha = 1/CV_GBG_WINDOW_SIZE */
00257 #define CV_BGFG_MOG_NGAUSSIANS               5       /* = K = number of Gaussians in mixture */
00258 #define CV_BGFG_MOG_WEIGHT_INIT              0.05
00259 #define CV_BGFG_MOG_SIGMA_INIT               30
00260 #define CV_BGFG_MOG_MINAREA                  15.f
00261 
00262 
00263 #define CV_BGFG_MOG_NCOLORS                  3
00264 
00265 typedef struct CvGaussBGStatModelParams
00266 {    
00267     int     win_size;               /* = 1/alpha */
00268     int     n_gauss;
00269     double  bg_threshold, std_threshold, minArea;
00270     double  weight_init, variance_init;
00271 }CvGaussBGStatModelParams;
00272 
00273 typedef struct CvGaussBGValues
00274 {
00275     int         match_sum;
00276     double      weight;
00277     double      variance[CV_BGFG_MOG_NCOLORS];
00278     double      mean[CV_BGFG_MOG_NCOLORS];
00279 } CvGaussBGValues;
00280 
00281 typedef struct CvGaussBGPoint
00282 {
00283     CvGaussBGValues* g_values;
00284 } CvGaussBGPoint;
00285 
00286 
00287 typedef struct CvGaussBGModel
00288 {
00289     CV_BG_STAT_MODEL_FIELDS();
00290     CvGaussBGStatModelParams   params;    
00291     CvGaussBGPoint*            g_point;    
00292     int                        countFrames;
00293 } CvGaussBGModel;
00294 
00295 
00296 /* Creates Gaussian mixture background model */
00297 CVAPI(CvBGStatModel*) cvCreateGaussianBGModel( IplImage* first_frame,
00298                 CvGaussBGStatModelParams* parameters CV_DEFAULT(NULL));
00299 
00300 
00301 typedef struct CvBGCodeBookElem
00302 {
00303     struct CvBGCodeBookElem* next;
00304     int tLastUpdate;
00305     int stale;
00306     uchar boxMin[3];
00307     uchar boxMax[3];
00308     uchar learnMin[3];
00309     uchar learnMax[3];
00310 } CvBGCodeBookElem;
00311 
00312 typedef struct CvBGCodeBookModel
00313 {
00314     CvSize size;
00315     int t;
00316     uchar cbBounds[3];
00317     uchar modMin[3];
00318     uchar modMax[3];
00319     CvBGCodeBookElem** cbmap;
00320     CvMemStorage* storage;
00321     CvBGCodeBookElem* freeList;
00322 } CvBGCodeBookModel;
00323 
00324 CVAPI(CvBGCodeBookModel*) cvCreateBGCodeBookModel();
00325 CVAPI(void) cvReleaseBGCodeBookModel( CvBGCodeBookModel** model );
00326 
00327 CVAPI(void) cvBGCodeBookUpdate( CvBGCodeBookModel* model, const CvArr* image,
00328                                 CvRect roi CV_DEFAULT(cvRect(0,0,0,0)),
00329                                 const CvArr* mask CV_DEFAULT(0) );
00330 
00331 CVAPI(int) cvBGCodeBookDiff( const CvBGCodeBookModel* model, const CvArr* image,
00332                              CvArr* fgmask, CvRect roi CV_DEFAULT(cvRect(0,0,0,0)) );
00333 
00334 CVAPI(void) cvBGCodeBookClearStale( CvBGCodeBookModel* model, int staleThresh,
00335                                     CvRect roi CV_DEFAULT(cvRect(0,0,0,0)),
00336                                     const CvArr* mask CV_DEFAULT(0) );
00337 
00338 CVAPI(CvSeq*) cvSegmentFGMask( CvArr *fgmask, int poly1Hull0 CV_DEFAULT(1),
00339                                float perimScale CV_DEFAULT(4.f),
00340                                CvMemStorage* storage CV_DEFAULT(0),
00341                                CvPoint offset CV_DEFAULT(cvPoint(0,0)));
00342 
00343 #ifdef __cplusplus
00344 }
00345 
00346 namespace cv
00347 {
00348 
00355 class CV_EXPORTS_W BackgroundSubtractor
00356 {
00357 public:
00359     virtual ~BackgroundSubtractor();
00361     CV_WRAP_AS(apply) virtual void operator()(const Mat& image, CV_OUT Mat& fgmask,
00362                                               double learningRate=0);
00363 };
00364 
00365 
00376 class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor
00377 {
00378 public:
00380     CV_WRAP BackgroundSubtractorMOG();
00382     CV_WRAP BackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma=0);
00384     virtual ~BackgroundSubtractorMOG();
00386     virtual void operator()(const Mat& image, Mat& fgmask, double learningRate=0);
00387     
00389     virtual void initialize(Size frameSize, int frameType);
00390     
00391     Size frameSize;
00392     int frameType;
00393     Mat bgmodel;
00394     int nframes;
00395     int history;
00396     int nmixtures;
00397     double varThreshold;
00398     double backgroundRatio;
00399     double noiseSigma;
00400 };      
00401 
00402 
00403 class CV_EXPORTS_W BackgroundSubtractorMOG2 : public BackgroundSubtractor
00404 {
00405 public:
00407     CV_WRAP BackgroundSubtractorMOG2();
00409     CV_WRAP BackgroundSubtractorMOG2(double alphaT,
00410                                      double sigma=15,
00411                                      int nmixtures=5,
00412                                      bool postFiltering=false,
00413                                      double minArea=15,
00414                                      bool detectShadows=true,
00415                                      bool removeForeground=false,
00416                                      
00417                                      double Tb=16,
00418                                      double Tg=9,
00419                                      double TB=0.9,
00420                                      double CT=0.05,
00421                                      
00422                                      int shadowOutputValue=127,
00423                                      double tau=0.5);
00424     
00426     virtual ~BackgroundSubtractorMOG2();
00428     virtual void operator()(const Mat& image, Mat& fgmask, double learningRate=0);
00429     
00431     virtual void initialize(Size frameSize,
00432                             double alphaT,
00433                             double sigma=15,
00434                             int nmixtures=5,
00435                             bool postFiltering=false,
00436                             double minArea=15,
00437                             bool detectShadows=true,
00438                             bool removeForeground=false,
00439                             
00440                             double Tb=16,
00441                             double Tg=9,
00442                             double TB=0.9,
00443                             double CT=0.05,
00444                             
00445                             int nShadowDetection=127,
00446                             double tau=0.5);
00447     
00448     void* model;
00449 };          
00450     
00451 }
00452 #endif
00453 
00454 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines