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
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
40 //M*/// ContEHMM.cpp: implementation of the CContEHMM class.
42 //////////////////////////////////////////////////////////////////////
51 static char THIS_FILE[]=__FILE__;
55 //////////////////////////////////////////////////////////////////////
56 // Construction/Destruction
57 //////////////////////////////////////////////////////////////////////
59 CContEHMM::CContEHMM()
65 CContEHMM::~CContEHMM()
67 if (m_hmm) cvRelease2DHMM( &m_hmm );
72 bool CContEHMM::CreateHMM( int* num_states, int* num_mix, int vect_size )
74 if (m_hmm) cvRelease2DHMM( &m_hmm );
77 m_hmm = cvCreate2DHMM( num_states, num_mix, vect_size );
79 m_vectSize = vect_size;
86 bool CContEHMM::Save( const char* filename )
90 if (!m_hmm) return false;
92 file = fopen( filename, "wt" );
94 if (!file) return false;
98 fprintf(file, "%s %d\n", "<NumSuperStates>", m_hmm->num_states );
99 fprintf(file, "%s ", "<NumStates>");
100 for( int i = 0; i < m_hmm->num_states; i++ )
102 fprintf(file, "%d ", m_hmm->u.ehmm[i].num_states );
106 fprintf(file, "%s ", "<NumMixtures>");
107 for( i = 0; i < m_hmm->num_states; i++ )
109 CvEHMM* ehmm = &(m_hmm->u.ehmm[i]);
111 for( int j = 0; j < ehmm->num_states; j++ )
113 fprintf(file, "%d ", ehmm->u.state[j].num_mix );
118 fprintf(file, "%s %d\n", "<VecSize>", m_vectSize);
120 //consequently write all hmms
122 for( i = 0; i < m_hmm->num_states + 1; i++ )
124 if (hmm->level == 0 )
125 fprintf(file, "%s\n", "<BeginEmbeddedHMM>");
127 fprintf(file, "%s\n", "<BeginExternalHMM>");
129 fprintf(file, "%s %d\n", "<NumStates>", hmm->num_states);
131 if (hmm->level == 0 )
133 for ( int j = 0; j < hmm->num_states; j++)
135 CvEHMMState* state = &(hmm->u.state[j]);
137 fprintf(file, "%s %d\n", "<State>", j);
138 fprintf(file, "%s %d\n", "<NumMixes>", state->num_mix);
140 float* mu = state->mu;
141 float* inv_var = state->inv_var;
143 for( int m = 0; m < state->num_mix; m++)
145 fprintf(file, "%s %d %s %f\n", "<Mixture>", m, "<Weight>", state->weight[m] );
146 fprintf(file, "%s\n", "<Mean>");
148 for (int k = 0; k < m_vectSize; k++)
150 fprintf(file, "%f ", mu[0]);
155 fprintf(file, "%s\n", "<Inverted_Deviation>");
157 for (k = 0; k < m_vectSize; k++)
159 fprintf(file, "%f ", inv_var[0]);
164 fprintf(file, "%s %f\n", "<LogVarVal>", state->log_var_val[m] );
171 //write the transition probability matrix
172 fprintf(file, "%s\n", "<TransP>" );
173 float* prob = hmm->transP;
175 for (int j = 0; j < hmm->num_states; j++)
177 for (int k = 0; k < hmm->num_states; k++)
179 fprintf(file, "%f ", *prob);
185 if( hmm->level == 0 )
186 fprintf(file, "%s\n", "<EndEmbeddedHMM>");
188 fprintf(file, "%s\n", "<EndExternalHMM>");
190 hmm = &(m_hmm->u.ehmm[i]);
196 bool CContEHMM::Load( const char* filename )
203 if (m_hmm) cvRelease2DHMM( &m_hmm);
205 file = fopen( filename, "rt" );
207 if (!file) return false;
210 fscanf(file, "%s %d\n", temp_char, num_states);
211 fscanf(file, "%s ", temp_char);
212 for( int i = 0; i < num_states[0]; i++ )
214 fscanf(file, "%d ", num_states + i + 1 );
218 //compute total number of internal states
219 int total_states = 0;
220 for( i = 0; i < num_states[0]; i++ )
222 total_states += num_states[i+1];
225 //read number of mixtures
226 fscanf(file, "%s ", temp_char);
227 for( i = 0; i < total_states; i++ )
229 fscanf(file, "%d ", &num_mix[i] );
233 fscanf(file, "%s %d\n", temp_char, &m_vectSize);
235 m_hmm = cvCreate2DHMM( num_states, num_mix, m_vectSize);
238 //create HMM with known parameters
239 //!!! cvCreate2DHMM( &m_hmm, num_states, num_mix, m_vectSize);
240 if (!m_hmm ) return false;
242 //consequently read all hmms
244 for( i = 0; i < num_states[0]+1; i++ )
246 fscanf(file, "%s\n", temp_char);
248 fscanf(file, "%s %d\n", temp_char , &temp_int); assert(temp_int==num_states[i]);
252 for (int j = 0; j < num_states[i]; j++)
254 CvEHMMState* state = &(hmm->u.state[j]);
256 fscanf(file, "%s %d\n", temp_char, &temp_int); assert(temp_int == j);
258 fscanf(file, "%s %d\n", temp_char, &temp_int); assert(temp_int == state->num_mix);
260 float* mu = state->mu;
261 float* inv_var = state->inv_var;
263 for( int m = 0; m < state->num_mix; m++)
266 fscanf(file, "%s %d %s %f\n", temp_char, &temp_int, temp_char, &(state->weight[m]) );
267 assert( temp_int == m );
268 fscanf(file, "%s\n", temp_char );
270 for (int k = 0; k < m_vectSize; k++)
272 fscanf(file, "%f ", mu);
277 fscanf(file, "%s\n", temp_char);
279 for (k = 0; k < m_vectSize; k++)
281 fscanf(file, "%f ", inv_var);
286 fscanf(file, "%s %f\n", temp_char, &(state->log_var_val[m]) );
292 //read the transition probability matrix
293 fscanf(file, "%s\n", temp_char );
294 float* prob = hmm->transP;
296 for (int j = 0; j < hmm->num_states; j++)
298 for (int k = 0; k < hmm->num_states; k++)
300 fscanf(file, "%f ", prob);
306 fscanf( file, "%s\n", temp_char );
308 hmm = &(m_hmm->u.ehmm[i]);
314 BOOL CContEHMM::Release()
318 cvRelease2DHMM( &m_hmm );