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.
40 #include "precomp.hpp"
42 // Function cvCreateBGStatModel creates and returns initialized BG model.
44 // first_frame - frame from video sequence
45 // model_type ñ type of BG model (CV_BG_MODEL_MOG, CV_BG_MODEL_FGD,Ö)
46 // parameters - (optional) if NULL the default parameters of the algorithm will be used
47 static 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 );
59 /* FOREGROUND DETECTOR INTERFACE */
60 class CvFGDetectorBase : public CvFGDetector
65 void* m_pFGParam; /* Foreground parameters. */
66 CvFGDStatModelParams m_ParamFGD;
67 CvGaussBGStatModelParams m_ParamMOG;
68 const char* m_SaveName;
69 const char* m_LoadName;
71 virtual void SaveState(CvFileStorage* )
73 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
75 if( m_SaveName ) /* File name is not empty */
77 //cvSaveStatModel(m_SaveName, (CvFGDStatModel*)m_pFG);
81 virtual void LoadState(CvFileStorage* , CvFileNode* )
83 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
85 if( m_LoadName ) /* File name is not empty */
87 //cvRestoreStatModel(m_LoadName, (CvFGDStatModel*)m_pFG);
91 CvFGDetectorBase(int type, void* param)
96 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
100 m_ParamFGD = *(CvFGDStatModelParams*)m_pFGParam;
104 m_ParamFGD.Lc = CV_BGFG_FGD_LC;
105 m_ParamFGD.N1c = CV_BGFG_FGD_N1C;
106 m_ParamFGD.N2c = CV_BGFG_FGD_N2C;
107 m_ParamFGD.Lcc = CV_BGFG_FGD_LCC;
108 m_ParamFGD.N1cc = CV_BGFG_FGD_N1CC;
109 m_ParamFGD.N2cc = CV_BGFG_FGD_N2CC;
110 m_ParamFGD.delta = CV_BGFG_FGD_DELTA;
111 m_ParamFGD.alpha1 = CV_BGFG_FGD_ALPHA_1;
112 m_ParamFGD.alpha2 = CV_BGFG_FGD_ALPHA_2;
113 m_ParamFGD.alpha3 = CV_BGFG_FGD_ALPHA_3;
114 m_ParamFGD.T = CV_BGFG_FGD_T;
115 m_ParamFGD.minArea = CV_BGFG_FGD_MINAREA;
116 m_ParamFGD.is_obj_without_holes = 1;
117 m_ParamFGD.perform_morphing = 1;
119 AddParam("LC",&m_ParamFGD.Lc);
120 AddParam("alpha1",&m_ParamFGD.alpha1);
121 AddParam("alpha2",&m_ParamFGD.alpha2);
122 AddParam("alpha3",&m_ParamFGD.alpha3);
123 AddParam("N1c",&m_ParamFGD.N1c);
124 AddParam("N2c",&m_ParamFGD.N2c);
125 AddParam("N1cc",&m_ParamFGD.N1cc);
126 AddParam("N2cc",&m_ParamFGD.N2cc);
129 AddParam("SaveName",&m_SaveName);
130 AddParam("LoadName",&m_LoadName);
131 AddParam("ObjWithoutHoles",&m_ParamFGD.is_obj_without_holes);
132 AddParam("Morphology",&m_ParamFGD.perform_morphing);
134 SetModuleName("FGD");
136 else if( m_FGType == CV_BG_MODEL_MOG ) // "MOG" == "Mixture Of Gaussians"
140 m_ParamMOG = *(CvGaussBGStatModelParams*)m_pFGParam;
143 { // These constants are all from cvaux/include/cvaux.h
144 m_ParamMOG.win_size = CV_BGFG_MOG_WINDOW_SIZE;
145 m_ParamMOG.bg_threshold = CV_BGFG_MOG_BACKGROUND_THRESHOLD;
147 m_ParamMOG.std_threshold = CV_BGFG_MOG_STD_THRESHOLD;
148 m_ParamMOG.weight_init = CV_BGFG_MOG_WEIGHT_INIT;
150 m_ParamMOG.variance_init = CV_BGFG_MOG_SIGMA_INIT*CV_BGFG_MOG_SIGMA_INIT;
151 m_ParamMOG.minArea = CV_BGFG_MOG_MINAREA;
152 m_ParamMOG.n_gauss = CV_BGFG_MOG_NGAUSSIANS;
154 AddParam("NG",&m_ParamMOG.n_gauss);
156 SetModuleName("MOG");
162 if(m_pFG)cvReleaseBGStatModel( &m_pFG );
166 if(m_pFG)cvReleaseBGStatModel( &m_pFG );
169 inline IplImage* GetMask()
171 return m_pFG?m_pFG->foreground:NULL;
174 /* Process current image: */
175 virtual void Process(IplImage* pImg)
179 void* param = m_pFGParam;
180 if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
184 else if( m_FGType == CV_BG_MODEL_MOG )
188 m_pFG = cvCreateBGStatModel(
196 cvUpdateBGStatModel( pImg, m_pFG );
200 /* Release foreground detector: */
201 virtual void Release()
204 if(m_pFG)cvReleaseBGStatModel( &m_pFG );
208 CvFGDetector* cvCreateFGDetectorBase(int type, void *param)
210 return (CvFGDetector*) new CvFGDetectorBase(type, param);