opencv源代码分析:icvGetTrainingDataCallback简单介绍

/*
*函数icvGetTrainingDataCallback介绍
*功能:对全部样本计算特征编号从first開始的num个特征,并保存到mat里。

*输入: *CvMat* mat矩阵样本总数个行,num个列。保存每一个样本的num个特征值。

*First:特征类型编号的開始处 *Num:要计算的特征类型个数。

*Userdata:积分矩阵和权重、特征模板等信息。 *输出: *CvMat* mat矩阵样本总数个行。num个列。保存每一个样本的num个特征值。 */ static void icvGetTrainingDataCallback( CvMat* mat, CvMat* sampleIdx, CvMat*, int first, int num, void* userdata ) { int i = 0; int j = 0; float val = 0.0F; float normfactor = 0.0F; CvHaarTrainingData* training_data; CvIntHaarFeatures* haar_features; #ifdef CV_COL_ARRANGEMENT assert( mat->rows >= num ); #else assert( mat->cols >= num ); #endif //userdata = cvUserdata( data, haarFeatures ) //userdata包括了參与训练的积分图和特征。其指针应该是用于回调的用户參数 training_data = ((CvUserdata*) userdata)->trainingData; haar_features = ((CvUserdata*) userdata)->haarFeatures; if( sampleIdx == NULL ) { int num_samples; #ifdef CV_COL_ARRANGEMENT num_samples = mat->cols; #else num_samples = mat->rows; #endif for( i = 0; i < num_samples; i++ )//样本数量 { for( j = 0; j < num; j++ )//每一个样本的第j个特征 { //计算一个样本(积分图为sum和tilted)的一个HaarFeature,并返回该值 val = cvEvalFastHaarFeature( ( haar_features->fastfeature + first + j ), (sum_type*) (training_data->sum.data.ptr + i * training_data->sum.step), (sum_type*) (training_data->tilted.data.ptr + i * training_data->tilted.step) ); normfactor = training_data->normfactor.data.fl[i]; val = ( normfactor == 0.0F ) ? 0.0F : (val / normfactor); #ifdef CV_COL_ARRANGEMENT CV_MAT_ELEM( *mat, float, j, i ) = val; #else CV_MAT_ELEM( *mat, float, i, j ) = val; #endif } } } else { uchar* idxdata = NULL; size_t step = 0; int numidx = 0; int idx = 0; assert( CV_MAT_TYPE( sampleIdx->type ) == CV_32FC1 ); idxdata = sampleIdx->data.ptr; if( sampleIdx->rows == 1 ) { step = sizeof( float ); numidx = sampleIdx->cols; } else { step = sampleIdx->step; numidx = sampleIdx->rows; } for( i = 0; i < numidx; i++ ) { for( j = 0; j < num; j++ ) { idx = (int)( *((float*) (idxdata + i * step)) ); val = cvEvalFastHaarFeature( ( haar_features->fastfeature + first + j ), (sum_type*) (training_data->sum.data.ptr + idx * training_data->sum.step), (sum_type*) (training_data->tilted.data.ptr + idx * training_data->tilted.step) ); normfactor = training_data->normfactor.data.fl[idx]; val = ( normfactor == 0.0F ) ? 0.0F : (val / normfactor); #ifdef CV_COL_ARRANGEMENT CV_MAT_ELEM( *mat, float, j, idx ) = val; #else CV_MAT_ELEM( *mat, float, idx, j ) = val; #endif } } } #if 0 /*def CV_VERBOSE*/ if( first % 5000 == 0 ) { fprintf( stderr, "%3d%%\r", (int) (100.0 * first / haar_features->count) ); fflush( stderr ); } #endif /* CV_VERBOSE */ }

posted @ 2017-04-15 18:11  lytwajue  阅读(211)  评论(0编辑  收藏  举报