cvhmm1d.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,155 行 · 第 1/3 页
SVN-BASE
1,155 行
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "_cvaux.h"
#include <float.h>
#include "_cvutils.h"
#if 0
#define LN2PI 1.837877f
#define BIG_FLT 1.e+10f
#define _CV_ERGODIC 1
#define _CV_CAUSAL 2
#define _CV_LAST_STATE 1
#define _CV_BEST_STATE 2
//*F///////////////////////////////////////////////////////////////////////////////////////
// Name: icvForward1DHMM
// Purpose: The function performs baum-welsh algorithm
// Context:
// Parameters: obs_info - addres of pointer to CvImgObsInfo structure
// num_hor_obs - number of horizontal observation vectors
// num_ver_obs - number of horizontal observation vectors
// obs_size - length of observation vector
//
// Returns: error status
//
// Notes:
//F*/
#if 0
CvStatus icvForward1DHMM( int num_states, int num_obs, CvMatr64d A,
CvMatr64d B,
double* scales)
{
// assume that observation and transition
// probabilities already computed
int m_HMMType = _CV_CAUSAL;
double* m_pi = icvAlloc( num_states* sizeof( double) );
/* alpha is matrix
rows throuhg states
columns through time
*/
double* alpha = icvAlloc( num_states*num_obs * sizeof( double ) );
/* All calculations will be in non-logarithmic domain */
/* Initialization */
/* set initial state probabilities */
m_pi[0] = 1;
for (i = 1; i < num_states; i++)
{
m_pi[i] = 0.0;
}
for (i = 0; i < num_states; i++)
{
alpha[i] = m_pi[i] * m_b[ i];
}
/******************************************************************/
/* Induction */
if ( m_HMMType == _CV_ERGODIC )
{
int t;
for (t = 1 ; t < num_obs; t++)
{
for (j = 0; j < num_states; j++)
{
double sum = 0.0;
int i;
for (i = 0; i < num_states; i++)
{
sum += alpha[(t - 1) * num_states + i] * A[i * num_states + j];
}
alpha[(t - 1) * num_states + j] = sum * B[t * num_states + j];
/* add computed alpha to scale factor */
sum_alpha += alpha[(t - 1) * num_states + j];
}
double scale = 1/sum_alpha;
/* scale alpha */
for (j = 0; j < num_states; j++)
{
alpha[(t - 1) * num_states + j] *= scale;
}
scales[t] = scale;
}
}
#endif
//*F///////////////////////////////////////////////////////////////////////////////////////
// Name: icvCreateObsInfo
// Purpose: The function allocates memory for CvImgObsInfo structure
// and its inner stuff
// Context:
// Parameters: obs_info - addres of pointer to CvImgObsInfo structure
// num_hor_obs - number of horizontal observation vectors
// num_ver_obs - number of horizontal observation vectors
// obs_size - length of observation vector
//
// Returns: error status
//
// Notes:
//F*/
/*CvStatus icvCreateObsInfo( CvImgObsInfo** obs_info,
CvSize num_obs, int obs_size )
{
int total = num_obs.height * num_obs.width;
CvImgObsInfo* obs = (CvImgObsInfo*)icvAlloc( sizeof( CvImgObsInfo) );
obs->obs_x = num_obs.width;
obs->obs_y = num_obs.height;
obs->obs = (float*)icvAlloc( total * obs_size * sizeof(float) );
obs->state = (int*)icvAlloc( 2 * total * sizeof(int) );
obs->mix = (int*)icvAlloc( total * sizeof(int) );
obs->obs_size = obs_size;
obs_info[0] = obs;
return CV_NO_ERR;
}*/
/*CvStatus icvReleaseObsInfo( CvImgObsInfo** p_obs_info )
{
CvImgObsInfo* obs_info = p_obs_info[0];
icvFree( &(obs_info->obs) );
icvFree( &(obs_info->mix) );
icvFree( &(obs_info->state) );
icvFree( &(obs_info) );
p_obs_info[0] = NULL;
return CV_NO_ERR;
} */
//*F///////////////////////////////////////////////////////////////////////////////////////
// Name: icvCreate1DHMM
// Purpose: The function allocates memory for 1-dimensional HMM
// and its inner stuff
// Context:
// Parameters: hmm - addres of pointer to CvEHMM structure
// state_number - number of states in HMM
// num_mix - number of gaussian mixtures in HMM states
// size of array is defined by previous parameter
// obs_size - length of observation vectors
//
// Returns: error status
// Notes:
//F*/
CvStatus icvCreate1DHMM( CvEHMM** this_hmm,
int state_number, int* num_mix, int obs_size )
{
int i;
int real_states = state_number;
CvEHMMState* all_states;
CvEHMM* hmm;
int total_mix = 0;
float* pointers;
/* allocate memory for hmm */
hmm = (CvEHMM*)icvAlloc( sizeof(CvEHMM) );
/* set number of superstates */
hmm->num_states = state_number;
hmm->level = 0;
/* allocate memory for all states */
all_states = (CvEHMMState *)icvAlloc( real_states * sizeof( CvEHMMState ) );
/* assign number of mixtures */
for( i = 0; i < real_states; i++ )
{
all_states[i].num_mix = num_mix[i];
}
/* compute size of inner of all real states */
for( i = 0; i < real_states; i++ )
{
total_mix += num_mix[i];
}
/* allocate memory for states stuff */
pointers = (float*)icvAlloc( total_mix * (2/*for mu invvar */ * obs_size +
2/*for weight and log_var_val*/ ) * sizeof( float) );
/* organize memory */
for( i = 0; i < real_states; i++ )
{
all_states[i].mu = pointers; pointers += num_mix[i] * obs_size;
all_states[i].inv_var = pointers; pointers += num_mix[i] * obs_size;
all_states[i].log_var_val = pointers; pointers += num_mix[i];
all_states[i].weight = pointers; pointers += num_mix[i];
}
hmm->u.state = all_states;
hmm->transP = icvCreateMatrix_32f( hmm->num_states, hmm->num_states );
hmm->obsProb = NULL;
/* if all ok - return pointer */
*this_hmm = hmm;
return CV_NO_ERR;
}
CvStatus icvRelease1DHMM( CvEHMM** phmm )
{
CvEHMM* hmm = phmm[0];
icvDeleteMatrix( hmm->transP );
if (hmm->obsProb != NULL)
{
int* tmp = ((int*)(hmm->obsProb)) - 3;
icvFree( &(tmp) );
}
icvFree( &(hmm->u.state->mu) );
icvFree( &(hmm->u.state) );
phmm[0] = NULL;
return CV_NO_ERR;
}
/*can be used in CHMM & DHMM */
CvStatus icvUniform1DSegm( Cv1DObsInfo* obs_info, CvEHMM* hmm )
{
/* implementation is very bad */
int i;
CvEHMMState* first_state;
/* check arguments */
if ( !obs_info || !hmm ) return CV_NULLPTR_ERR;
first_state = hmm->u.state;
for (i = 0; i < obs_info->obs_x; i++)
{
//bad line (division )
int state = (i * hmm->num_states)/obs_info->obs_x;
obs_info->state[i] = state;
}
return CV_NO_ERR;
}
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: InitMixSegm
// Purpose: The function implements the mixture segmentation of the states of the embedded HMM
// Context: used with the Viterbi training of the embedded HMM
// Function uses K-Means algorithm for clustering
//
// Parameters: obs_info_array - array of pointers to image observations
// num_img - length of above array
// hmm - pointer to HMM structure
//
// Returns: error status
//
// Notes:
//F*/
CvStatus icvInit1DMixSegm(Cv1DObsInfo** obs_info_array, int num_img, CvEHMM* hmm)
{
int k, i, j;
int* num_samples; /* number of observations in every state */
int* counter; /* array of counters for every state */
int** a_class; /* for every state - characteristic array */
CvVect32f** samples; /* for every state - pointer to observation vectors */
int*** samples_mix; /* for every state - array of pointers to vectors mixtures */
CvTermCriteria criteria = cvTermCriteria( CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,
1000, /* iter */
0.01f ); /* eps */
int total = hmm->num_states;
CvEHMMState* first_state = hmm->u.state;
/* for every state integer is allocated - number of vectors in state */
num_samples = (int*)icvAlloc( total * sizeof(int) );
/* integer counter is allocated for every state */
counter = (int*)icvAlloc( total * sizeof(int) );
samples = (CvVect32f**)icvAlloc( total * sizeof(CvVect32f*) );
samples_mix = (int***)icvAlloc( total * sizeof(int**) );
/* clear */
memset( num_samples, 0 , total*sizeof(int) );
memset( counter, 0 , total*sizeof(int) );
/* for every state the number of vectors which belong to it is computed (smth. like histogram) */
for (k = 0; k < num_img; k++)
{
CvImgObsInfo* obs = obs_info_array[k];
for (i = 0; i < obs->obs_x; i++)
{
int state = obs->state[ i ];
num_samples[state] += 1;
}
}
/* for every state int* is allocated */
a_class = (int**)icvAlloc( total*sizeof(int*) );
for (i = 0; i < total; i++)
{
a_class[i] = (int*)icvAlloc( num_samples[i] * sizeof(int) );
samples[i] = (CvVect32f*)icvAlloc( num_samples[i] * sizeof(CvVect32f) );
samples_mix[i] = (int**)icvAlloc( num_samples[i] * sizeof(int*) );
}
/* for every state vectors which belong to state are gathered */
for (k = 0; k < num_img; k++)
{
CvImgObsInfo* obs = obs_info_array[k];
int num_obs = obs->obs_x;
float* vector = obs->obs;
for (i = 0; i < num_obs; i++, vector+=obs->obs_size )
{
int state = obs->state[i];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?