电子商务网站建设教学总结,WordPress 蜘蛛检测,网站源码程序修改,网站免费推广软件带着强烈的兴趣#xff0c;上周开始人脸识别的尝试与学习#xff0c;并且将具体的操作过程记录了下来
链接如下#xff1a;http://blog.csdn.net/u011583927/article/details/44627493
这周开始了对于算法的深入学习#xff0c;下面进入正题。 Haar特征的原理是什么…带着强烈的兴趣上周开始人脸识别的尝试与学习并且将具体的操作过程记录了下来
链接如下http://blog.csdn.net/u011583927/article/details/44627493
这周开始了对于算法的深入学习下面进入正题。 Haar特征的原理是什么 Haar特征分为三类边缘特征、线性特征、中心特征和对角线特征组合成特征模板。特征模板内有白色和黑色两种矩形并定义该模板的特征值为白色矩形像素和减去黑色矩形像素和在opencv实现中为黑色-白色。Haar特征值反映了图像的灰度变化情况。例如脸部的一些特征能由矩形特征简单的描述如眼睛要比脸颊颜色要深鼻梁两侧比鼻梁颜色要深嘴巴比周围颜色要深等。但矩形特征只对一些简单的图形结构如边缘、线段较敏感所以只能描述特定走向水平、垂直、对角的结构。本段文字及下面两幅图引用自http://blog.csdn.net/zouxy09/article/details/7929570
Viola提出的haar特征 Lienhart等牛们提出的Haar-like特征 矩形特征可位于图像任意位置大小也可以任意改变所以矩形特征值是矩形模版类别、矩形位置和矩形大小这三个因素的函数当然对于新提出的有旋转角度的haar特征还要把旋转的因素考虑进去。
所以一个Haar特征的数据结构应该包含以下内容
*haar特征模板类型
*是否有旋转
*矩阵位置及大小 CvIntHaarFeatures是如何构成的
在Opencv中我们用CvTHaarFeature和CvFastHaarFeature作为描述单个特征的数据结构用CvIntHaarFeatures作为一个封装的类型通过这个类型中的两个指针分别是CvTHaarFeature*和CvFastHaarFeature*指针可以间接遍寻到存储的所有的特征。下面来看下它们的具体构造 CvTHaarFeature的数据结构
//CvTHaarFeature由至多三个矩形表示特征位置
typedef struct CvTHaarFeature
{ char desc[CV_HAAR_FEATURE_DESC_MAX]; //描述haar特征模板类型的变量 int tilted; //标识是否有旋转通过desc字符数组开头是否为tilted判断 struct { CvRect r; float weight; } rect[CV_HAAR_FEATURE_MAX]; //三个矩形来描述特征位置
} CvTHaarFeature; 创建一个CvTHaarFeature特征
/*例haarFeature cvHaarFeature(tilted_haar_y2, x, y, dx,2*dy, -1, x, y,dx, dy, 2 );*/
CV_INLINECvTHaarFeature cvHaarFeature(const char* desc, int x0, int y0, int w0,int h0, float wt0, int x1, int y1, int w1,int h1, float wt1, int x2, int y2, int w2,int h2, float wt2 )
{ CvTHaarFeature hf; assert( CV_HAAR_FEATURE_MAX 3 ); assert( strlen( desc ) CV_HAAR_FEATURE_DESC_MAX ); strcpy( (hf.desc[0]), desc ); hf.tilted ( hf.desc[0] t ); hf.rect[0].r.x x0; hf.rect[0].r.y y0; hf.rect[0].r.width w0; hf.rect[0].r.height h0; hf.rect[0].weight wt0; hf.rect[1].r.x x1; hf.rect[1].r.y y1; hf.rect[1].r.width w1; hf.rect[1].r.height h1; hf.rect[1].weight wt1; hf.rect[2].r.x x2; hf.rect[2].r.y y2; hf.rect[2].r.width w2; hf.rect[2].r.height h2; hf.rect[2].weight wt2; return hf;
} CvFastHaarFeature的数据结构
//与CvTHaarFeature类似不同的是通过4个点来描述特征矩形的位置大小信息
typedef struct CvFastHaarFeature
{ int tilted; struct { int p0, p1, p2, p3; float weight; } rect[CV_HAAR_FEATURE_MAX];
} CvFastHaarFeature; CvIntHaarFeatures的数据结构
typedef struct CvIntHaarFeatures
{ CvSize winsize; int count; CvTHaarFeature* feature; CvFastHaarFeature* fastfeature;
} CvIntHaarFeatures; 了解了如何构成我们就来创建icvCreateIntHaarFeatures方法的具体实现 接下来就是最重要的一步如何创建我们想要得到的所有特征信息及CvIntHaarFeatures下面是icvCreateIntHaarFeatures方法的具体实现和详细注释
由于opencv和C都是初学用了很长时间写了大量注释0基础也绝对能看懂希望能对大家有帮助 /*
* icvCreateIntHaarFeatures
*
* Create internal representation of haar features
*
* mode:
* 0 - BASIC Viola提出的原始举行特征
* 1 - CORE All upright 所有垂直的haar特征
* 2 - ALL All features 所有haar特征
*symmetric: 目标图形是否为垂直对称
*/
static
CvIntHaarFeatures* icvCreateIntHaarFeatures( CvSize winsize,
int mode,
int symmetric )
{
CvIntHaarFeatures* features NULL;
CvTHaarFeature haarFeature;
/*内存存储器是一个可用来存储诸如序列,轮廓,图形,子划分等动态增长数据结构的底层结构。它是由一系列以同等大小的内存块构成,呈列表型*/
CvMemStorage* storage NULL;
CvSeq* seq NULL;
CvSeqWriter writer;
int s0 36; /* minimum total area size of basic haar feature */
int s1 12; /* minimum total area size of tilted倾斜的 haar features 2 */
int s2 18; /* minimum total area size of tilted haar features 3 */
int s3 24; /* minimum total area size of tilted haar features 4 */
int x 0;
int y 0;
int dx 0;
int dy 0;
#if 0
float factor 1.0F;
factor ((float) winsize.width) * winsize.height / (24 * 24);
s0 (int) (s0 * factor);
s1 (int) (s1 * factor);
s2 (int) (s2 * factor);
s3 (int) (s3 * factor);
#else
//程序必然走这边为什么这么写
s0 1;
s1 1;
s2 1;
s3 1;
#endif
/* CV_VECTOR_CREATE( vec, CvIntHaarFeature, size, maxsize ) */
storage cvCreateMemStorage();
//功能创建新序列并初始化写入部分
/*我的理解这里其实是定义了writer工具每次写入数据的大小以及写入到哪个内存存储器
在之后调用 CV_WRITE_SEQ_ELEM( haarFeature, writer )时就可以自动将一个haarFeature类型的数据写入内存存储器中*/
cvStartWriteSeq( 0, sizeof( CvSeq ), sizeof( haarFeature ), storage, writer );
/*矩形特征可位于图像任意位置大小也可以任意改变所以矩形特征值是矩形模版类别、矩形位置和矩形大小这三个因素的函数*/
for( x 0; x winsize.width; x )
{
for( y 0; y winsize.height; y )
{
//xy确定了特征矩形的左上角坐标
for( dx 1; dx winsize.width; dx )
{
for( dy 1; dy winsize.height; dy )
{
//dxdy确定了特征矩形的大小
//下面需要按照不同的特征模板类型分别讨论在模板不越界的情况下添
加该特征
// haar_x2 对应上图中的a特征模板黑色为白色为-
if ( (xdx*2 winsize.width) (ydy winsize.height) ) {
if (dx*2*dy s0) continue;
if (!symmetric || (xxdx*2 winsize.width))
{
//目标图像不为垂直对称或目标垂直对称但满足上式条件
//若目标不垂直对称显然要计算当前矩形特征的特征值
//若对称则只计算左半部分全部位于标准样本左半边的矩形特征的特征值
haarFeature cvHaarFeature( haar_x2,
x, y, dx*2, dy, -1,
xdx, y, dx , dy, 2 );
/* CV_VECTOR_PUSH( vec, CvIntHaarFeature, haarFeature, size, maxsize, step ) */
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_y2 对应上图中的b特征模板
if ( (xdx winsize.width) (ydy*2 winsize.height) ) {
if (dx*2*dy s0) continue;
if (!symmetric || (xxdx winsize.width)) {
haarFeature cvHaarFeature( haar_y2,
x, y, dx, dy*2, -1,
x, ydy, dx, dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_x3 对应上图中的c特征模板
if ( (xdx*3 winsize.width) (ydy winsize.height) ) {
if (dx*3*dy s0) continue;
if (!symmetric || (xxdx*3 winsize.width)) {
haarFeature cvHaarFeature( haar_x3,
x, y, dx*3, dy, -1,
xdx, y, dx, dy, 3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_y3 对应上图中的d特征模板
if ( (xdx winsize.width) (ydy*3 winsize.height) ) {
if (dx*3*dy s0) continue;
if (!symmetric || (xxdx winsize.width)) {
haarFeature cvHaarFeature( haar_y3,
x, y, dx, dy*3, -1,
x, ydy, dx, dy, 3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
if( mode ! 0 /*BASIC*/ ) {
// haar_x4 对应上图中的2b特征模板
if ( (xdx*4 winsize.width) (ydy winsize.height) ) {
if (dx*4*dy s0) continue;
if (!symmetric || (xxdx*4 winsize.width)) {
haarFeature cvHaarFeature( haar_x4,
x, y, dx*4, dy, -1,
xdx, y, dx*2, dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// haar_y4 对应上图中的2d特征模板
if ( (xdx winsize.width ) (ydy*4 winsize.height) ) {
if (dx*4*dy s0) continue;
if (!symmetric || (xxdx winsize.width)) {
haarFeature cvHaarFeature( haar_y4,
x, y, dx, dy*4, -1,
x, ydy, dx, dy*2, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
}
// x2_y2 对应上图中的e特征模板
if ( (xdx*2 winsize.width) (ydy*2 winsize.height) ) {
if (dx*4*dy s0) continue;
if (!symmetric || (xxdx*2 winsize.width)) {
haarFeature cvHaarFeature( haar_x2_y2,
x , y, dx*2, dy*2, -1,
x , y , dx , dy, 2,
xdx, ydy, dx , dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
if (mode ! 0 /*BASIC*/) {
// point 对应上图中的3a特征模板
if ( (xdx*3 winsize.width) (ydy*3 winsize.height) ) {
if (dx*9*dy s0) continue;
if (!symmetric || (xxdx*3 winsize.width)) {
haarFeature cvHaarFeature( haar_point,
x , y, dx*3, dy*3, -1,
xdx, ydy, dx , dy , 9);
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
}
if (mode 2 /*ALL*/) {
// tilted haar_x2 (x, y, w, h, b, weight)
//对应上图中的1c特征模板
if ( (x2*dx winsize.width) (y2*dxdy winsize.height) (x-dy 0) ) {
if (dx*2*dy s1) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_x2,
x, y, dx*2, dy, -1,
x, y, dx , dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_y2 (x, y, w, h, b, weight)
//对应上图中的1d特征模板
if ( (xdx winsize.width) (ydx2*dy winsize.height) (x-2*dy 0) ) {
if (dx*2*dy s1) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_y2,
x, y, dx, 2*dy, -1,
x, y, dx, dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_x3 (x, y, w, h, b, weight)
if ( (x3*dx winsize.width) (y3*dxdy winsize.height) (x-dy 0) ) {
if (dx*3*dy s2) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_x3,
x, y, dx*3, dy, -1,
xdx, ydx, dx , dy, 3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_y3 (x, y, w, h, b, weight)
if ( (xdx winsize.width) (ydx3*dy winsize.height) (x-3*dy 0) ) {
if (dx*3*dy s2) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_y3,
x, y, dx, 3*dy, -1,
x-dy, ydy, dx, dy, 3 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_x4 (x, y, w, h, b, weight)
if ( (x4*dx winsize.width) (y4*dxdy winsize.height) (x-dy 0) ) {
if (dx*4*dy s3) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_x4,
x, y, dx*4, dy, -1,
xdx, ydx, dx*2, dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
// tilted haar_y4 (x, y, w, h, b, weight)
if ( (xdx winsize.width) (ydx4*dy winsize.height) (x-4*dy 0) ) {
if (dx*4*dy s3) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_y4,
x, y, dx, 4*dy, -1,
x-dy, ydy, dx, 2*dy, 2 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
/*
// tilted point
if ( (xdx*3 winsize.width - 1) (ydy*3 winsize.height - 1) (x-3*dy 0)) {
if (dx*9*dy 36) continue;
if (!symmetric || (x (winsize.width / 2) )) {
haarFeature cvHaarFeature( tilted_haar_point,
x, y, dx*3, dy*3, -1,
x, ydy, dx , dy, 9 );
CV_WRITE_SEQ_ELEM( haarFeature, writer );
}
}
*/
}
}
}
}
}
/*我的理解当前已经完成了数据的写入但是是存储在内存存储器中的调用此方法将存储器中的所有数据转移到cvSeq中*/
seq cvEndWriteSeq( writer );
在OpenCV中临时缓存用cvAlloc和cvFree函数分配和回收.函数应注意适当对齐,对未释放的内存保持跟踪检查溢出。
features (CvIntHaarFeatures*) cvAlloc( sizeof( CvIntHaarFeatures )
( sizeof( CvTHaarFeature ) sizeof( CvFastHaarFeature ) ) * seq-total );
features-feature (CvTHaarFeature*) (features 1);
features-fastfeature (CvFastHaarFeature*) ( features-feature seq-total );
features-count seq-total;
features-winsize winsize;
cvCvtSeqToArray( seq, (CvArr*) features-feature );
cvReleaseMemStorage( storage );
//特征的rect由坐标表示转换为由像素索引表示
icvConvertToFastHaarFeature( features-feature, features-fastfeature,
features-count, (winsize.width 1) );
return features;
}
由于我是小白感觉上面代码中的最后几行逻辑可能比较绕卡在那里好久终于理解。
所以做了一张图可以借助下图帮助理解 span stylefont-family: Arial, Helvetica, sans-serif;看到这里对于Haar特征的理论以及实际使用应该理解的不错了吧希望能和大家一起交流/span