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考試詳情;