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

ICV_DEF_FIND_STUMP_THRESHOLD( suffix, type, error )

时间:2015-07-08 19:02:26      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

源代码在opencv库里面的cvboost.cpp文件里面,该宏的总体思路是通过定义带参数的宏,把参数传递给宏内部的函数,然后进行一系列的计算,并最终根据一定的算法找到合适的阈值。具体解析如下:

#define ICV_DEF_FIND_STUMP_THRESHOLD( suffix, type, error )                              static int icvFindStumpThreshold_##suffix(                                                       uchar* data, size_t datastep,                                                            uchar* wdata, size_t wstep,                                                              uchar* ydata, size_t ystep,                                                              uchar* idxdata, size_t idxstep, int num,                                                 float* lerror,                                                                           float* rerror,                                                                           float* threshold, float* left, float* right,                                             float* sumw, float* sumwy, float* sumwyy )                                       {                                                                                            int found = 0;                                                                           float wyl  = 0.0F;                                                                       float wl   = 0.0F;                                                                       float wyyl = 0.0F;                                                                       float wyr  = 0.0F;                                                                       float wr   = 0.0F;                                                                                                                                                                float curleft  = 0.0F;                                                                   float curright = 0.0F;                                                                   float* prevval = NULL;                                                                   float* curval  = NULL;                                                                   float curlerror = 0.0F;                                                                  float currerror = 0.0F;                                                                                                                                                           int i = 0;                                                                               int idx = 0;                                                                                                                                                                      if( *sumw == FLT_MAX )                                                                   {                                                                                            /* calculate sums */                                                                     float *y = NULL;                                                                         float *w = NULL;                                                                         float wy = 0.0F;                                                                                                                                                                  *sumw   = 0.0F;                                                                          *sumwy  = 0.0F;                                                                          *sumwyy = 0.0F;                                                                          for( i = 0; i < num; i++ )                                                               {                                                                                            idx = (int) ( *((type*) (idxdata + i*idxstep)) );      //根据样本的数据和step确定其索引idx                                  w = (float*) (wdata + idx * wstep);                                                      *sumw += *w;                        //计算权重和                                          y = (float*) (ydata + idx * ystep);                                                      wy = (*w) * (*y);                   //计算权重w和类别y的乘积和                            *sumwy += wy;                                                                            *sumwyy += wy * (*y);                                                                }                                                                                    }                                                                                                                                                                                 for( i = 0; i < num; i++ )                                                               {                                                                                            idx = (int) ( *((type*) (idxdata + i*idxstep)) );                                        curval = (float*) (data + idx * datastep);                                                /* for debug purpose */                                                                 if( i > 0 ) assert( (*prevval) <= (*curval) );                                                                                                                                    wyr  = *sumwy - wyl;      //计算右边的权重与类别的乘积和                                   wr   = *sumw  - wl;       //计算右边的权重和                                                                                                                                       if( wl > 0.0 ) curleft = wyl / wl;    //计算左边不纯度                                                   else curleft = 0.0F;                                                                                                                                                              if( wr > 0.0 ) curright = wyr / wr;   //计算右边不纯度                                                   else curright = 0.0F;                                                                                                                                                             error                                                                                                                                                                             if( curlerror + currerror < (*lerror) + (*rerror) )      //如果当前的不纯度和小于原来的不纯度和                                {                                                                                            (*lerror) = curlerror;                                                                   (*rerror) = currerror;                                                                   *threshold = *curval;                                                                    if( i > 0 ) {                                                                                *threshold = 0.5F * (*threshold + *prevval);                                         }                                                                                        *left  = curleft;                                                                        *right = curright;                                                                       found = 1;                                                                           }                                                                                                                                                                                 do                                                                                       {                                                                                            wl  += *((float*) (wdata + idx * wstep));                                                wyl += (*((float*) (wdata + idx * wstep)))                                                   * (*((float*) (ydata + idx * ystep)));                                               wyyl += *((float*) (wdata + idx * wstep))                                                    * (*((float*) (ydata + idx * ystep)))                                                    * (*((float*) (ydata + idx * ystep)));                                           }                                                                                        while( (++i) < num &&                                                                        ( *((float*) (data + (idx =                                                                  (int) ( *((type*) (idxdata + i*idxstep))) ) * datastep))                                 == *curval ) );                                                                  --i;                                                                                     prevval = curval;                                                                    } /* for each value */                                                                                                                                                            return found;                                                                        } 


版权声明:本文为博主原创文章,未经博主允许不得转载。

ICV_DEF_FIND_STUMP_THRESHOLD( suffix, type, error )

标签:

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

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