2010-05-12 01:44:00 +08:00
|
|
|
/*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 "precomp.hpp"
|
|
|
|
|
|
|
|
#define LN2PI 1.837877f
|
|
|
|
#define BIG_FLT 1.e+10f
|
|
|
|
|
|
|
|
|
|
|
|
#define _CV_ERGODIC 1
|
|
|
|
#define _CV_CAUSAL 2
|
|
|
|
|
|
|
|
#define _CV_LAST_STATE 1
|
2012-06-12 22:46:12 +08:00
|
|
|
#define _CV_BEST_STATE 2
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
//*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: _cvCreateObsInfo
|
2012-06-12 22:46:12 +08:00
|
|
|
// Purpose: The function allocates memory for CvImgObsInfo structure
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Notes:
|
|
|
|
//F*/
|
|
|
|
static CvStatus CV_STDCALL icvCreateObsInfo( CvImgObsInfo** obs_info,
|
2010-05-12 01:44:00 +08:00
|
|
|
CvSize num_obs, int obs_size )
|
|
|
|
{
|
|
|
|
int total = num_obs.height * num_obs.width;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvImgObsInfo* obs = (CvImgObsInfo*)cvAlloc( sizeof( CvImgObsInfo) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
obs->obs_x = num_obs.width;
|
|
|
|
obs->obs_y = num_obs.height;
|
|
|
|
|
|
|
|
obs->obs = (float*)cvAlloc( total * obs_size * sizeof(float) );
|
|
|
|
|
|
|
|
obs->state = (int*)cvAlloc( 2 * total * sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
obs->mix = (int*)cvAlloc( total * sizeof(int) );
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
obs->obs_size = obs_size;
|
|
|
|
|
|
|
|
obs_info[0] = obs;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return CV_NO_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CvStatus CV_STDCALL icvReleaseObsInfo( CvImgObsInfo** p_obs_info )
|
|
|
|
{
|
|
|
|
CvImgObsInfo* obs_info = p_obs_info[0];
|
|
|
|
|
|
|
|
cvFree( &(obs_info->obs) );
|
|
|
|
cvFree( &(obs_info->mix) );
|
2012-06-12 22:46:12 +08:00
|
|
|
cvFree( &(obs_info->state) );
|
2010-05-12 01:44:00 +08:00
|
|
|
cvFree( &(obs_info) );
|
|
|
|
|
|
|
|
p_obs_info[0] = NULL;
|
|
|
|
|
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
//*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: icvCreate2DHMM
|
2012-06-12 22:46:12 +08:00
|
|
|
// Purpose: The function allocates memory for 2-dimensional embedded HMM model
|
2010-05-12 01:44:00 +08:00
|
|
|
// and its inner stuff
|
|
|
|
// Context:
|
|
|
|
// Parameters: hmm - addres of pointer to CvEHMM structure
|
|
|
|
// state_number - array of hmm sizes (size of array == state_number[0]+1 )
|
2012-06-12 22:46:12 +08:00
|
|
|
// num_mix - number of gaussian mixtures in low-level HMM states
|
2010-05-12 01:44:00 +08:00
|
|
|
// size of array is defined by previous array values
|
|
|
|
// obs_size - length of observation vectors
|
|
|
|
//
|
|
|
|
// Returns: error status
|
|
|
|
//
|
|
|
|
// Notes: state_number[0] - number of states in external HMM.
|
|
|
|
// state_number[i] - number of states in embedded HMM
|
2012-06-12 22:46:12 +08:00
|
|
|
//
|
2010-05-12 01:44:00 +08:00
|
|
|
// example for face recognition: state_number = { 5 3 6 6 6 3 },
|
|
|
|
// length of num_mix array = 3+6+6+6+3 = 24//
|
|
|
|
//
|
|
|
|
//F*/
|
|
|
|
static CvStatus CV_STDCALL icvCreate2DHMM( CvEHMM** this_hmm,
|
|
|
|
int* state_number, int* num_mix, int obs_size )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int real_states = 0;
|
|
|
|
|
|
|
|
CvEHMMState* all_states;
|
|
|
|
CvEHMM* hmm;
|
|
|
|
int total_mix = 0;
|
|
|
|
float* pointers;
|
|
|
|
|
|
|
|
//compute total number of states of all level in 2d EHMM
|
|
|
|
for( i = 1; i <= state_number[0]; i++ )
|
|
|
|
{
|
|
|
|
real_states += state_number[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate memory for all hmms (from all levels) */
|
|
|
|
hmm = (CvEHMM*)cvAlloc( (state_number[0] + 1) * sizeof(CvEHMM) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* set number of superstates */
|
|
|
|
hmm[0].num_states = state_number[0];
|
|
|
|
hmm[0].level = 1;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* allocate memory for all states */
|
|
|
|
all_states = (CvEHMMState *)cvAlloc( 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];
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
/* allocate memory for states stuff */
|
2012-06-12 22:46:12 +08:00
|
|
|
pointers = (float*)cvAlloc( total_mix * (2/*for mu invvar */ * obs_size +
|
2010-05-12 01:44:00 +08:00
|
|
|
2/*for weight and log_var_val*/ ) * sizeof( float) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* organize memory */
|
|
|
|
for( i = 0; i < real_states; i++ )
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
all_states[i].mu = pointers; pointers += num_mix[i] * obs_size;
|
2010-05-12 01:44:00 +08:00
|
|
|
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];
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* set pointer to embedded hmm array */
|
|
|
|
hmm->u.ehmm = hmm + 1;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i < hmm[0].num_states; i++ )
|
|
|
|
{
|
|
|
|
hmm[i+1].u.state = all_states;
|
|
|
|
all_states += state_number[i+1];
|
|
|
|
hmm[i+1].num_states = state_number[i+1];
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i <= state_number[0]; i++ )
|
|
|
|
{
|
|
|
|
hmm[i].transP = icvCreateMatrix_32f( hmm[i].num_states, hmm[i].num_states );
|
|
|
|
hmm[i].obsProb = NULL;
|
|
|
|
hmm[i].level = i ? 0 : 1;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* if all ok - return pointer */
|
|
|
|
*this_hmm = hmm;
|
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
static CvStatus CV_STDCALL icvRelease2DHMM( CvEHMM** phmm )
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
CvEHMM* hmm = phmm[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
int i;
|
|
|
|
for( i = 0; i < hmm[0].num_states + 1; i++ )
|
|
|
|
{
|
|
|
|
icvDeleteMatrix( hmm[i].transP );
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if (hmm->obsProb != NULL)
|
|
|
|
{
|
|
|
|
int* tmp = ((int*)(hmm->obsProb)) - 3;
|
|
|
|
cvFree( &(tmp) );
|
|
|
|
}
|
|
|
|
|
|
|
|
cvFree( &(hmm->u.ehmm->u.state->mu) );
|
|
|
|
cvFree( &(hmm->u.ehmm->u.state) );
|
|
|
|
|
|
|
|
|
|
|
|
/* free hmm structures */
|
|
|
|
cvFree( phmm );
|
|
|
|
|
|
|
|
phmm[0] = NULL;
|
|
|
|
|
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/* distance between 2 vectors */
|
|
|
|
static float icvSquareDistance( CvVect32f v1, CvVect32f v2, int len )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
double dist0 = 0;
|
|
|
|
double dist1 = 0;
|
|
|
|
|
|
|
|
for( i = 0; i <= len - 4; i += 4 )
|
|
|
|
{
|
|
|
|
double t0 = v1[i] - v2[i];
|
|
|
|
double t1 = v1[i+1] - v2[i+1];
|
|
|
|
dist0 += t0*t0;
|
|
|
|
dist1 += t1*t1;
|
|
|
|
|
|
|
|
t0 = v1[i+2] - v2[i+2];
|
|
|
|
t1 = v1[i+3] - v2[i+3];
|
|
|
|
dist0 += t0*t0;
|
|
|
|
dist1 += t1*t1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; i < len; i++ )
|
|
|
|
{
|
|
|
|
double t0 = v1[i] - v2[i];
|
|
|
|
dist0 += t0*t0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (float)(dist0 + dist1);
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*can be used in CHMM & DHMM */
|
|
|
|
static CvStatus CV_STDCALL
|
|
|
|
icvUniformImgSegm( CvImgObsInfo* obs_info, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
/* implementation is very bad */
|
|
|
|
int i, j, counter = 0;
|
|
|
|
CvEHMMState* first_state;
|
|
|
|
float inv_x = 1.f/obs_info->obs_x;
|
|
|
|
float inv_y = 1.f/obs_info->obs_y;
|
|
|
|
|
|
|
|
/* check arguments */
|
|
|
|
if ( !obs_info || !hmm ) return CV_NULLPTR_ERR;
|
|
|
|
|
|
|
|
first_state = hmm->u.ehmm->u.state;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < obs_info->obs_y; i++)
|
|
|
|
{
|
|
|
|
//bad line (division )
|
|
|
|
int superstate = (int)((i * hmm->num_states)*inv_y);/* /obs_info->obs_y; */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int index = (int)(hmm->u.ehmm[superstate].u.state - first_state);
|
|
|
|
|
|
|
|
for (j = 0; j < obs_info->obs_x; j++, counter++)
|
|
|
|
{
|
|
|
|
int state = (int)((j * hmm->u.ehmm[superstate].num_states)* inv_x); /* / obs_info->obs_x; */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
obs_info->state[2 * counter] = superstate;
|
|
|
|
obs_info->state[2 * counter + 1] = state + index;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
#else
|
|
|
|
//this is not ready yet
|
|
|
|
|
|
|
|
int i,j,k,m;
|
2012-06-12 22:46:12 +08:00
|
|
|
CvEHMMState* first_state = hmm->u.ehmm->u.state;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/* check bad arguments */
|
|
|
|
if ( hmm->num_states > obs_info->obs_y ) return CV_BADSIZE_ERR;
|
|
|
|
|
|
|
|
//compute vertical subdivision
|
|
|
|
float row_per_state = (float)obs_info->obs_y / hmm->num_states;
|
|
|
|
float col_per_state[1024]; /* maximum 1024 superstates */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//for every horizontal band compute subdivision
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[i]);
|
|
|
|
col_per_state[i] = (float)obs_info->obs_x / ehmm->num_states;
|
|
|
|
}
|
|
|
|
|
|
|
|
//compute state bounds
|
|
|
|
int ss_bound[1024];
|
|
|
|
for( i = 0; i < hmm->num_states - 1; i++ )
|
|
|
|
{
|
|
|
|
ss_bound[i] = floor( row_per_state * ( i+1 ) );
|
|
|
|
}
|
|
|
|
ss_bound[hmm->num_states - 1] = obs_info->obs_y;
|
|
|
|
|
|
|
|
//work inside every superstate
|
|
|
|
|
|
|
|
int row = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[i]);
|
|
|
|
int index = ehmm->u.state - first_state;
|
|
|
|
|
|
|
|
//calc distribution in superstate
|
|
|
|
int es_bound[1024];
|
|
|
|
for( j = 0; j < ehmm->num_states - 1; j++ )
|
|
|
|
{
|
|
|
|
es_bound[j] = floor( col_per_state[i] * ( j+1 ) );
|
|
|
|
}
|
|
|
|
es_bound[ehmm->num_states - 1] = obs_info->obs_x;
|
|
|
|
|
|
|
|
//assign states to first row of superstate
|
|
|
|
int col = 0;
|
|
|
|
for( j = 0; j < ehmm->num_states; j++ )
|
|
|
|
{
|
|
|
|
for( k = col; k < es_bound[j]; k++, col++ )
|
|
|
|
{
|
|
|
|
obs_info->state[row * obs_info->obs_x + 2 * k] = i;
|
|
|
|
obs_info->state[row * obs_info->obs_x + 2 * k + 1] = j + index;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
col = es_bound[j];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//copy the same to other rows of superstate
|
|
|
|
for( m = row; m < ss_bound[i]; m++ )
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
memcpy( &(obs_info->state[m * obs_info->obs_x * 2]),
|
2010-05-12 01:44:00 +08:00
|
|
|
&(obs_info->state[row * obs_info->obs_x * 2]), obs_info->obs_x * 2 * sizeof(int) );
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
row = ss_bound[i];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return CV_NO_ERR;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*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
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Parameters: obs_info_array - array of pointers to image observations
|
2010-05-12 01:44:00 +08:00
|
|
|
// num_img - length of above array
|
2012-06-12 22:46:12 +08:00
|
|
|
// hmm - pointer to HMM structure
|
|
|
|
//
|
2010-05-12 01:44:00 +08:00
|
|
|
// Returns: error status
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
|
|
|
static CvStatus CV_STDCALL
|
|
|
|
icvInitMixSegm( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm )
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
|
|
|
int k, i, j;
|
2010-05-12 01:44:00 +08:00
|
|
|
int* num_samples; /* number of observations in every state */
|
|
|
|
int* counter; /* array of counters for every state */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int** a_class; /* for every state - characteristic array */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvVect32f** samples; /* for every state - pointer to observation vectors */
|
2012-06-12 22:46:12 +08:00
|
|
|
int*** samples_mix; /* for every state - array of pointers to vectors mixtures */
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvTermCriteria criteria = cvTermCriteria( CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,
|
|
|
|
1000, /* iter */
|
|
|
|
0.01f ); /* eps */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int total = 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
CvEHMMState* first_state = hmm->u.ehmm->u.state;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0 ; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
total += hmm->u.ehmm[i].num_states;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* for every state integer is allocated - number of vectors in state */
|
|
|
|
num_samples = (int*)cvAlloc( total * sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* integer counter is allocated for every state */
|
|
|
|
counter = (int*)cvAlloc( total * sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
samples = (CvVect32f**)cvAlloc( total * sizeof(CvVect32f*) );
|
|
|
|
samples_mix = (int***)cvAlloc( total * sizeof(int**) );
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* clear */
|
|
|
|
memset( num_samples, 0 , total*sizeof(int) );
|
|
|
|
memset( counter, 0 , total*sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* for every state the number of vectors which belong to it is computed (smth. like histogram) */
|
|
|
|
for (k = 0; k < num_img; k++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
CvImgObsInfo* obs = obs_info_array[k];
|
|
|
|
int count = 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < obs->obs_y; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < obs->obs_x; j++, count++)
|
|
|
|
{
|
|
|
|
int state = obs->state[ 2 * count + 1];
|
|
|
|
num_samples[state] += 1;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* for every state int* is allocated */
|
|
|
|
a_class = (int**)cvAlloc( total*sizeof(int*) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < total; i++)
|
|
|
|
{
|
|
|
|
a_class[i] = (int*)cvAlloc( num_samples[i] * sizeof(int) );
|
|
|
|
samples[i] = (CvVect32f*)cvAlloc( num_samples[i] * sizeof(CvVect32f) );
|
|
|
|
samples_mix[i] = (int**)cvAlloc( num_samples[i] * sizeof(int*) );
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* for every state vectors which belong to state are gathered */
|
|
|
|
for (k = 0; k < num_img; k++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
CvImgObsInfo* obs = obs_info_array[k];
|
|
|
|
int num_obs = ( obs->obs_x ) * ( obs->obs_y );
|
|
|
|
float* vector = obs->obs;
|
|
|
|
|
|
|
|
for (i = 0; i < num_obs; i++, vector+=obs->obs_size )
|
|
|
|
{
|
|
|
|
int state = obs->state[2*i+1];
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
samples[state][counter[state]] = vector;
|
|
|
|
samples_mix[state][counter[state]] = &(obs->mix[i]);
|
2012-06-12 22:46:12 +08:00
|
|
|
counter[state]++;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* clear counters */
|
|
|
|
memset( counter, 0, total*sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* do the actual clustering using the K Means algorithm */
|
|
|
|
for (i = 0; i < total; i++)
|
|
|
|
{
|
|
|
|
if ( first_state[i].num_mix == 1)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
for (k = 0; k < num_samples[i]; k++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
/* all vectors belong to one mixture */
|
|
|
|
a_class[i][k] = 0;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
else if( num_samples[i] )
|
|
|
|
{
|
|
|
|
/* clusterize vectors */
|
2012-06-12 22:46:12 +08:00
|
|
|
cvKMeans( first_state[i].num_mix, samples[i], num_samples[i],
|
2010-05-12 01:44:00 +08:00
|
|
|
obs_info_array[0]->obs_size, criteria, a_class[i] );
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* for every vector number of mixture is assigned */
|
|
|
|
for( i = 0; i < total; i++ )
|
|
|
|
{
|
|
|
|
for (j = 0; j < num_samples[i]; j++)
|
|
|
|
{
|
|
|
|
samples_mix[i][j][0] = a_class[i][j];
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < total; i++)
|
|
|
|
{
|
|
|
|
cvFree( &(a_class[i]) );
|
|
|
|
cvFree( &(samples[i]) );
|
|
|
|
cvFree( &(samples_mix[i]) );
|
|
|
|
}
|
|
|
|
|
|
|
|
cvFree( &a_class );
|
|
|
|
cvFree( &samples );
|
|
|
|
cvFree( &samples_mix );
|
|
|
|
cvFree( &counter );
|
2012-06-12 22:46:12 +08:00
|
|
|
cvFree( &num_samples );
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return CV_NO_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: ComputeUniModeGauss
|
2012-06-12 22:46:12 +08:00
|
|
|
// Purpose: The function computes the Gaussian pdf for a sample vector
|
2010-05-12 01:44:00 +08:00
|
|
|
// Context:
|
|
|
|
// Parameters: obsVeq - pointer to the sample vector
|
|
|
|
// mu - pointer to the mean vector of the Gaussian pdf
|
|
|
|
// var - pointer to the variance vector of the Gaussian pdf
|
|
|
|
// VecSize - the size of sample vector
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Returns: the pdf of the sample vector given the specified Gaussian
|
|
|
|
//
|
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
2012-06-12 22:46:12 +08:00
|
|
|
/*static float icvComputeUniModeGauss(CvVect32f vect, CvVect32f mu,
|
|
|
|
CvVect32f inv_var, float log_var_val, int vect_size)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
int n;
|
2010-05-12 01:44:00 +08:00
|
|
|
double tmp;
|
|
|
|
double prob;
|
|
|
|
|
|
|
|
prob = -log_var_val;
|
|
|
|
|
|
|
|
for (n = 0; n < vect_size; n++)
|
|
|
|
{
|
|
|
|
tmp = (vect[n] - mu[n]) * inv_var[n];
|
|
|
|
prob = prob - tmp * tmp;
|
|
|
|
}
|
|
|
|
//prob *= 0.5f;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return (float)prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
}*/
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: ComputeGaussMixture
|
2012-06-12 22:46:12 +08:00
|
|
|
// Purpose: The function computes the mixture Gaussian pdf of a sample vector.
|
2010-05-12 01:44:00 +08:00
|
|
|
// Context:
|
|
|
|
// Parameters: obsVeq - pointer to the sample vector
|
|
|
|
// mu - two-dimensional pointer to the mean vector of the Gaussian pdf;
|
2012-06-12 22:46:12 +08:00
|
|
|
// the first dimension is indexed over the number of mixtures and
|
2010-05-12 01:44:00 +08:00
|
|
|
// the second dimension is indexed along the size of the mean vector
|
|
|
|
// var - two-dimensional pointer to the variance vector of the Gaussian pdf;
|
2012-06-12 22:46:12 +08:00
|
|
|
// the first dimension is indexed over the number of mixtures and
|
2010-05-12 01:44:00 +08:00
|
|
|
// the second dimension is indexed along the size of the variance vector
|
|
|
|
// VecSize - the size of sample vector
|
|
|
|
// weight - pointer to the wights of the Gaussian mixture
|
|
|
|
// NumMix - the number of Gaussian mixtures
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Returns: the pdf of the sample vector given the specified Gaussian mixture.
|
|
|
|
//
|
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
|
|
|
/* Calculate probability of observation at state in logarithmic scale*/
|
|
|
|
/*static float
|
2012-06-12 22:46:12 +08:00
|
|
|
icvComputeGaussMixture( CvVect32f vect, float* mu,
|
|
|
|
float* inv_var, float* log_var_val,
|
2010-05-12 01:44:00 +08:00
|
|
|
int vect_size, float* weight, int num_mix )
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
double prob, l_prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
prob = 0.0f;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if (num_mix == 1)
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
return icvComputeUniModeGauss( vect, mu, inv_var, log_var_val[0], vect_size);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int m;
|
|
|
|
for (m = 0; m < num_mix; m++)
|
|
|
|
{
|
|
|
|
if ( weight[m] > 0.0)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
|
|
|
l_prob = icvComputeUniModeGauss(vect, mu + m*vect_size,
|
2010-05-12 01:44:00 +08:00
|
|
|
inv_var + m * vect_size,
|
2012-06-12 22:46:12 +08:00
|
|
|
log_var_val[m],
|
|
|
|
vect_size);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
prob = prob + weight[m]*exp((double)l_prob);
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
prob = log(prob);
|
|
|
|
}
|
|
|
|
return (float)prob;
|
|
|
|
}*/
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: EstimateObsProb
|
2012-06-12 22:46:12 +08:00
|
|
|
// Purpose: The function computes the probability of every observation in every state
|
2010-05-12 01:44:00 +08:00
|
|
|
// Context:
|
|
|
|
// Parameters: obs_info - observations
|
|
|
|
// hmm - hmm
|
2012-06-12 22:46:12 +08:00
|
|
|
// Returns: error status
|
2010-05-12 01:44:00 +08:00
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
|
|
|
static CvStatus CV_STDCALL icvEstimateObsProb( CvImgObsInfo* obs_info, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int total_states = 0;
|
|
|
|
|
|
|
|
/* check if matrix exist and check current size
|
|
|
|
if not sufficient - realloc */
|
2012-06-12 22:46:12 +08:00
|
|
|
int status = 0; /* 1 - not allocated, 2 - allocated but small size,
|
2010-05-12 01:44:00 +08:00
|
|
|
3 - size is enough, but distribution is bad, 0 - all ok */
|
|
|
|
|
|
|
|
for( j = 0; j < hmm->num_states; j++ )
|
|
|
|
{
|
|
|
|
total_states += hmm->u.ehmm[j].num_states;
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
if ( hmm->obsProb == NULL )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
/* allocare memory */
|
|
|
|
int need_size = ( obs_info->obs_x * obs_info->obs_y * total_states * sizeof(float) +
|
|
|
|
obs_info->obs_y * hmm->num_states * sizeof( CvMatr32f) );
|
|
|
|
|
|
|
|
int* buffer = (int*)cvAlloc( need_size + 3 * sizeof(int) );
|
|
|
|
buffer[0] = need_size;
|
|
|
|
buffer[1] = obs_info->obs_y;
|
|
|
|
buffer[2] = obs_info->obs_x;
|
|
|
|
hmm->obsProb = (float**) (buffer + 3);
|
|
|
|
status = 3;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
/* check current size */
|
|
|
|
int* total= (int*)(((int*)(hmm->obsProb)) - 3);
|
|
|
|
int need_size = ( obs_info->obs_x * obs_info->obs_y * total_states * sizeof(float) +
|
|
|
|
obs_info->obs_y * hmm->num_states * sizeof( CvMatr32f/*(float*)*/ ) );
|
|
|
|
|
|
|
|
assert( sizeof(float*) == sizeof(int) );
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
if ( need_size > (*total) )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int* buffer = ((int*)(hmm->obsProb)) - 3;
|
|
|
|
cvFree( &buffer);
|
|
|
|
buffer = (int*)cvAlloc( need_size + 3 * sizeof(int));
|
|
|
|
buffer[0] = need_size;
|
|
|
|
buffer[1] = obs_info->obs_y;
|
|
|
|
buffer[2] = obs_info->obs_x;
|
|
|
|
|
|
|
|
hmm->obsProb = (float**)(buffer + 3);
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
status = 3;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
int* obsx = ((int*)(hmm->obsProb)) - 1;
|
|
|
|
int* obsy = ((int*)(hmm->obsProb)) - 2;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
assert( (*obsx > 0) && (*obsy > 0) );
|
|
|
|
|
|
|
|
/* is good distribution? */
|
2012-06-12 22:46:12 +08:00
|
|
|
if ( (obs_info->obs_x > (*obsx) ) || (obs_info->obs_y > (*obsy) ) )
|
|
|
|
status = 3;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* if bad status - do reallocation actions */
|
|
|
|
assert( (status == 0) || (status == 3) );
|
|
|
|
|
|
|
|
if ( status )
|
|
|
|
{
|
|
|
|
float** tmp = hmm->obsProb;
|
|
|
|
float* tmpf;
|
|
|
|
|
|
|
|
/* distribute pointers of ehmm->obsProb */
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
hmm->u.ehmm[i].obsProb = tmp;
|
2010-05-12 01:44:00 +08:00
|
|
|
tmp += obs_info->obs_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpf = (float*)tmp;
|
|
|
|
|
|
|
|
/* distribute pointers of ehmm->obsProb[j] */
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &( hmm->u.ehmm[i] );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( j = 0; j < obs_info->obs_y; j++ )
|
|
|
|
{
|
|
|
|
ehmm->obsProb[j] = tmpf;
|
|
|
|
tmpf += ehmm->num_states * obs_info->obs_x;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}/* end of pointer distribution */
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
#if 1
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
#define MAX_BUF_SIZE 1200
|
|
|
|
float local_log_mix_prob[MAX_BUF_SIZE];
|
|
|
|
double local_mix_prob[MAX_BUF_SIZE];
|
|
|
|
int vect_size = obs_info->obs_size;
|
|
|
|
CvStatus res = CV_NO_ERR;
|
|
|
|
|
|
|
|
float* log_mix_prob = local_log_mix_prob;
|
|
|
|
double* mix_prob = local_mix_prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int max_size = 0;
|
|
|
|
int obs_x = obs_info->obs_x;
|
|
|
|
|
|
|
|
/* calculate temporary buffer size */
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[i]);
|
|
|
|
CvEHMMState* state = ehmm->u.state;
|
|
|
|
|
|
|
|
int max_mix = 0;
|
|
|
|
for( j = 0; j < ehmm->num_states; j++ )
|
|
|
|
{
|
|
|
|
int t = state[j].num_mix;
|
|
|
|
if( max_mix < t ) max_mix = t;
|
|
|
|
}
|
|
|
|
max_mix *= ehmm->num_states;
|
|
|
|
if( max_size < max_mix ) max_size = max_mix;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_size *= obs_x * vect_size;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* allocate buffer */
|
|
|
|
if( max_size > MAX_BUF_SIZE )
|
|
|
|
{
|
|
|
|
log_mix_prob = (float*)cvAlloc( max_size*(sizeof(float) + sizeof(double)));
|
|
|
|
if( !log_mix_prob ) return CV_OUTOFMEM_ERR;
|
|
|
|
mix_prob = (double*)(log_mix_prob + max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( log_mix_prob, 0, max_size*sizeof(float));
|
|
|
|
|
|
|
|
/*****************computing probabilities***********************/
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* loop through external states */
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[i]);
|
|
|
|
CvEHMMState* state = ehmm->u.state;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int max_mix = 0;
|
|
|
|
int n_states = ehmm->num_states;
|
|
|
|
|
|
|
|
/* determine maximal number of mixtures (again) */
|
|
|
|
for( j = 0; j < ehmm->num_states; j++ )
|
|
|
|
{
|
|
|
|
int t = state[j].num_mix;
|
|
|
|
if( max_mix < t ) max_mix = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop through rows of the observation matrix */
|
|
|
|
for( j = 0; j < obs_info->obs_y; j++ )
|
|
|
|
{
|
|
|
|
int m, n;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float* obs = obs_info->obs + j * obs_x * vect_size;
|
|
|
|
float* log_mp = max_mix > 1 ? log_mix_prob : ehmm->obsProb[j];
|
|
|
|
double* mp = mix_prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* several passes are done below */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* 1. calculate logarithms of probabilities for each mixture */
|
|
|
|
|
|
|
|
/* loop through mixtures */
|
|
|
|
for( m = 0; m < max_mix; m++ )
|
|
|
|
{
|
|
|
|
/* set pointer to first observation in the line */
|
|
|
|
float* vect = obs;
|
|
|
|
|
|
|
|
/* cycles through obseravtions in the line */
|
|
|
|
for( n = 0; n < obs_x; n++, vect += vect_size, log_mp += n_states )
|
|
|
|
{
|
|
|
|
int k, l;
|
|
|
|
for( l = 0; l < n_states; l++ )
|
|
|
|
{
|
|
|
|
if( state[l].num_mix > m )
|
|
|
|
{
|
|
|
|
float* mu = state[l].mu + m*vect_size;
|
|
|
|
float* inv_var = state[l].inv_var + m*vect_size;
|
|
|
|
double prob = -state[l].log_var_val[m];
|
|
|
|
for( k = 0; k < vect_size; k++ )
|
|
|
|
{
|
|
|
|
double t = (vect[k] - mu[k])*inv_var[k];
|
|
|
|
prob -= t*t;
|
|
|
|
}
|
|
|
|
log_mp[l] = MAX( (float)prob, -500 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip the rest if there is a single mixture */
|
|
|
|
if( max_mix == 1 ) continue;
|
|
|
|
|
|
|
|
/* 2. calculate exponent of log_mix_prob
|
|
|
|
(i.e. probability for each mixture) */
|
|
|
|
cvbFastExp( log_mix_prob, mix_prob, max_mix * obs_x * n_states );
|
|
|
|
|
|
|
|
/* 3. sum all mixtures with weights */
|
|
|
|
/* 3a. first mixture - simply scale by weight */
|
|
|
|
for( n = 0; n < obs_x; n++, mp += n_states )
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
for( l = 0; l < n_states; l++ )
|
|
|
|
{
|
|
|
|
mp[l] *= state[l].weight[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3b. add other mixtures */
|
|
|
|
for( m = 1; m < max_mix; m++ )
|
|
|
|
{
|
|
|
|
int ofs = -m*obs_x*n_states;
|
|
|
|
for( n = 0; n < obs_x; n++, mp += n_states )
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
for( l = 0; l < n_states; l++ )
|
|
|
|
{
|
|
|
|
if( m < state[l].num_mix )
|
|
|
|
{
|
|
|
|
mp[l + ofs] += mp[l] * state[l].weight[m];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 4. Put logarithms of summary probabilities to the destination matrix */
|
|
|
|
cvbFastLog( mix_prob, ehmm->obsProb[j], obs_x * n_states );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( log_mix_prob != local_log_mix_prob ) cvFree( &log_mix_prob );
|
|
|
|
return res;
|
|
|
|
#undef MAX_BUF_SIZE
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
for( i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[i]);
|
|
|
|
CvEHMMState* state = ehmm->u.state;
|
|
|
|
|
|
|
|
for( j = 0; j < obs_info->obs_y; j++ )
|
|
|
|
{
|
|
|
|
int k,m;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int obs_index = j * obs_info->obs_x;
|
|
|
|
|
|
|
|
float* B = ehmm->obsProb[j];
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* cycles through obs and states */
|
|
|
|
for( k = 0; k < obs_info->obs_x; k++ )
|
|
|
|
{
|
|
|
|
CvVect32f vect = (obs_info->obs) + (obs_index + k) * vect_size;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float* matr_line = B + k * ehmm->num_states;
|
|
|
|
|
|
|
|
for( m = 0; m < ehmm->num_states; m++ )
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
matr_line[m] = icvComputeGaussMixture( vect, state[m].mu, state[m].inv_var,
|
2010-05-12 01:44:00 +08:00
|
|
|
state[m].log_var_val, vect_size, state[m].weight,
|
|
|
|
state[m].num_mix );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: EstimateTransProb
|
2012-06-12 22:46:12 +08:00
|
|
|
// Purpose: The function calculates the state and super state transition probabilities
|
|
|
|
// of the model given the images,
|
2010-05-12 01:44:00 +08:00
|
|
|
// the state segmentation and the input parameters
|
|
|
|
// Context:
|
2012-06-12 22:46:12 +08:00
|
|
|
// Parameters: obs_info_array - array of pointers to image observations
|
2010-05-12 01:44:00 +08:00
|
|
|
// num_img - length of above array
|
2012-06-12 22:46:12 +08:00
|
|
|
// hmm - pointer to HMM structure
|
2010-05-12 01:44:00 +08:00
|
|
|
// Returns: void
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
|
|
|
static CvStatus CV_STDCALL
|
|
|
|
icvEstimateTransProb( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
CvEHMMState* first_state = hmm->u.ehmm->u.state;
|
|
|
|
/* as a counter we will use transP matrix */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* initialization */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* clear transP */
|
|
|
|
icvSetZero_32f( hmm->transP, hmm->num_states, hmm->num_states );
|
|
|
|
for (i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
icvSetZero_32f( hmm->u.ehmm[i].transP , hmm->u.ehmm[i].num_states, hmm->u.ehmm[i].num_states );
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* compute the counters */
|
|
|
|
for (i = 0; i < num_img; i++)
|
|
|
|
{
|
|
|
|
int counter = 0;
|
|
|
|
CvImgObsInfo* info = obs_info_array[i];
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 0; j < info->obs_y; j++)
|
|
|
|
{
|
|
|
|
for (k = 0; k < info->obs_x; k++, counter++)
|
|
|
|
{
|
|
|
|
/* compute how many transitions from state to state
|
2012-06-12 22:46:12 +08:00
|
|
|
occured both in horizontal and vertical direction */
|
2010-05-12 01:44:00 +08:00
|
|
|
int superstate, state;
|
|
|
|
int nextsuperstate, nextstate;
|
|
|
|
int begin_ind;
|
|
|
|
|
|
|
|
superstate = info->state[2 * counter];
|
|
|
|
begin_ind = (int)(hmm->u.ehmm[superstate].u.state - first_state);
|
2012-06-12 22:46:12 +08:00
|
|
|
state = info->state[ 2 * counter + 1] - begin_ind;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (j < info->obs_y - 1)
|
|
|
|
{
|
|
|
|
int transP_size = hmm->num_states;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
nextsuperstate = info->state[ 2*(counter + info->obs_x) ];
|
|
|
|
|
|
|
|
hmm->transP[superstate * transP_size + nextsuperstate] += 1;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (k < info->obs_x - 1)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
int transP_size = hmm->u.ehmm[superstate].num_states;
|
|
|
|
|
|
|
|
nextstate = info->state[2*(counter+1) + 1] - begin_ind;
|
|
|
|
hmm->u.ehmm[superstate].transP[ state * transP_size + nextstate] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* estimate superstate matrix */
|
|
|
|
for( i = 0; i < hmm->num_states; i++)
|
|
|
|
{
|
|
|
|
float total = 0;
|
|
|
|
float inv_total;
|
|
|
|
for( j = 0; j < hmm->num_states; j++)
|
|
|
|
{
|
|
|
|
total += hmm->transP[i * hmm->num_states + j];
|
|
|
|
}
|
|
|
|
//assert( total );
|
|
|
|
|
|
|
|
inv_total = total ? 1.f/total : 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( j = 0; j < hmm->num_states; j++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
|
|
|
hmm->transP[i * hmm->num_states + j] =
|
|
|
|
hmm->transP[i * hmm->num_states + j] ?
|
2010-05-12 01:44:00 +08:00
|
|
|
(float)log( hmm->transP[i * hmm->num_states + j] * inv_total ) : -BIG_FLT;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* estimate other matrices */
|
|
|
|
for( k = 0; k < hmm->num_states; k++ )
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[k]);
|
|
|
|
|
|
|
|
for( i = 0; i < ehmm->num_states; i++)
|
|
|
|
{
|
|
|
|
float total = 0;
|
|
|
|
float inv_total;
|
|
|
|
for( j = 0; j < ehmm->num_states; j++)
|
|
|
|
{
|
|
|
|
total += ehmm->transP[i*ehmm->num_states + j];
|
|
|
|
}
|
|
|
|
//assert( total );
|
|
|
|
inv_total = total ? 1.f/total : 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( j = 0; j < ehmm->num_states; j++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
|
|
|
ehmm->transP[i * ehmm->num_states + j] =
|
2010-05-12 01:44:00 +08:00
|
|
|
(ehmm->transP[i * ehmm->num_states + j]) ?
|
|
|
|
(float)log( ehmm->transP[i * ehmm->num_states + j] * inv_total) : -BIG_FLT ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: MixSegmL2
|
|
|
|
// Purpose: The function implements the mixture segmentation of the states of the
|
|
|
|
// embedded HMM
|
|
|
|
// Context: used with the Viterbi training of the embedded HMM
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Parameters:
|
2010-05-12 01:44:00 +08:00
|
|
|
// obs_info_array
|
|
|
|
// num_img
|
|
|
|
// hmm
|
|
|
|
// Returns: void
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
|
|
|
static CvStatus CV_STDCALL
|
|
|
|
icvMixSegmL2( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
int k, i, j, m;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvEHMMState* state = hmm->u.ehmm[0].u.state;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (k = 0; k < num_img; k++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
int counter = 0;
|
|
|
|
CvImgObsInfo* info = obs_info_array[k];
|
|
|
|
|
|
|
|
for (i = 0; i < info->obs_y; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < info->obs_x; j++, counter++)
|
|
|
|
{
|
|
|
|
int e_state = info->state[2 * counter + 1];
|
|
|
|
float min_dist;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
min_dist = icvSquareDistance((info->obs) + (counter * info->obs_size),
|
2010-05-12 01:44:00 +08:00
|
|
|
state[e_state].mu, info->obs_size);
|
2012-06-12 22:46:12 +08:00
|
|
|
info->mix[counter] = 0;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (m = 1; m < state[e_state].num_mix; m++)
|
|
|
|
{
|
|
|
|
float dist=icvSquareDistance( (info->obs) + (counter * info->obs_size),
|
|
|
|
state[e_state].mu + m * info->obs_size,
|
|
|
|
info->obs_size);
|
|
|
|
if (dist < min_dist)
|
|
|
|
{
|
|
|
|
min_dist = dist;
|
2012-06-12 22:46:12 +08:00
|
|
|
/* assign mixture with smallest distance */
|
2010-05-12 01:44:00 +08:00
|
|
|
info->mix[counter] = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
CvStatus icvMixSegmProb(CvImgObsInfo* obs_info, int num_img, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
int k, i, j, m;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvEHMMState* state = hmm->ehmm[0].state_info;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (k = 0; k < num_img; k++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
int counter = 0;
|
|
|
|
CvImgObsInfo* info = obs_info + k;
|
|
|
|
|
|
|
|
for (i = 0; i < info->obs_y; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < info->obs_x; j++, counter++)
|
|
|
|
{
|
|
|
|
int e_state = info->in_state[counter];
|
|
|
|
float max_prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
max_prob = icvComputeUniModeGauss( info->obs[counter], state[e_state].mu[0],
|
|
|
|
state[e_state].inv_var[0],
|
2010-05-12 01:44:00 +08:00
|
|
|
state[e_state].log_var[0],
|
|
|
|
info->obs_size );
|
2012-06-12 22:46:12 +08:00
|
|
|
info->mix[counter] = 0;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (m = 1; m < state[e_state].num_mix; m++)
|
|
|
|
{
|
|
|
|
float prob=icvComputeUniModeGauss(info->obs[counter], state[e_state].mu[m],
|
2012-06-12 22:46:12 +08:00
|
|
|
state[e_state].inv_var[m],
|
2010-05-12 01:44:00 +08:00
|
|
|
state[e_state].log_var[m],
|
|
|
|
info->obs_size);
|
|
|
|
if (prob > max_prob)
|
|
|
|
{
|
|
|
|
max_prob = prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
// assign mixture with greatest probability.
|
2010-05-12 01:44:00 +08:00
|
|
|
info->mix[counter] = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
*/
|
|
|
|
static CvStatus CV_STDCALL
|
|
|
|
icvViterbiSegmentation( int num_states, int /*num_obs*/, CvMatr32f transP,
|
|
|
|
CvMatr32f B, int start_obs, int prob_type,
|
|
|
|
int** q, int min_num_obs, int max_num_obs,
|
|
|
|
float* prob )
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
// memory allocation
|
2010-05-12 01:44:00 +08:00
|
|
|
int i, j, last_obs;
|
|
|
|
int m_HMMType = _CV_ERGODIC; /* _CV_CAUSAL or _CV_ERGODIC */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int m_ProbType = prob_type; /* _CV_LAST_STATE or _CV_BEST_STATE */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int m_minNumObs = min_num_obs; /*??*/
|
|
|
|
int m_maxNumObs = max_num_obs; /*??*/
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int m_numStates = num_states;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float* m_pi = (float*)cvAlloc( num_states* sizeof(float) );
|
|
|
|
CvMatr32f m_a = transP;
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
// offset brobability matrix to starting observation
|
2010-05-12 01:44:00 +08:00
|
|
|
CvMatr32f m_b = B + start_obs * num_states;
|
|
|
|
//so m_xl will not be used more
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
//m_xl = start_obs;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
/* if (muDur != NULL){
|
2010-05-12 01:44:00 +08:00
|
|
|
m_d = new int[m_numStates];
|
|
|
|
m_l = new double[m_numStates];
|
|
|
|
for (i = 0; i < m_numStates; i++){
|
2012-06-12 22:46:12 +08:00
|
|
|
m_l[i] = muDur[i];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
m_d = NULL;
|
|
|
|
m_l = NULL;
|
|
|
|
}
|
|
|
|
*/
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvMatr32f m_Gamma = icvCreateMatrix_32f( num_states, m_maxNumObs );
|
|
|
|
int* m_csi = (int*)cvAlloc( num_states * m_maxNumObs * sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//stores maximal result for every ending observation */
|
|
|
|
CvVect32f m_MaxGamma = prob;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// assert( m_xl + max_num_obs <= num_obs );
|
|
|
|
|
|
|
|
/*??m_q = new int*[m_maxNumObs - m_minNumObs];
|
|
|
|
??for (i = 0; i < m_maxNumObs - m_minNumObs; i++)
|
|
|
|
?? m_q[i] = new int[m_minNumObs + i + 1];
|
|
|
|
*/
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* Viterbi initialization */
|
|
|
|
/* set initial state probabilities, in logarithmic scale */
|
|
|
|
for (i = 0; i < m_numStates; i++)
|
|
|
|
{
|
|
|
|
m_pi[i] = -BIG_FLT;
|
|
|
|
}
|
|
|
|
m_pi[0] = 0.0f;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < num_states; i++)
|
|
|
|
{
|
|
|
|
m_Gamma[0 * num_states + i] = m_pi[i] + m_b[0 * num_states + i];
|
2012-06-12 22:46:12 +08:00
|
|
|
m_csi[0 * num_states + i] = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/******************************************************************/
|
|
|
|
/* Viterbi recursion */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ( m_HMMType == _CV_CAUSAL ) //causal model
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
int t;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (t = 1 ; t < m_maxNumObs; t++)
|
|
|
|
{
|
|
|
|
// evaluate self-to-self transition for state 0
|
|
|
|
m_Gamma[t * num_states + 0] = m_Gamma[(t-1) * num_states + 0] + m_a[0];
|
|
|
|
m_csi[t * num_states + 0] = 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 1; j < num_states; j++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
float self = m_Gamma[ (t-1) * num_states + j] + m_a[ j * num_states + j];
|
|
|
|
float prev = m_Gamma[ (t-1) * num_states +(j-1)] + m_a[ (j-1) * num_states + j];
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ( prev > self )
|
|
|
|
{
|
|
|
|
m_csi[t * num_states + j] = j-1;
|
|
|
|
m_Gamma[t * num_states + j] = prev;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_csi[t * num_states + j] = j;
|
|
|
|
m_Gamma[t * num_states + j] = self;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
m_Gamma[t * num_states + j] = m_Gamma[t * num_states + j] + m_b[t * num_states + j];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
else if ( m_HMMType == _CV_ERGODIC ) //ergodic model
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
int t;
|
|
|
|
for (t = 1 ; t < m_maxNumObs; t++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 0; j < num_states; j++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
m_Gamma[ t*num_states + j] = m_Gamma[(t-1) * num_states + 0] + m_a[0*num_states+j];
|
|
|
|
m_csi[t *num_states + j] = 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 1; i < num_states; i++)
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
float currGamma = m_Gamma[(t-1) *num_states + i] + m_a[i *num_states + j];
|
2010-05-12 01:44:00 +08:00
|
|
|
if (currGamma > m_Gamma[t *num_states + j])
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
m_Gamma[t * num_states + j] = currGamma;
|
|
|
|
m_csi[t * num_states + j] = i;
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
m_Gamma[t *num_states + j] = m_Gamma[t *num_states + j] + m_b[t * num_states + j];
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for( last_obs = m_minNumObs-1, i = 0; last_obs < m_maxNumObs; last_obs++, i++ )
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* Viterbi termination */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ( m_ProbType == _CV_LAST_STATE )
|
|
|
|
{
|
|
|
|
m_MaxGamma[i] = m_Gamma[last_obs * num_states + num_states - 1];
|
|
|
|
q[i][last_obs] = num_states - 1;
|
|
|
|
}
|
|
|
|
else if( m_ProbType == _CV_BEST_STATE )
|
|
|
|
{
|
|
|
|
int k;
|
2012-06-12 22:46:12 +08:00
|
|
|
q[i][last_obs] = 0;
|
|
|
|
m_MaxGamma[i] = m_Gamma[last_obs * num_states + 0];
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for(k = 1; k < num_states; k++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
if ( m_Gamma[last_obs * num_states + k] > m_MaxGamma[i] )
|
|
|
|
{
|
|
|
|
m_MaxGamma[i] = m_Gamma[last_obs * num_states + k];
|
|
|
|
q[i][last_obs] = k;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/******************************************************************/
|
|
|
|
/* Viterbi backtracking */
|
|
|
|
for (t = last_obs-1; t >= 0; t--)
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
q[i][t] = m_csi[(t+1) * num_states + q[i][t+1] ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* memory free */
|
|
|
|
cvFree( &m_pi );
|
|
|
|
cvFree( &m_csi );
|
2012-06-12 22:46:12 +08:00
|
|
|
icvDeleteMatrix( m_Gamma );
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return CV_NO_ERR;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*F///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: icvEViterbi
|
|
|
|
// Purpose: The function calculates the embedded Viterbi algorithm
|
2012-06-12 22:46:12 +08:00
|
|
|
// for 1 image
|
2010-05-12 01:44:00 +08:00
|
|
|
// Context:
|
2012-06-12 22:46:12 +08:00
|
|
|
// Parameters:
|
2010-05-12 01:44:00 +08:00
|
|
|
// obs_info - observations
|
|
|
|
// hmm - HMM
|
2012-06-12 22:46:12 +08:00
|
|
|
//
|
|
|
|
// Returns: the Embedded Viterbi probability (float)
|
2010-05-12 01:44:00 +08:00
|
|
|
// and do state segmentation of observations
|
|
|
|
//
|
2012-06-12 22:46:12 +08:00
|
|
|
// Notes:
|
2010-05-12 01:44:00 +08:00
|
|
|
//F*/
|
|
|
|
static float CV_STDCALL icvEViterbi( CvImgObsInfo* obs_info, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
int i, j, counter;
|
|
|
|
float log_likelihood;
|
|
|
|
|
|
|
|
float inv_obs_x = 1.f / obs_info->obs_x;
|
|
|
|
|
|
|
|
CvEHMMState* first_state = hmm->u.ehmm->u.state;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* memory allocation for superB */
|
|
|
|
CvMatr32f superB = icvCreateMatrix_32f(hmm->num_states, obs_info->obs_y );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* memory allocation for q */
|
|
|
|
int*** q = (int***)cvAlloc( hmm->num_states * sizeof(int**) );
|
|
|
|
int* super_q = (int*)cvAlloc( obs_info->obs_y * sizeof(int) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < hmm->num_states; i++)
|
|
|
|
{
|
|
|
|
q[i] = (int**)cvAlloc( obs_info->obs_y * sizeof(int*) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 0; j < obs_info->obs_y ; j++)
|
|
|
|
{
|
|
|
|
q[i][j] = (int*)cvAlloc( obs_info->obs_x * sizeof(int) );
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* start Viterbi segmentation */
|
|
|
|
for (i = 0; i < hmm->num_states; i++)
|
|
|
|
{
|
|
|
|
CvEHMM* ehmm = &(hmm->u.ehmm[i]);
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 0; j < obs_info->obs_y; j++)
|
|
|
|
{
|
|
|
|
float max_gamma;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* 1D HMM Viterbi segmentation */
|
2012-06-12 22:46:12 +08:00
|
|
|
icvViterbiSegmentation( ehmm->num_states, obs_info->obs_x,
|
|
|
|
ehmm->transP, ehmm->obsProb[j], 0,
|
2010-05-12 01:44:00 +08:00
|
|
|
_CV_LAST_STATE, &q[i][j], obs_info->obs_x,
|
|
|
|
obs_info->obs_x, &max_gamma);
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
superB[j * hmm->num_states + i] = max_gamma * inv_obs_x;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* perform global Viterbi segmentation (i.e. process higher-level HMM) */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
icvViterbiSegmentation( hmm->num_states, obs_info->obs_y,
|
|
|
|
hmm->transP, superB, 0,
|
2010-05-12 01:44:00 +08:00
|
|
|
_CV_LAST_STATE, &super_q, obs_info->obs_y,
|
|
|
|
obs_info->obs_y, &log_likelihood );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
log_likelihood /= obs_info->obs_y ;
|
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
counter = 0;
|
|
|
|
/* assign new state to observation vectors */
|
|
|
|
for (i = 0; i < obs_info->obs_y; i++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 0; j < obs_info->obs_x; j++, counter++)
|
|
|
|
{
|
|
|
|
int superstate = super_q[i];
|
|
|
|
int state = (int)(hmm->u.ehmm[superstate].u.state - first_state);
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
obs_info->state[2 * counter] = superstate;
|
|
|
|
obs_info->state[2 * counter + 1] = state + q[superstate][i][j];
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* memory deallocation for superB */
|
|
|
|
icvDeleteMatrix( superB );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/*memory deallocation for q */
|
|
|
|
for (i = 0; i < hmm->num_states; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < obs_info->obs_y ; j++)
|
|
|
|
{
|
|
|
|
cvFree( &q[i][j] );
|
|
|
|
}
|
|
|
|
cvFree( &q[i] );
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
cvFree( &q );
|
|
|
|
cvFree( &super_q );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return log_likelihood;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
static CvStatus CV_STDCALL
|
|
|
|
icvEstimateHMMStateParams( CvImgObsInfo** obs_info_array, int num_img, CvEHMM* hmm )
|
|
|
|
{
|
|
|
|
/* compute gamma, weights, means, vars */
|
|
|
|
int k, i, j, m;
|
|
|
|
int total = 0;
|
|
|
|
int vect_len = obs_info_array[0]->obs_size;
|
|
|
|
|
|
|
|
float start_log_var_val = LN2PI * vect_len;
|
|
|
|
|
|
|
|
CvVect32f tmp_vect = icvCreateVector_32f( vect_len );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvEHMMState* first_state = hmm->u.ehmm[0].u.state;
|
|
|
|
|
|
|
|
assert( sizeof(float) == sizeof(int) );
|
|
|
|
|
|
|
|
for(i = 0; i < hmm->num_states; i++ )
|
|
|
|
{
|
|
|
|
total+= hmm->u.ehmm[i].num_states;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************Gamma***********************/
|
|
|
|
/* initialize gamma */
|
|
|
|
for( i = 0; i < total; i++ )
|
|
|
|
{
|
|
|
|
for (m = 0; m < first_state[i].num_mix; m++)
|
|
|
|
{
|
|
|
|
((int*)(first_state[i].weight))[m] = 0;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* maybe gamma must be computed in mixsegm process ?? */
|
|
|
|
|
|
|
|
/* compute gamma */
|
|
|
|
for (k = 0; k < num_img; k++)
|
|
|
|
{
|
|
|
|
CvImgObsInfo* info = obs_info_array[k];
|
|
|
|
int num_obs = info->obs_y * info->obs_x;
|
|
|
|
|
|
|
|
for (i = 0; i < num_obs; i++)
|
|
|
|
{
|
|
|
|
int state, mixture;
|
|
|
|
state = info->state[2*i + 1];
|
|
|
|
mixture = info->mix[i];
|
2012-06-12 22:46:12 +08:00
|
|
|
/* computes gamma - number of observations corresponding
|
|
|
|
to every mixture of every state */
|
|
|
|
((int*)(first_state[state].weight))[mixture] += 1;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
/***************Mean and Var***********************/
|
|
|
|
/* compute means and variances of every item */
|
|
|
|
/* initially variance placed to inv_var */
|
|
|
|
/* zero mean and variance */
|
|
|
|
for (i = 0; i < total; i++)
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
memset( (void*)first_state[i].mu, 0, first_state[i].num_mix * vect_len *
|
2010-05-12 01:44:00 +08:00
|
|
|
sizeof(float) );
|
2012-06-12 22:46:12 +08:00
|
|
|
memset( (void*)first_state[i].inv_var, 0, first_state[i].num_mix * vect_len *
|
2010-05-12 01:44:00 +08:00
|
|
|
sizeof(float) );
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* compute sums */
|
|
|
|
for (i = 0; i < num_img; i++)
|
|
|
|
{
|
|
|
|
CvImgObsInfo* info = obs_info_array[i];
|
|
|
|
int total_obs = info->obs_x * info->obs_y;
|
|
|
|
|
|
|
|
float* vector = info->obs;
|
|
|
|
|
|
|
|
for (j = 0; j < total_obs; j++, vector+=vect_len )
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
int state = info->state[2 * j + 1];
|
2012-06-12 22:46:12 +08:00
|
|
|
int mixture = info->mix[j];
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvVect32f mean = first_state[state].mu + mixture * vect_len;
|
|
|
|
CvVect32f mean2 = first_state[state].inv_var + mixture * vect_len;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
icvAddVector_32f( mean, vector, mean, vect_len );
|
|
|
|
for( k = 0; k < vect_len; k++ )
|
|
|
|
mean2[k] += vector[k]*vector[k];
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/*compute the means and variances */
|
|
|
|
/* assume gamma already computed */
|
|
|
|
for (i = 0; i < total; i++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
CvEHMMState* state = &(first_state[i]);
|
|
|
|
|
|
|
|
for (m = 0; m < state->num_mix; m++)
|
|
|
|
{
|
|
|
|
CvVect32f mu = state->mu + m * vect_len;
|
2012-06-12 22:46:12 +08:00
|
|
|
CvVect32f invar = state->inv_var + m * vect_len;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if ( ((int*)state->weight)[m] > 1)
|
|
|
|
{
|
|
|
|
float inv_gamma = 1.f/((int*)(state->weight))[m];
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
icvScaleVector_32f( mu, mu, vect_len, inv_gamma);
|
|
|
|
icvScaleVector_32f( invar, invar, vect_len, inv_gamma);
|
|
|
|
}
|
|
|
|
|
|
|
|
icvMulVectors_32f(mu, mu, tmp_vect, vect_len);
|
2012-06-12 22:46:12 +08:00
|
|
|
icvSubVector_32f( invar, tmp_vect, invar, vect_len);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* low bound of variance - 100 (Ara's experimental result) */
|
|
|
|
for( k = 0; k < vect_len; k++ )
|
|
|
|
{
|
|
|
|
invar[k] = (invar[k] > 100.f) ? invar[k] : 100.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute log_var */
|
|
|
|
state->log_var_val[m] = start_log_var_val;
|
|
|
|
for( k = 0; k < vect_len; k++ )
|
|
|
|
{
|
|
|
|
state->log_var_val[m] += (float)log( invar[k] );
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/* SMOLI 27.10.2000 */
|
|
|
|
state->log_var_val[m] *= 0.5;
|
|
|
|
|
|
|
|
|
|
|
|
/* compute inv_var = 1/sqrt(2*variance) */
|
|
|
|
icvScaleVector_32f(invar, invar, vect_len, 2.f );
|
|
|
|
cvbInvSqrt( invar, invar, vect_len );
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/***************Weights***********************/
|
|
|
|
/* normilize gammas - i.e. compute mixture weights */
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//compute weights
|
|
|
|
for (i = 0; i < total; i++)
|
2012-06-12 22:46:12 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
int gamma_total = 0;
|
|
|
|
float norm;
|
|
|
|
|
|
|
|
for (m = 0; m < first_state[i].num_mix; m++)
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
gamma_total += ((int*)(first_state[i].weight))[m];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
norm = gamma_total ? (1.f/(float)gamma_total) : 0.f;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (m = 0; m < first_state[i].num_mix; m++)
|
|
|
|
{
|
|
|
|
first_state[i].weight[m] = ((int*)(first_state[i].weight))[m] * norm;
|
2012-06-12 22:46:12 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
icvDeleteVector( tmp_vect);
|
2012-06-12 22:46:12 +08:00
|
|
|
return CV_NO_ERR;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
CvStatus icvLightingCorrection8uC1R( uchar* img, CvSize roi, int src_step )
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int width = roi.width;
|
|
|
|
int height = roi.height;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float x1, x2, y1, y2;
|
|
|
|
int f[3] = {0, 0, 0};
|
|
|
|
float a[3] = {0, 0, 0};
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float h1;
|
|
|
|
float h2;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float c1,c2;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float min = FLT_MAX;
|
|
|
|
float max = -FLT_MAX;
|
|
|
|
float correction;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float* float_img = icvAlloc( width * height * sizeof(float) );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
x1 = width * (width + 1) / 2.0f; // Sum (1, ... , width)
|
|
|
|
x2 = width * (width + 1 ) * (2 * width + 1) / 6.0f; // Sum (1^2, ... , width^2)
|
|
|
|
y1 = height * (height + 1)/2.0f; // Sum (1, ... , width)
|
|
|
|
y2 = height * (height + 1 ) * (2 * height + 1) / 6.0f; // Sum (1^2, ... , width^2)
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// extract grayvalues
|
|
|
|
for (i = 0; i < height; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < width; j++)
|
|
|
|
{
|
|
|
|
f[2] = f[2] + j * img[i*src_step + j];
|
|
|
|
f[1] = f[1] + i * img[i*src_step + j];
|
|
|
|
f[0] = f[0] + img[i*src_step + j];
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
h1 = (float)f[0] * (float)x1 / (float)width;
|
|
|
|
h2 = (float)f[0] * (float)y1 / (float)height;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
a[2] = ((float)f[2] - h1) / (float)(x2*height - x1*x1*height/(float)width);
|
|
|
|
a[1] = ((float)f[1] - h2) / (float)(y2*width - y1*y1*width/(float)height);
|
2012-06-12 22:46:12 +08:00
|
|
|
a[0] = (float)f[0]/(float)(width*height) - (float)y1*a[1]/(float)height -
|
2010-05-12 01:44:00 +08:00
|
|
|
(float)x1*a[2]/(float)width;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
for (i = 0; i < height; i++)
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
for (j = 0; j < width; j++)
|
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
correction = a[0] + a[1]*(float)i + a[2]*(float)j;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float_img[i*width + j] = img[i*src_step + j] - correction;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (float_img[i*width + j] < min) min = float_img[i*width+j];
|
|
|
|
if (float_img[i*width + j] > max) max = float_img[i*width+j];
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//rescaling to the range 0:255
|
|
|
|
c2 = 0;
|
|
|
|
if (max == min)
|
|
|
|
c2 = 255.0f;
|
|
|
|
else
|
|
|
|
c2 = 255.0f/(float)(max - min);
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
c1 = (-(float)min)*c2;
|
2012-06-12 22:46:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for (i = 0; i < height; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < width; j++)
|
|
|
|
{
|
|
|
|
int value = (int)floor(c2*float_img[i*width + j] + c1);
|
|
|
|
if (value < 0) value = 0;
|
|
|
|
if (value > 255) value = 255;
|
|
|
|
img[i*src_step + j] = (uchar)value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cvFree( &float_img );
|
|
|
|
return CV_NO_ERR;
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
CvStatus icvLightingCorrection( icvImage* img )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CvSize roi;
|
|
|
|
if ( img->type != IPL_DEPTH_8U || img->channels != 1 )
|
|
|
|
return CV_BADFACTOR_ERR;
|
|
|
|
|
|
|
|
roi = _cvSize( img->roi.width, img->roi.height );
|
2012-06-12 22:46:12 +08:00
|
|
|
|
|
|
|
return _cvLightingCorrection8uC1R( img->data + img->roi.y * img->step + img->roi.x,
|
2010-05-12 01:44:00 +08:00
|
|
|
roi, img->step );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
CV_IMPL CvEHMM*
|
|
|
|
cvCreate2DHMM( int *state_number, int *num_mix, int obs_size )
|
|
|
|
{
|
|
|
|
CvEHMM* hmm = 0;
|
|
|
|
|
|
|
|
IPPI_CALL( icvCreate2DHMM( &hmm, state_number, num_mix, obs_size ));
|
|
|
|
|
|
|
|
return hmm;
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvRelease2DHMM( CvEHMM ** hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvRelease2DHMM( hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL CvImgObsInfo*
|
|
|
|
cvCreateObsInfo( CvSize num_obs, int obs_size )
|
|
|
|
{
|
|
|
|
CvImgObsInfo *obs_info = 0;
|
|
|
|
|
|
|
|
IPPI_CALL( icvCreateObsInfo( &obs_info, num_obs, obs_size ));
|
|
|
|
|
|
|
|
return obs_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvReleaseObsInfo( CvImgObsInfo ** obs_info )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvReleaseObsInfo( obs_info ));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvUniformImgSegm( CvImgObsInfo * obs_info, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvUniformImgSegm( obs_info, hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvInitMixSegm( CvImgObsInfo ** obs_info_array, int num_img, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvInitMixSegm( obs_info_array, num_img, hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvEstimateHMMStateParams( CvImgObsInfo ** obs_info_array, int num_img, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvEstimateHMMStateParams( obs_info_array, num_img, hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvEstimateTransProb( CvImgObsInfo ** obs_info_array, int num_img, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvEstimateTransProb( obs_info_array, num_img, hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvEstimateObsProb( CvImgObsInfo * obs_info, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvEstimateObsProb( obs_info, hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL float
|
|
|
|
cvEViterbi( CvImgObsInfo * obs_info, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
if( (obs_info == NULL) || (hmm == NULL) )
|
|
|
|
CV_Error( CV_BadDataPtr, "Null pointer." );
|
|
|
|
|
|
|
|
return icvEViterbi( obs_info, hmm );
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvMixSegmL2( CvImgObsInfo ** obs_info_array, int num_img, CvEHMM * hmm )
|
|
|
|
{
|
|
|
|
IPPI_CALL( icvMixSegmL2( obs_info_array, num_img, hmm ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file */
|
|
|
|
|