KD树的应用(1)SIFT+KD_BBF搜索算法
3.1、SIFT特征匹配算法
之前本blog内阐述过图像特征匹配SIFT算法,写过五篇文章,这五篇文章分别为:
-
九、图像特征提取与匹配之SIFT算法 (sift算法系列五篇文章)
-
九(续)、sift算法的编译与实现
-
九(再续)、教你一步一步用c语言实现sift算法、上
-
九(再续)、教你一步一步用c语言实现sift算法、下
-
九(三续):SIFT算法的应用--目标识别之Bag-of-words模型
不熟悉SIFT算法相关概念的可以看上述几篇文章,这里不再做赘述。与此同时,本文此部分也作为十五个经典算法研究系列里SIFT算法的九之四续。
OK,我们知道,在sift算法中,给定两幅图片图片,若要做特征匹配,一般会先提取出图片中的下列相关属性作为特征点:
-
-
-
-
-
-
-
struct feature
-
{
-
double x;
-
double y;
-
double a;
-
double b;
-
double c;
-
double scl;
-
double ori;
-
int d;
-
double descr[FEATURE_MAX_D];
-
int type;
-
int category;
-
struct feature* fwd_match;
-
struct feature* bck_match;
-
struct feature* mdl_match;
-
CvPoint2D64f img_pt;
-
CvPoint2D64f mdl_pt;
-
void* feature_data;
-
char dense;
-
};
而后在sift.h文件中定义两个关键函数,这里,我们把它们称之为函数一,和函数二,如下所示:
函数一的声明:
-
extern int sift_features( IplImage* img, struct feature** feat );
函数一的实现:
-
int sift_features( IplImage* img, struct feature** feat )
-
{
-
return _sift_features( img, feat, SIFT_INTVLS, SIFT_SIGMA, SIFT_CONTR_THR,
-
SIFT_CURV_THR, SIFT_IMG_DBL, SIFT_DESCR_WIDTH,
-
SIFT_DESCR_HIST_BINS );
-
}
从上述函数一的实现中,我们可以看到,它内部实际上调用的是这个函数:_sift_features(..),也就是下面马上要分析的函数二。
函数二的声明:
-
extern int _sift_features( IplImage* img, struct feature** feat, int intvls,
-
double sigma, double contr_thr, int curv_thr,
-
int img_dbl, int descr_width, int descr_hist_bins );
函数二的实现:
-
int _sift_features( IplImage* img, struct feature** feat, int intvls,
-
double sigma, double contr_thr, int curv_thr,
-
int img_dbl, int descr_width, int descr_hist_bins )
-
{
-
IplImage* init_img;
-
IplImage*** gauss_pyr, *** dog_pyr;
-
CvMemStorage* storage;
-
CvSeq* features;
-
int octvs, i, n = 0,n0 = 0,n1 = 0,n2 = 0,n3 = 0,n4 = 0;
-
int start;
-
-
-
if( ! img )
-
fatal_error( "NULL pointer error, %s, line %d", __FILE__, __LINE__ );
-
-
if( ! feat )
-
fatal_error( "NULL pointer error, %s, line %d", __FILE__, __LINE__ );
-
-
-
start=GetTickCount();
-
init_img = create_init_img( img, img_dbl, sigma );
-
octvs = log( (float)(MIN( init_img->width, init_img->height )) ) / log((float)(2.0)) -5;
-
gauss_pyr = build_gauss_pyr( init_img, octvs, intvls, sigma );
-
dog_pyr = build_dog_pyr( gauss_pyr, octvs, intvls );
-
fprintf( stderr, " creat the pyramid use %d\n",GetTickCount()-start);
-
-
storage = cvCreateMemStorage( 0 );
-
start=GetTickCount();
-
features = scale_space_extrema( dog_pyr, octvs, intvls, contr_thr,
-
curv_thr, storage );
-
fprintf( stderr, " find the extrum points in DOG use %d\n",GetTickCount()-start);
-
-
calc_feature_scales( features, sigma, intvls );
-
-
if( img_dbl )
-
adjust_for_img_dbl( features );
-
start=GetTickCount();
-
calc_feature_oris( features, gauss_pyr );
-
fprintf( stderr, " get the main oritation use %d\n",GetTickCount()-start);
-
-
start=GetTickCount();
-
compute_descriptors( features, gauss_pyr, descr_width, descr_hist_bins );
-
fprintf( stderr, " compute the descriptors use %d\n",GetTickCount()-start);
-
-
-
-
-
n = features->total;
-
*feat = (feature*)(calloc( n, sizeof(struct feature) ));
-
*feat = (feature*)(cvCvtSeqToArray( features, *feat, CV_WHOLE_SEQ ));
-
-
for( i = 0; i < n; i++ )
-
{
-
free( (*feat)[i].feature_data );
-
(*feat)[i].feature_data = NULL;
-
if((*feat)[i].dense == 4) ++n4;
-
else if((*feat)[i].dense == 3) ++n3;
-
else if((*feat)[i].dense == 2) ++n2;
-
else if((*feat)[i].dense == 1) ++n1;
-
else ++n0;
-
}
-
-
-
-
fprintf( stderr, "In the total feature points the extent4 points is %d\n",n4);
-
fprintf( stderr, "In the total feature points the extent3 points is %d\n",n3);
-
fprintf( stderr, "In the total feature points the extent2 points is %d\n",n2);
-
fprintf( stderr, "In the total feature points the extent1 points is %d\n",n1);
-
fprintf( stderr, "In the total feature points the extent0 points is %d\n",n0);
-
cvReleaseMemStorage( &storage );
-
cvReleaseImage( &init_img );
-
release_pyr( &gauss_pyr, octvs, intvls + 3 );
-
release_pyr( &dog_pyr, octvs, intvls + 2 );
-
-
return n;
-
}
说明:上面的函数二,包含了SIFT算法中几乎所有函数,是SIFT算法的核心。本文不打算一一分析上面所有函数,只会抽取其中涉及到BBF查询机制相关的函数。
CDA数据分析师考试相关入口一览(建议收藏):
▷ 想报名CDA认证考试,点击>>>
“CDA报名”
了解CDA考试详情;
▷ 想加入CDA考试题库,点击>>> “CDA题库” 了解CDA考试详情;
▷ 想学习CDA考试教材,点击>>> “CDA教材” 了解CDA考试详情;
▷ 想查询CDA考试成绩,点击>>> “CDA成绩” 了解CDA考试详情;
▷ 想了解CDA考试含金量,点击>>> “CDA含金量” 了解CDA考试详情;
▷ 想获取CDA考试时间/费用/条件/大纲/通过率,点击 >>>“CDA考试官网” 了解CDA考试详情;