1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
21 // * Redistribution's in binary form must reproduce the above copyright notice,
22 // this list of conditions and the following disclaimer in the documentation
23 // and/or other materials provided with the distribution.
25 // * The name of Intel Corporation may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
42 // Function cvCreateBGStatModel creates and returns initialized BG model.
44 // first_frame - frame from video sequence
45 // model_type
\96 type of BG model (CV_BG_MODEL_MOG, CV_BG_MODEL_FGD,
\85)
46 // parameters - (optional) if NULL the default parameters of the algorithm will be used
47 CvBGStatModel* cvCreateBGStatModel( IplImage* first_frame, int model_type, void* params )
49 CvBGStatModel* bg_model = NULL;
51 if( model_type == CV_BG_MODEL_FGD || model_type == CV_BG_MODEL_FGD_SIMPLE )
52 bg_model = cvCreateFGDStatModel( first_frame, (CvFGDStatModelParams*)params );
53 else if( model_type == CV_BG_MODEL_MOG )
54 bg_model = cvCreateGaussianBGModel( first_frame, (CvGaussBGStatModelParams*)params );
60 /* FOREGROUND DETECTOR INTERFACE */
61 class CvFGDetectorBase:public CvFGDetector
66 void* m_pFGParam; /* Foreground parameters. */
67 CvFGDStatModelParams m_ParamFGD;
68 CvGaussBGStatModelParams m_ParamMOG;
72 virtual void SaveState(CvFileStorage* )
74 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
76 if( m_SaveName ) /* File name is not empty */
78 //cvSaveStatModel(m_SaveName, (CvFGDStatModel*)m_pFG);
82 virtual void LoadState(CvFileStorage* , CvFileNode* )
84 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
86 if( m_LoadName ) /* File name is not empty */
88 //cvRestoreStatModel(m_LoadName, (CvFGDStatModel*)m_pFG);
92 CvFGDetectorBase(int type, void* param)
97 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
101 m_ParamFGD = *(CvFGDStatModelParams*)m_pFGParam;
105 m_ParamFGD.Lc = CV_BGFG_FGD_LC;
106 m_ParamFGD.N1c = CV_BGFG_FGD_N1C;
107 m_ParamFGD.N2c = CV_BGFG_FGD_N2C;
108 m_ParamFGD.Lcc = CV_BGFG_FGD_LCC;
109 m_ParamFGD.N1cc = CV_BGFG_FGD_N1CC;
110 m_ParamFGD.N2cc = CV_BGFG_FGD_N2CC;
111 m_ParamFGD.delta = CV_BGFG_FGD_DELTA;
112 m_ParamFGD.alpha1 = CV_BGFG_FGD_ALPHA_1;
113 m_ParamFGD.alpha2 = CV_BGFG_FGD_ALPHA_2;
114 m_ParamFGD.alpha3 = CV_BGFG_FGD_ALPHA_3;
115 m_ParamFGD.T = CV_BGFG_FGD_T;
116 m_ParamFGD.minArea = CV_BGFG_FGD_MINAREA;
117 m_ParamFGD.is_obj_without_holes = 1;
118 m_ParamFGD.perform_morphing = 1;
120 AddParam("LC",&m_ParamFGD.Lc);
121 AddParam("alpha1",&m_ParamFGD.alpha1);
122 AddParam("alpha2",&m_ParamFGD.alpha2);
123 AddParam("alpha3",&m_ParamFGD.alpha3);
124 AddParam("N1c",&m_ParamFGD.N1c);
125 AddParam("N2c",&m_ParamFGD.N2c);
126 AddParam("N1cc",&m_ParamFGD.N1cc);
127 AddParam("N2cc",&m_ParamFGD.N2cc);
130 AddParam("SaveName",&m_SaveName);
131 AddParam("LoadName",&m_LoadName);
132 AddParam("ObjWithoutHoles",&m_ParamFGD.is_obj_without_holes);
133 AddParam("Morphology",&m_ParamFGD.perform_morphing);
135 else if( m_FGType == CV_BG_MODEL_MOG ) // "MOG" == "Mixture Of Gaussians"
139 m_ParamMOG = *(CvGaussBGStatModelParams*)m_pFGParam;
142 { // These constants are all from cvaux/include/cvaux.h
143 m_ParamMOG.win_size = CV_BGFG_MOG_WINDOW_SIZE;
144 m_ParamMOG.bg_threshold = CV_BGFG_MOG_BACKGROUND_THRESHOLD;
146 m_ParamMOG.std_threshold = CV_BGFG_MOG_STD_THRESHOLD;
147 m_ParamMOG.weight_init = CV_BGFG_MOG_WEIGHT_INIT;
149 m_ParamMOG.variance_init = CV_BGFG_MOG_SIGMA_INIT*CV_BGFG_MOG_SIGMA_INIT;
150 m_ParamMOG.minArea = CV_BGFG_MOG_MINAREA;
151 m_ParamMOG.n_gauss = CV_BGFG_MOG_NGAUSSIANS;
153 AddParam("NG",&m_ParamMOG.n_gauss);
159 if(m_pFG)cvReleaseBGStatModel( &m_pFG );
163 if(m_pFG)cvReleaseBGStatModel( &m_pFG );
166 inline IplImage* GetMask()
168 return m_pFG?m_pFG->foreground:NULL;
171 /* Process current image: */
172 virtual void Process(IplImage* pImg)
176 void* param = m_pFGParam;
177 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
181 else if( m_FGType == CV_BG_MODEL_MOG )
185 m_pFG = cvCreateBGStatModel(
193 cvUpdateBGStatModel( pImg, m_pFG );
197 /* Release foreground detector: */
198 virtual void Release()
201 if(m_pFG)cvReleaseBGStatModel( &m_pFG );
205 CvFGDetector* cvCreateFGDetectorBase(int type, void *param)
207 return (CvFGDetector*) new CvFGDetectorBase(type, param);