opencv  2.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /****************************************************************************************\
53 * Background/foreground segmentation *
54 \****************************************************************************************/
55 
56 /* We discriminate between foreground and background pixels
57  * by building and maintaining a model of the background.
58  * Any pixel which does not fit this model is then deemed
59  * to be foreground.
60  *
61  * At present we support two core background models,
62  * one of which has two variations:
63  *
64  * o CV_BG_MODEL_FGD: latest and greatest algorithm, described in
65  *
66  * Foreground Object Detection from Videos Containing Complex Background.
67  * Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian.
68  * ACM MM2003 9p
69  *
70  * o CV_BG_MODEL_FGD_SIMPLE:
71  * A code comment describes this as a simplified version of the above,
72  * but the code is in fact currently identical
73  *
74  * o CV_BG_MODEL_MOG: "Mixture of Gaussians", older algorithm, described in
75  *
76  * Moving target classification and tracking from real-time video.
77  * A Lipton, H Fujijoshi, R Patil
78  * Proceedings IEEE Workshop on Application of Computer Vision pp 8-14 1998
79  *
80  * Learning patterns of activity using real-time tracking
81  * C Stauffer and W Grimson August 2000
82  * IEEE Transactions on Pattern Analysis and Machine Intelligence 22(8):747-757
83  */
84 
85 
86 #define CV_BG_MODEL_FGD 0
87 #define CV_BG_MODEL_MOG 1 /* "Mixture of Gaussians". */
88 #define CV_BG_MODEL_FGD_SIMPLE 2
89 
90 struct CvBGStatModel;
91 
92 typedef void (CV_CDECL * CvReleaseBGStatModel)( struct CvBGStatModel** bg_model );
93 typedef int (CV_CDECL * CvUpdateBGStatModel)( IplImage* curr_frame, struct CvBGStatModel* bg_model,
94  double learningRate );
95 
96 #define CV_BG_STAT_MODEL_FIELDS() \
97  int type; /*type of BG model*/ \
98  CvReleaseBGStatModel release; \
99  CvUpdateBGStatModel update; \
100  IplImage* background; /*8UC3 reference background image*/ \
101  IplImage* foreground; /*8UC1 foreground image*/ \
102  IplImage** layers; /*8UC3 reference background image, can be null */ \
103  int layer_count; /* can be zero */ \
104  CvMemStorage* storage; /*storage for foreground_regions*/ \
105  CvSeq* foreground_regions /*foreground object contours*/
106 
107 typedef struct CvBGStatModel
108 {
110 } CvBGStatModel;
111 
112 //
113 
114 // Releases memory used by BGStatModel
115 CVAPI(void) cvReleaseBGStatModel( CvBGStatModel** bg_model );
116 
117 // Updates statistical model and returns number of found foreground regions
118 CVAPI(int) cvUpdateBGStatModel( IplImage* current_frame, CvBGStatModel* bg_model,
119  double learningRate CV_DEFAULT(-1));
120 
121 // Performs FG post-processing using segmentation
122 // (all pixels of a region will be classified as foreground if majority of pixels of the region are FG).
123 // parameters:
124 // segments - pointer to result of segmentation (for example MeanShiftSegmentation)
125 // bg_model - pointer to CvBGStatModel structure
126 CVAPI(void) cvRefineForegroundMaskBySegm( CvSeq* segments, CvBGStatModel* bg_model );
127 
128 /* Common use change detection function */
129 CVAPI(int) cvChangeDetection( IplImage* prev_frame,
130  IplImage* curr_frame,
131  IplImage* change_mask );
132 
133 /*
134  Interface of ACM MM2003 algorithm
135 */
136 
137 /* Default parameters of foreground detection algorithm: */
138 #define CV_BGFG_FGD_LC 128
139 #define CV_BGFG_FGD_N1C 15
140 #define CV_BGFG_FGD_N2C 25
141 
142 #define CV_BGFG_FGD_LCC 64
143 #define CV_BGFG_FGD_N1CC 25
144 #define CV_BGFG_FGD_N2CC 40
145 
146 /* Background reference image update parameter: */
147 #define CV_BGFG_FGD_ALPHA_1 0.1f
148 
149 /* stat model update parameter
150  * 0.002f ~ 1K frame(~45sec), 0.005 ~ 18sec (if 25fps and absolutely static BG)
151  */
152 #define CV_BGFG_FGD_ALPHA_2 0.005f
153 
154 /* start value for alpha parameter (to fast initiate statistic model) */
155 #define CV_BGFG_FGD_ALPHA_3 0.1f
156 
157 #define CV_BGFG_FGD_DELTA 2
158 
159 #define CV_BGFG_FGD_T 0.9f
160 
161 #define CV_BGFG_FGD_MINAREA 15.f
162 
163 #define CV_BGFG_FGD_BG_UPDATE_TRESH 0.5f
164 
165 /* See the above-referenced Li/Huang/Gu/Tian paper
166  * for a full description of these background-model
167  * tuning parameters.
168  *
169  * Nomenclature: 'c' == "color", a three-component red/green/blue vector.
170  * We use histograms of these to model the range of
171  * colors we've seen at a given background pixel.
172  *
173  * 'cc' == "color co-occurrence", a six-component vector giving
174  * RGB color for both this frame and preceding frame.
175  * We use histograms of these to model the range of
176  * color CHANGES we've seen at a given background pixel.
177  */
178 typedef struct CvFGDStatModelParams
179 {
180  int Lc; /* Quantized levels per 'color' component. Power of two, typically 32, 64 or 128. */
181  int N1c; /* Number of color vectors used to model normal background color variation at a given pixel. */
182  int N2c; /* Number of color vectors retained at given pixel. Must be > N1c, typically ~ 5/3 of N1c. */
183  /* Used to allow the first N1c vectors to adapt over time to changing background. */
184 
185  int Lcc; /* Quantized levels per 'color co-occurrence' component. Power of two, typically 16, 32 or 64. */
186  int N1cc; /* Number of color co-occurrence vectors used to model normal background color variation at a given pixel. */
187  int N2cc; /* Number of color co-occurrence vectors retained at given pixel. Must be > N1cc, typically ~ 5/3 of N1cc. */
188  /* Used to allow the first N1cc vectors to adapt over time to changing background. */
189 
190  int is_obj_without_holes;/* If TRUE we ignore holes within foreground blobs. Defaults to TRUE. */
191  int perform_morphing; /* Number of erode-dilate-erode foreground-blob cleanup iterations. */
192  /* These erase one-pixel junk blobs and merge almost-touching blobs. Default value is 1. */
193 
194  float alpha1; /* How quickly we forget old background pixel values seen. Typically set to 0.1 */
195  float alpha2; /* "Controls speed of feature learning". Depends on T. Typical value circa 0.005. */
196  float alpha3; /* Alternate to alpha2, used (e.g.) for quicker initial convergence. Typical value 0.1. */
197 
198  float delta; /* Affects color and color co-occurrence quantization, typically set to 2. */
199  float T; /* "A percentage value which determines when new features can be recognized as new background." (Typically 0.9).*/
200  float minArea; /* Discard foreground blobs whose bounding box is smaller than this threshold. */
202 
203 typedef struct CvBGPixelCStatTable
204 {
205  float Pv, Pvb;
206  uchar v[3];
208 
209 typedef struct CvBGPixelCCStatTable
210 {
211  float Pv, Pvb;
212  uchar v[6];
214 
215 typedef struct CvBGPixelStat
216 {
217  float Pbc;
218  float Pbcc;
223 } CvBGPixelStat;
224 
225 
226 typedef struct CvFGDStatModel
227 {
235 
236 /* Creates FGD model */
237 CVAPI(CvBGStatModel*) cvCreateFGDStatModel( IplImage* first_frame,
238  CvFGDStatModelParams* parameters CV_DEFAULT(NULL));
239 
240 /*
241  Interface of Gaussian mixture algorithm
242 
243  "An improved adaptive background mixture model for real-time tracking with shadow detection"
244  P. KadewTraKuPong and R. Bowden,
245  Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
246  http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
247 */
248 
249 /* Note: "MOG" == "Mixture Of Gaussians": */
250 
251 #define CV_BGFG_MOG_MAX_NGAUSSIANS 500
252 
253 /* default parameters of gaussian background detection algorithm */
254 #define CV_BGFG_MOG_BACKGROUND_THRESHOLD 0.7 /* threshold sum of weights for background test */
255 #define CV_BGFG_MOG_STD_THRESHOLD 2.5 /* lambda=2.5 is 99% */
256 #define CV_BGFG_MOG_WINDOW_SIZE 200 /* Learning rate; alpha = 1/CV_GBG_WINDOW_SIZE */
257 #define CV_BGFG_MOG_NGAUSSIANS 5 /* = K = number of Gaussians in mixture */
258 #define CV_BGFG_MOG_WEIGHT_INIT 0.05
259 #define CV_BGFG_MOG_SIGMA_INIT 30
260 #define CV_BGFG_MOG_MINAREA 15.f
261 
262 
263 #define CV_BGFG_MOG_NCOLORS 3
264 
266 {
267  int win_size; /* = 1/alpha */
268  int n_gauss;
272 
273 typedef struct CvGaussBGValues
274 {
276  double weight;
280 
281 typedef struct CvGaussBGPoint
282 {
285 
286 
287 typedef struct CvGaussBGModel
288 {
294 
295 
296 /* Creates Gaussian mixture background model */
297 CVAPI(CvBGStatModel*) cvCreateGaussianBGModel( IplImage* first_frame,
298  CvGaussBGStatModelParams* parameters CV_DEFAULT(NULL));
299 
300 
301 typedef struct CvBGCodeBookElem
302 {
303  struct CvBGCodeBookElem* next;
305  int stale;
311 
312 typedef struct CvBGCodeBookModel
313 {
315  int t;
323 
325 CVAPI(void) cvReleaseBGCodeBookModel( CvBGCodeBookModel** model );
326 
327 CVAPI(void) cvBGCodeBookUpdate( CvBGCodeBookModel* model, const CvArr* image,
328  CvRect roi CV_DEFAULT(cvRect(0,0,0,0)),
329  const CvArr* mask CV_DEFAULT(0) );
330 
331 CVAPI(int) cvBGCodeBookDiff( const CvBGCodeBookModel* model, const CvArr* image,
332  CvArr* fgmask, CvRect roi CV_DEFAULT(cvRect(0,0,0,0)) );
333 
334 CVAPI(void) cvBGCodeBookClearStale( CvBGCodeBookModel* model, int staleThresh,
335  CvRect roi CV_DEFAULT(cvRect(0,0,0,0)),
336  const CvArr* mask CV_DEFAULT(0) );
337 
338 CVAPI(CvSeq*) cvSegmentFGMask( CvArr *fgmask, int poly1Hull0 CV_DEFAULT(1),
339  float perimScale CV_DEFAULT(4.f),
340  CvMemStorage* storage CV_DEFAULT(0),
341  CvPoint offset CV_DEFAULT(cvPoint(0,0)));
342 
343 #ifdef __cplusplus
344 }
345 
346 namespace cv
347 {
348 
356 {
357 public:
359  virtual ~BackgroundSubtractor();
361  CV_WRAP_AS(apply) virtual void operator()(const Mat& image, CV_OUT Mat& fgmask,
362  double learningRate=0);
363 };
364 
365 
377 {
378 public:
382  CV_WRAP BackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma=0);
384  virtual ~BackgroundSubtractorMOG();
386  virtual void operator()(const Mat& image, Mat& fgmask, double learningRate=0);
387 
389  virtual void initialize(Size frameSize, int frameType);
390 
394  int nframes;
395  int history;
397  double varThreshold;
399  double noiseSigma;
400 };
401 
402 }
403 #endif
404 
405 #endif