Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / legacy / src / 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 "precomp.hpp"
41
42 // Function cvCreateBGStatModel creates and returns initialized BG model.
43 // Parameters:
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 )
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 /* FOREGROUND DETECTOR INTERFACE */
60 class CvFGDetectorBase : public CvFGDetector
61 {
62 protected:
63     CvBGStatModel*  m_pFG;
64     int             m_FGType;
65     void*           m_pFGParam; /* Foreground parameters. */
66     CvFGDStatModelParams        m_ParamFGD;
67     CvGaussBGStatModelParams    m_ParamMOG;
68     const char*                       m_SaveName;
69     const char*                       m_LoadName;
70 public:
71     virtual void SaveState(CvFileStorage* )
72     {
73         if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
74         {
75             if( m_SaveName ) /* File name is not empty */
76             {
77                 //cvSaveStatModel(m_SaveName, (CvFGDStatModel*)m_pFG);
78             }
79         }
80     };
81     virtual void LoadState(CvFileStorage* , CvFileNode* )
82     {
83         if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
84         {
85             if( m_LoadName ) /* File name is not empty */
86             {
87                 //cvRestoreStatModel(m_LoadName, (CvFGDStatModel*)m_pFG);
88             }
89         }
90     };
91     CvFGDetectorBase(int type, void* param)
92     {
93         m_pFG = NULL;
94         m_FGType = type;
95         m_pFGParam = param;
96         if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
97         {
98             if(m_pFGParam)
99             {
100               m_ParamFGD = *(CvFGDStatModelParams*)m_pFGParam;
101             }
102             else
103             {
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;
118             }
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);
127             m_SaveName = 0;
128             m_LoadName = 0;
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);
133
134         SetModuleName("FGD");
135         }
136         else if( m_FGType == CV_BG_MODEL_MOG )                  // "MOG" == "Mixture Of Gaussians"
137         {
138             if(m_pFGParam)
139             {
140                 m_ParamMOG = *(CvGaussBGStatModelParams*)m_pFGParam;
141             }
142             else
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;
146
147                 m_ParamMOG.std_threshold = CV_BGFG_MOG_STD_THRESHOLD;
148                 m_ParamMOG.weight_init   = CV_BGFG_MOG_WEIGHT_INIT;
149
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;
153             }
154             AddParam("NG",&m_ParamMOG.n_gauss);
155
156             SetModuleName("MOG");
157         }
158
159     };
160     ~CvFGDetectorBase()
161     {
162         if(m_pFG)cvReleaseBGStatModel( &m_pFG );
163     }
164     void ParamUpdate()
165     {
166         if(m_pFG)cvReleaseBGStatModel( &m_pFG );
167     }
168
169     inline IplImage* GetMask()
170     {
171         return m_pFG?m_pFG->foreground:NULL;
172     };
173
174     /* Process current image: */
175     virtual void    Process(IplImage* pImg)
176     {
177         if(m_pFG == NULL)
178         {
179             void* param = m_pFGParam;
180             if( m_FGType == CV_BG_MODEL_FGD || m_FGType == CV_BG_MODEL_FGD_SIMPLE )
181             {
182                 param = &m_ParamFGD;
183             }
184             else if( m_FGType == CV_BG_MODEL_MOG )
185             {
186                 param = &m_ParamMOG;
187             }
188             m_pFG = cvCreateBGStatModel(
189                 pImg,
190                 m_FGType,
191                 param);
192             LoadState(0, 0);
193         }
194         else
195         {
196             cvUpdateBGStatModel( pImg, m_pFG );
197         }
198     };
199
200     /* Release foreground detector: */
201     virtual void    Release()
202     {
203         SaveState(0);
204         if(m_pFG)cvReleaseBGStatModel( &m_pFG );
205     };
206 };
207
208 CvFGDetector* cvCreateFGDetectorBase(int type, void *param)
209 {
210     return (CvFGDetector*) new CvFGDetectorBase(type, param);
211 }