《OpenCV学习笔记A》由会员分享,可在线阅读,更多相关《OpenCV学习笔记A(35页珍藏版)》请在金锄头文库上搜索。
1、分类器模式识别目标检测方法最初由 Paul Viola Viola01提出,并由Rainer Lienhart Lienhart02对这一方法进展了改善. 首先,利用样本大约几百幅样本图片的 harr 特征进展分类器训练,得到一个级联的 boosted 分类器。训练样本分为正例样本和反例样本,其中正例样本是指待检目标样本(例如人脸或汽车等),反例样本指其它任意图片,全部的样本图片都被归一化为同样的尺寸大小(例如, 20x20)。分类器训练完以后,就可以应用于输入图像中的感爱好区域(与训练样本一样的尺寸)的检测。检测到目标区域(汽车或人脸)分类器输出为 1,否那么输出为 0。为了检测整副图像,可
2、以在图像中移动搜寻窗口,检测每一个位置来确定可能的目标。 为了搜寻不同大小的目标物体,分类器被设计为可以进展尺寸转变,这样比转变待检图像的尺寸大小更为有效。所以,为了在图像中检测未知大小的目标物体,扫描程序通常需要用不同比例大小的搜寻窗口对图片进展几次扫描。分类器中的“级联”是指最终的分类器是由几个简洁分类器级联组成。在图像检测中,被检窗口依次通过每一级分类器, 这样在前面几层的检测中大局部的候选区域就被排解了,全部通过每一级分类器检测的区域即为目标区域。 目前支持这种分类器的 boosting 技术有四种: Discrete Adaboost, Real Adaboost, Gentle A
3、daboost and Logitboost。“boosted“ 即指级联分类器的每一层都可以从中选取一个 boosting 算法(权重投票),并利用根底分类器的自我训练得到。根底分类器是至少有两个叶结点的决策树分类器。 Haar 特征是根底分类器的输入,主要描述如下。目前的算法主要利用下面的 Harr 特征。每个特定分类器所使用的特征用外形、感爱好区域中的位置以及比例系数这里的比例系数跟检测时候承受的比例系数是不一样的,尽管最终会取两个系数的乘积值来定义。例如在第三行特征(2c)的状况下,响应计算为复盖全部特征整个矩形框(包括两个白色矩形框和一个黑色矩形框)象素的和减去黑色矩形框内象素和的三
4、倍 。每个矩形框内的象素和都可以通过积分图象很快的计算出来。(观察下面和对 cvIntegral 的描述).通过 HaarFaceDetect 的演示版可以观察目标检测的工作状况。下面只是检测局部的参考手册。 haartraining 是它的一个单独的应用,可以用来对系列样本训练级联的 boosted 分类器。具体观察opencv/apps/haartraining。CvHaarFeature, CvHaarClassifier, CvHaarStageClassifier, CvHaarClassifierCascade Boosted Haar 分类器构造#define CV_HAAR_F
5、EATURE_MAX3/* 一个 harr 特征由 23 个具有相应权重的矩形组成a haar feature consists of 2-3 rectangles with appropriate weights */typedef struct CvHaarFeatureinttilted;/* 0 means up-right feature, 1 means 45-rotated feature */* 2-3 rectangles with weights of opposite signs andwith absolute values inversely proportional
6、 to the areas of the rectangles. if rect2.weight !=0, thenthe feature consists of 3 rectangles, otherwise it consists of 2 */ structCvRect r; float weight; rectCV_HAAR_FEATURE_MAX;CvHaarFeature;/* a single tree classifier (stump in the simplest case) that returns the response for the featureat the p
7、articular image location (i.e. pixel sum over subrectangles of the window)and gives outa value depending on the responce */ typedef struct CvHaarClassifierint count;/* number of nodes in the decision tree */* these are “parallel“ arrays. Every index icorresponds to a node of the decision tree (root
8、has 0-th index).lefti - index of the left child (or negated index if the left child is a leaf) righti - index of the right child (or negated index if the right child is aleaf)thresholdi - branch threshold. if feature responce is = threshold, leftbranchis chosen, otherwise right branch is chosed. alp
9、hai - output value correponding to the leaf. */CvHaarFeature* haar_feature; float* threshold;int* left; int* right; float* alpha;CvHaarClassifier;/* a boosted battery of classifiers(=stage classifier): the stage classifier returns 1if the sum of the classifiers” responcesis greater than threshold an
10、d 0 otherwise */ typedef struct CvHaarStageClassifierintcount;/* number of classifiers in the battery */ float threshold; /* threshold for the boosted classifier */ CvHaarClassifier* classifier; /* array of classifiers */* these fields are used for organizing trees of stage classifiers, rather than
11、just stright cascades */int next; int child; int parent;CvHaarStageClassifier;typedef struct CvHidHaarClassifierCascade CvHidHaarClassifierCascade;/* cascade or tree of stage classifiers */ typedef struct CvHaarClassifierCascadeintflags; /* signature */intcount; /* number of stages */CvSize orig_win
12、dow_size; /* original object size (the cascade is trained for) */* these two parameters are set by cvSetImagesForHaarClassifierCascade */ CvSize real_window_size; /* current object size */double scale; /* current scale */CvHaarStageClassifier* stage_classifier; /* array of stage classifiers */ CvHid
13、HaarClassifierCascade* hid_cascade; /* hidden optimized representation of thecascade,created bycvSetImagesForHaarClassifierCascade */CvHaarClassifierCascade;全部的构造都代表一个级联 boosted Haar 分类器。级联有下面的等级构造:Cascade:Stage1:Classifier11:Feature11 Classifier12:Feature12.Stage2:Classifier21:Feature21.整个等级可以手工构建,
14、也可以利用函数 cvLoadHaarClassifierCascade 从已有的磁盘文件或嵌入式基中导入。cvLoadHaarClassifierCascade从文件中装载训练好的级联分类器或者从 OpenCV 中嵌入的分类器数据库中导入CvHaarClassifierCascade* cvLoadHaarClassifierCascade(const char* directory, CvSize orig_window_size );directory训练好的级联分类器的路径orig_window_size级联分类器训练中承受的检测目标的尺寸。由于这个信息没有在级联分类器中存储,全部要单独
15、指出。函数 cvLoadHaarClassifierCascade 用于从文件中装载训练好的利用海尔特征的级联分类器,或者从 OpenCV 中嵌入的分类器数据库中导入。分类器的训练可以应用函数haartraining(具体观察opencv/apps/haartraining) 这个数值是在训练分类器时就确定好的,修改它并不能转变检测的范围或精度。需要留意的是,这个函数已经过时了。现在的目标检测分类器通常存储在 XML 或 YAML 文件中,而不是通过路径导入。从文件中导入分类器,可以使用函数 cvLoad 。cvReleaseHaarClassifierCascade释放 haar classifier cascade。void cvReleaseHaarClassifierCascade( CvHaarClassifierCascade* cascade ); cascade双指针类型指针指向要释放的 cascade. 指针由函数声明。函数 cvReleaseHaarClassifierCascade 释放cascade 的动态内存,其中cascade 的动态内存或者是手工创立,或者