close
SIFT( Scale-invariant feature transform )
Source : http://blogs.oregonstate.edu/hess/
Reference : http://en.wikipedia.org/wiki/Scale-invariant_feature_transform
Include header and define marco :
#include "sift.h" #include "imgfeatures.h" #include "kdtree.h" #include "utils.h" #include "xform.h" /* the maximum number of keypoint NN candidates to check during BBF search */ #define KDTREE_BBF_MAX_NN_CHKS 200 /* threshold on squared ratio of distances between NN and 2nd NN */ #define NN_SQ_DIST_RATIO_THR 0.49
Main code :
iplSrc1 : input image1
iplSrc2 : input image2
iplRst : output image
struct feature* feat1, * feat2, * feat; struct feature** nbrs; struct kd_node* kd_root; //--------------------------------------------------------- CvPoint pt1, pt2; double d0, d1; int n1, n2, k, i, m = 0; //--------------------------------------------------------- iplRst = stack_imgs( iplSrc1 , iplSrc2 ); //--------------------------------------------------------- n1 = sift_features( iplSrc1 , & feat1 ); n2 = sift_features( iplSrc2 , & feat2 ); kd_root = kdtree_build( feat2 , n2 ); //--------------------------------------------------------- for(i=0;i<n1;i++) { feat = feat1 + i; k = kdtree_bbf_knn( kd_root , feat , 2 , & nbrs , KDTREE_BBF_MAX_NN_CHKS ); if( k==2 ) { d0 = descr_dist_sq( feat , nbrs[0] ); d1 = descr_dist_sq( feat , nbrs[1] ); if( d0 < d1 * NN_SQ_DIST_RATIO_THR ) { pt1 = cvPoint( cvRound( feat->x ), cvRound( feat->y ) ); pt2 = cvPoint( cvRound( nbrs[0]->x ), cvRound( nbrs[0]->y ) ); pt2.x += iplSrc1->width; cvLine( iplRst, pt1, pt2, CV_RGB(255,0,0), 1, 8, 0 ); cvCircle( iplRst, pt1, 4, CV_RGB(0,255,0), 1, 8, 0 ); cvCircle( iplRst, pt2, 4, CV_RGB(255,255,0), 1, 8, 0 ); m++; feat1[i].fwd_match = nbrs[0]; } } free( nbrs ); } //--------------------------------------------------------- kdtree_release( kd_root ); free( feat1 ); free( feat2 );
Output :
文章標籤
全站熱搜