请看注释。这个函数,是人脸识别主函数,里面出现过的函数之一,作用是初始化分类器的数据,就是一个xml文件的数据初始化。


 static CvHidHaarClassifierCascade* icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
{
CvRect* ipp_features = ;//定义一个矩形框指针
float *ipp_weights = , *ipp_thresholds = , *ipp_val1 = , *ipp_val2 = ;//单精度浮点数指针4个
int* ipp_counts = ;//整形指针1个 CvHidHaarClassifierCascade* out = ;//最终返回的值 int i, j, k, l;//for循环的控制变量
int datasize;//数据大小
int total_classifiers = ;//总的分类器数目
int total_nodes = ;
char errorstr[];//错误信息数组
CvHidHaarClassifier* haar_classifier_ptr;//级联分类器指针
CvHidHaarTreeNode* haar_node_ptr;
CvSize orig_window_size;//提取窗口的大小
int has_tilted_features = ;
int max_count = ; if( !CV_IS_HAAR_CLASSIFIER(cascade) )//判断传进来的分类器文件是否真正确
CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" ); if( cascade->hid_cascade )//判断改分类器xml文件是否已经被初始化了
CV_Error( CV_StsError, "hid_cascade has been already created" ); if( !cascade->stage_classifier )//如果没有阶级分类器,报错
CV_Error( CV_StsNullPtr, "" ); if( cascade->count <= )//如果分类器的阶级数<=0,报错
CV_Error( CV_StsOutOfRange, "Negative number of cascade stages" ); orig_window_size = cascade->orig_window_size;//获取识别窗口的大小 /* check input structure correctness and calculate total memory size needed for
internal representation of the classifier cascade */ for( i = ; i < cascade->count; i++ )//对xml文件里面的每阶段的stage进行循环提取相关数据
{
CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
//获取每次进入循环的后阶段的子分类器,以haarcascade_upperbody.xml 为例子,count是30,stage_classifier的count是20 if( !stage_classifier->classifier ||//判断阶段分类器、子分类器及其stage 层数 是否合法
stage_classifier->count <= )
{
sprintf( errorstr, "header of the stage classifier #%d is invalid "
"(has null pointers or non-positive classfier count)", i );
CV_Error( CV_StsError, errorstr );
} max_count = MAX( max_count, stage_classifier->count );//获取子分类器stage的数目,以haarcascade_upperbody.xml为例,是20
total_classifiers += stage_classifier->count;//统计出总的子分类器的stage数目,即tree,再统计 for( j = ; j < stage_classifier->count; j++ )
//这个for循环主要是进入到子分类器tree里面的数据提取并且对其正确性的判断,
//循环条件为字stage数目,以haarcascade_upperbody.xml为例,为20
{
CvHaarClassifier* classifier = stage_classifier->classifier + j;//同上,找到此时循环的tree total_nodes += classifier->count;//计算出此时循环的tree子分类器的root node 数目,再统计。以haarcascade_upperbody.xml为例,每个tree的node是1
for( l = ; l < classifier->count; l++ )
//这个是关键循环,主数据的获取
//以haarcascade_upperbody.xml为例,此时classifier->count=1,循环一次,进入里面获取关键数据
{
for( k = ; k < CV_HAAR_FEATURE_MAX; k++ )//CV_HAAR_FEATURE_MAX = 3,循环三次,feature的最大数目,以haarcascade_upperbody.xml为例,只有1个
{
if( classifier->haar_feature[l].rect[k].r.width )
//逐层递归,先找feature,再找它里面的rect标签里面的矩阵row,行的宽度
//以haarcascade_upperbody.xml为例,是2
{
CvRect r = classifier->haar_feature[l].rect[k].r;//把此时row矩阵框赋给r
int tilted = classifier->haar_feature[l].tilted;//获取xml标签tited的值
has_tilted_features |= tilted != ;//|是位运算,例如0|1=1,这行的作用是判断has和tilted那个是1,还不知道其意义何在
if( r.width < || r.height < || r.y < ||
r.x + r.width > orig_window_size.width
||
(!tilted &&
(r.x < || r.y + r.height > orig_window_size.height))
||
(tilted && (r.x - r.height < ||
r.y + r.width + r.height > orig_window_size.height)))
//这个if语句是对feature里面的数据矩形的各方面判断,包括矩形的宽、高、等
//矩形# %d的分类器# %d”“级分类器# %d是不是在里面”“参考(原创)级联窗口”
{
sprintf( errorstr, "rectangle #%d of the classifier #%d of "
"the stage classifier #%d is not inside "
"the reference (original) cascade window", k, j, i );
CV_Error( CV_StsNullPtr, errorstr );
}
}
}
}
}
}
//上面数据的判断结束后,到这里 datasize = sizeof(CvHidHaarClassifierCascade) +//获取整个分类器,xml文件的数据大小
sizeof(CvHidHaarStageClassifier)*cascade->count +
sizeof(CvHidHaarClassifier) * total_classifiers +
sizeof(CvHidHaarTreeNode) * total_nodes +
sizeof(void*)*(total_nodes + total_classifiers); out = (CvHidHaarClassifierCascade*)cvAlloc( datasize );//给最终返回的变量分配内存大小
memset( out, , sizeof(*out) );//对变量初始化,全部填充0 //下面是逐个赋值,初始化头部
/* init header */
out->count = cascade->count;//新分类器out的stage数目
out->stage_classifier = (CvHidHaarStageClassifier*)(out + );//子分类器tree的数目
haar_classifier_ptr = (CvHidHaarClassifier*)(out->stage_classifier + cascade->count);//tree指针
haar_node_ptr = (CvHidHaarTreeNode*)(haar_classifier_ptr + total_classifiers);//tree里面node的指针 out->isStumpBased = ;//布尔类型,true
out->has_tilted_features = has_tilted_features;
out->is_tree = ; /* initialize internal representation */
for( i = ; i < cascade->count; i++ )
{
CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
CvHidHaarStageClassifier* hid_stage_classifier = out->stage_classifier + i; hid_stage_classifier->count = stage_classifier->count;
hid_stage_classifier->threshold = stage_classifier->threshold - icv_stage_threshold_bias;
hid_stage_classifier->classifier = haar_classifier_ptr;
hid_stage_classifier->two_rects = ;
haar_classifier_ptr += stage_classifier->count; hid_stage_classifier->parent = (stage_classifier->parent == -)
? NULL : out->stage_classifier + stage_classifier->parent;
hid_stage_classifier->next = (stage_classifier->next == -)
? NULL : out->stage_classifier + stage_classifier->next;
hid_stage_classifier->child = (stage_classifier->child == -)
? NULL : out->stage_classifier + stage_classifier->child; out->is_tree |= hid_stage_classifier->next != NULL; for( j = ; j < stage_classifier->count; j++ )
{
CvHaarClassifier* classifier = stage_classifier->classifier + j;
CvHidHaarClassifier* hid_classifier = hid_stage_classifier->classifier + j;
int node_count = classifier->count;
float* alpha_ptr = (float*)(haar_node_ptr + node_count); hid_classifier->count = node_count;
hid_classifier->node = haar_node_ptr;
hid_classifier->alpha = alpha_ptr; for( l = ; l < node_count; l++ )
{
CvHidHaarTreeNode* node = hid_classifier->node + l;
CvHaarFeature* feature = classifier->haar_feature + l;
memset( node, -, sizeof(*node) );
node->threshold = classifier->threshold[l];
node->left = classifier->left[l];
node->right = classifier->right[l]; if( fabs(feature->rect[].weight) < DBL_EPSILON ||
feature->rect[].r.width == ||
feature->rect[].r.height == )
memset( &(node->feature.rect[]), , sizeof(node->feature.rect[]) );
else
hid_stage_classifier->two_rects = ;
} memcpy( alpha_ptr, classifier->alpha, (node_count+)*sizeof(alpha_ptr[]));
haar_node_ptr =
(CvHidHaarTreeNode*)cvAlignPtr(alpha_ptr+node_count+, sizeof(void*)); out->isStumpBased &= node_count == ;
}
}
/*
#ifdef HAVE_IPP
int can_use_ipp = !out->has_tilted_features && !out->is_tree && out->isStumpBased; if( can_use_ipp )
{
int ipp_datasize = cascade->count*sizeof(out->ipp_stages[0]);
float ipp_weight_scale=(float)(1./((orig_window_size.width-icv_object_win_border*2)*
(orig_window_size.height-icv_object_win_border*2))); out->ipp_stages = (void**)cvAlloc( ipp_datasize );
memset( out->ipp_stages, 0, ipp_datasize ); ipp_features = (CvRect*)cvAlloc( max_count*3*sizeof(ipp_features[0]) );
ipp_weights = (float*)cvAlloc( max_count*3*sizeof(ipp_weights[0]) );
ipp_thresholds = (float*)cvAlloc( max_count*sizeof(ipp_thresholds[0]) );
ipp_val1 = (float*)cvAlloc( max_count*sizeof(ipp_val1[0]) );
ipp_val2 = (float*)cvAlloc( max_count*sizeof(ipp_val2[0]) );
ipp_counts = (int*)cvAlloc( max_count*sizeof(ipp_counts[0]) ); for( i = 0; i < cascade->count; i++ )
{
CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
for( j = 0, k = 0; j < stage_classifier->count; j++ )
{
CvHaarClassifier* classifier = stage_classifier->classifier + j;
int rect_count = 2 + (classifier->haar_feature->rect[2].r.width != 0); ipp_thresholds[j] = classifier->threshold[0];
ipp_val1[j] = classifier->alpha[0];
ipp_val2[j] = classifier->alpha[1];
ipp_counts[j] = rect_count; for( l = 0; l < rect_count; l++, k++ )
{
ipp_features[k] = classifier->haar_feature->rect[l].r;
//ipp_features[k].y = orig_window_size.height - ipp_features[k].y - ipp_features[k].height;
ipp_weights[k] = classifier->haar_feature->rect[l].weight*ipp_weight_scale;
}
} if( ippiHaarClassifierInitAlloc_32f( (IppiHaarClassifier_32f**)&out->ipp_stages[i],
(const IppiRect*)ipp_features, ipp_weights, ipp_thresholds,
ipp_val1, ipp_val2, ipp_counts, stage_classifier->count ) < 0 )
break;
} if( i < cascade->count )
{
for( j = 0; j < i; j++ )
if( out->ipp_stages[i] )
ippiHaarClassifierFree_32f( (IppiHaarClassifier_32f*)out->ipp_stages[i] );
cvFree( &out->ipp_stages );
}
}
#endif
*/
cascade->hid_cascade = out;
assert( (char*)haar_node_ptr - (char*)out <= datasize ); cvFree( &ipp_features );
cvFree( &ipp_weights );
cvFree( &ipp_thresholds );
cvFree( &ipp_val1 );
cvFree( &ipp_val2 );
cvFree( &ipp_counts ); return out;
}

opencv 之 icvCreateHidHaarClassifierCascade 分类器信息初始化函数部分详细代码注释。的更多相关文章

  1. 常见的JS手写函数汇总(代码注释、持续更新)

    最近在复习面试中常见的JS手写函数,顺便进行代码注释和总结,方便自己回顾也加深记,内容也会陆陆续续进行补充和改善. 一.手写深拷贝 <script> const obj1 = { name ...

  2. atitit  opencv apiattilax总结 约500个函数 .xlsx

    atitit  opencv apiattilax总结 约500个函数 .xlsx 1.1. CxCore中文参考手册 1 1.2. 机器学习中文参考手册  knn  svm  1 1.3. CvAu ...

  3. iOS: 聊聊 Designated Initializer(指定初始化函数)

    iOS: 聊聊 Designated Initializer(指定初始化函数) 一.iOS的对象创建和初始化 iOS 中对象创建是分两步完成: 分配内存 初始化对象的成员变量 我们最熟悉的创建NSOb ...

  4. 第3阶段——内核启动分析之start_kernel初始化函数(5)

    内核启动分析之start_kernel初始化函数(init/main.c) stext函数启动内核后,就开始进入start_kernel初始化各个函数, 下面只是浅尝辄止的描述一下函数的功能,很多函数 ...

  5. 金融量化分析【day112】:量化平台的使用-初始化函数

    一.set_benchmark - 设置基准 1.实现代码 # 导入函数库 import jqdata #初始化函数,设定基准等等 def initialize(context): set_bench ...

  6. 《扩展和嵌入python解释器》1.4 模块方法表和初始化函数

    <扩展和嵌入python解释器>1.4 模块方法表和初始化函数   1.4 模块方法表和初始化函数 下面,我演示如何从Python程序调用spam_system().首先,我们需要在’方法 ...

  7. 痞子衡嵌入式:深扒IAR启动函数流程之段初始化函数__iar_data_init3实现

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IAR启动函数流程里的段初始化函数__iar_data_init3实现. 本篇是 <IAR启动函数流程及其__low_level_ ...

  8. linux-0.11分析:init文件 main.c的第二个初始化函数trap_init() 第五篇随笔

    2.第二个初始化函数,trap_init() 参考 [github这个博主的 厉害][ https://github.com/sunym1993/flash-linux0.11-talk ] kern ...

  9. linux-0.11分析:init文件 main.c的第一个初始化函数mem_int 第四篇随笔

    init文件夹 mian.c 参考 [github这个博主的 厉害][ https://github.com/sunym1993/flash-linux0.11-talk ] 首先先看看这个mian. ...

随机推荐

  1. angular 中父元素ng-repeat后子元素ng-click失效

    在angular中使用ng-repeat后ng-click失效,今天在这个上面踩坑了.特此记录一下. 因为ng-repeat创造了新的SCOPE.如果要使用这个scope的话就必须使用$parent来 ...

  2. “LC.exe”错误

    错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...

  3. 简单正则匹配QQ邮箱

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <script src ...

  4. Html5 中的新语义标签

    1.1 语义标签对于我们并不陌生,如<p>表示一个段落.<ul>表示一个无序列表<h1> ~ <h6>表示一系列标题等,在此基础上HTML5增加了大量更 ...

  5. Weblogic反序列化漏洞补丁更新解决方案

    Weblogic反序列化漏洞的解决方案基于网上给的方案有两种: 第一种方案如下 使用SerialKiller替换进行序列化操作的ObjectInputStream类; 在不影响业务的情况下,临时删除掉 ...

  6. ASP.NET Web API 2.1支持Binary JSON(Bson)

    ASP.NET Web API 2.1内建支持XML.Json.Bson.form-urlencoded的MiME type,今天重点介绍下Bson.BSON是由10gen开发的一个数据格式,目前主要 ...

  7. UI控件(UIWebView)

    本文主要记录UIWebView三方面内容: 1.基本的加载网页链接或文件: 2.网页js调用原生,也就是Cordova混合架构的原理: 3.原生调用js程序: 原生部分主要代码: @implement ...

  8. Emacs 配置文件

    以下是我整理的 emacs 配置文件,供刚开始玩 emacs 的同学参考.网上有人说:emacs 是神的编辑器,如果能够用到这样的编辑器,那这个人就是神了.从我个人的经验来看,emacs 是一把利器, ...

  9. 微小,但是美好的改变 G2 2.2发布

    G2 2.2.0, 『微小,但是美好的改变.』 # 一.丰富可视化交互形式 #### 选择: [图形的单选.多选](https://g2.alipay.com/tutorial/geom-select ...

  10. java轻量级IOC框架Guice

    Google-Guice入门介绍(较为清晰的说明了流程):http://blog.csdn.net/derekjiang/article/details/7231490 使用Guice,需要添加第三方 ...