码迷,mamicode.com
首页 > 其他好文 > 详细

opencv源码分析:icvGetTrainingDataCallback简介

时间:2015-06-23 20:13:15      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:icvgettrainingdataca   opencv源码分析   cvhaartraining.cpp   

/*
*函数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 */
}


opencv源码分析:icvGetTrainingDataCallback简介

标签:icvgettrainingdataca   opencv源码分析   cvhaartraining.cpp   

原文地址:http://blog.csdn.net/ding977921830/article/details/46607703

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!