Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / cvaux / src / vs / bgfg_estimation.cpp
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 //                        Intel License Agreement
11 //
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
14 //
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
17 //
18 //   * Redistribution's of source code must retain the above copyright notice,
19 //     this list of conditions and the following disclaimer.
20 //
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.
24 //
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.
27 //
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.
38 //
39 //M*/
40 #include "_cvaux.h"
41
42 // Function cvCreateBGStatModel creates and returns initialized BG model.
43 // Parameters:
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 )
48 {
49     CvBGStatModel* bg_model = NULL;
50
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 );
55
56     return bg_model;
57 }
58
59
60 /* FOREGROUND DETECTOR INTERFACE */
61 class CvFGDetectorBase:public CvFGDetector
62 {
63 protected:
64     CvBGStatModel*  m_pFG;
65     int             m_FGType;
66     void*           m_pFGParam; /* Foreground parameters. */
67     CvFGDStatModelParams        m_ParamFGD;
68     CvGaussBGStatModelParams    m_ParamMOG;
69     char*                       m_SaveName;
70     char*                       m_LoadName;
71 public:
72     virtual void SaveState(CvFileStorage* )
73     {
74         if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
75         {
76             if( m_SaveName ) /* File name is not empty */
77             {
78                 //cvSaveStatModel(m_SaveName, (CvFGDStatModel*)m_pFG);
79             }
80         }
81     };
82     virtual void LoadState(CvFileStorage* , CvFileNode* )
83     {
84         if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
85         {
86             if( m_LoadName ) /* File name is not empty */
87             {
88                 //cvRestoreStatModel(m_LoadName, (CvFGDStatModel*)m_pFG);
89             }
90         }
91     };
92     CvFGDetectorBase(int type, void* param)
93     {
94         m_pFG = NULL;
95         m_FGType = type;
96         m_pFGParam = param;
97         if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
98         {
99             if(m_pFGParam)
100             {
101               m_ParamFGD = *(CvFGDStatModelParams*)m_pFGParam;
102             }
103             else
104             {
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;
119             }
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);
128             m_SaveName = 0;
129             m_LoadName = 0;
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);
134         }
135         else if( m_FGType == CV_BG_MODEL_MOG )                  // "MOG" == "Mixture Of Gaussians"
136         {
137             if(m_pFGParam)
138             {
139                 m_ParamMOG = *(CvGaussBGStatModelParams*)m_pFGParam;
140             }
141             else
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;
145
146                 m_ParamMOG.std_threshold = CV_BGFG_MOG_STD_THRESHOLD;
147                 m_ParamMOG.weight_init   = CV_BGFG_MOG_WEIGHT_INIT;
148
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;
152             }
153             AddParam("NG",&m_ParamMOG.n_gauss);
154         }
155
156     };
157     ~CvFGDetectorBase()
158     {
159         if(m_pFG)cvReleaseBGStatModel( &m_pFG );
160     }
161     void ParamUpdate()
162     {
163         if(m_pFG)cvReleaseBGStatModel( &m_pFG );
164     }
165
166     inline IplImage* GetMask()
167     {
168         return m_pFG?m_pFG->foreground:NULL;
169     };
170
171     /* Process current image: */
172     virtual void    Process(IplImage* pImg)
173     {
174         if(m_pFG == NULL)
175         {
176             void* param = m_pFGParam;
177             if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
178             {
179                 param = &m_ParamFGD;
180             }
181             else if( m_FGType == CV_BG_MODEL_MOG )
182             {
183                 param = &m_ParamMOG;
184             }
185             m_pFG = cvCreateBGStatModel(
186                 pImg,
187                 m_FGType,
188                 param);
189             LoadState(0, 0);
190         }
191         else
192         {
193             cvUpdateBGStatModel( pImg, m_pFG );
194         }
195     };
196
197     /* Release foreground detector: */
198     virtual void    Release()
199     {
200         SaveState(0);
201         if(m_pFG)cvReleaseBGStatModel( &m_pFG );
202     };
203 };
204
205 CvFGDetector* cvCreateFGDetectorBase(int type, void *param)
206 {
207     return (CvFGDetector*) new CvFGDetectorBase(type, param);
208 }