int CUtil_Sift::CmpSiftFiles( const char* siftfile,vector< pair<int,string> >& vecDstFiles ) { if ( !siftfile || vecDstFiles.size()<1 ) { return -1; } struct feature* feat1, * feat2, * feat; int n1, n2, k, i, m; int iVec; /////////////////////////////////////////////////////////////// struct feature** nbrs; struct kd_node* kd_root; double d0, d1; n2 = import_features((char*)siftfile,FEATURE_LOWE,&feat2); if(n2<1) { return -1; } fprintf(stderr,"there are %d points in siftfile\n",n2); kd_root = kdtree_build( feat2, n2 ); for ( iVec=0; iVec<(int)vecDstFiles.size(); iVec++ ) { vecDstFiles[iVec].first=0; n1 = import_features((char*)(vecDstFiles[iVec].second.c_str()),FEATURE_LOWE,&feat1); if ( n1<1 ) { continue; } m=0; 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 ) { m++; feat1[i].fwd_match = nbrs[0]; } } free( nbrs ); } fprintf(stderr,"get %d same points\n",m); vecDstFiles[iVec].first = m; free( feat1 ); } kdtree_release( kd_root ); free( feat2 ); sort(vecDstFiles.begin(),vecDstFiles.end(),cmp); return 0; }
int CUtil_Sift::CmpSiftFilesF( const char* siftfile1,const char* siftfile2 ) { if ( !siftfile1 || !siftfile2 ) { return -1; } struct feature* feat1, * feat2, * feat; int n1, n2, k, i, m; /////////////////////////////////////////////////////////////// struct feature** nbrs; struct kd_node* kd_root; double d0, d1; n1 = import_features((char*)siftfile1,FEATURE_LOWE,&feat1); if(n1<1) { return -1; } kd_root = kdtree_build( feat1, n1 ); { n2 = import_features((char*)siftfile2,FEATURE_LOWE,&feat2); if ( n2<1 ) { return -1; } m=0; for( i = 0; i < n2; i++ ) { feat = feat2 + 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 ) { m++; feat2[i].fwd_match = nbrs[0]; } } free( nbrs ); } free( feat2 ); } kdtree_release( kd_root ); free( feat1 ); return m; }
int main( int argc, char** argv ) { 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; if( argc != 3 ) fatal_error( "usage: %s <feat_file> <feat_file>", argv[0] ); n1 = import_features( argv[1], feat_type, &feat1 ); if( n1 == -1 ) fatal_error( "unable to import features from %s", argv[1] ); n2 = import_features( argv[2], feat_type, &feat2 ); if( n2 == -1 ) fatal_error( "unable to import features from %s", argv[2] ); 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.y += 20; m++; feat1[i].fwd_match = nbrs[0]; } } free( nbrs ); } printf("%d\n", m ); kdtree_release( kd_root ); free( feat1 ); free( feat2 ); return 0; }
// Feature Thread void* featureThread(void* featureData) { char file[25]; int index, numFeatures; struct feature* feat; struct fData* temp; temp = (struct fData*) featureData; IplImage* img = temp->img; index = temp->index; sprintf(file, "features/temp%d", index); struct feature* feat1; if (import_features(file, FEATURE_LOWE, &feat1) == -1) { numFeatures = sift_features(img, &feat); export_features(file, feat, numFeatures); printf("features for image %d:\n", index); // print all features if (DEBUG) { printFeature(feat, numFeatures, 100); printf("\n\n"); } } }
int main( int argc, char** argv ) { IplImage* img; struct feature* feat; char* name; int n; char feat_image_name[200]; if (argc==3){//command:dspFeat .sift image feat_file=argv[1]; img_file=argv[2]; } img = cvLoadImage( img_file, 1 ); if( ! img ) fatal_error( "unable to load image from %s", img_file ); n = import_features( feat_file, feat_type, &feat ); if( n == -1 ) fatal_error( "unable to import features from %s", feat_file ); name = feat_file; draw_features( img, feat, n ); cvNamedWindow( name, 1 ); cvShowImage( name, img ); sprintf_s(feat_image_name,200,"%s.jpg",feat_file); cvSaveImage(feat_image_name,img,0); cvWaitKey( 0 ); return 0; }
int CUtil_Sift::GetSiftPts( const char* sift1 ) { if ( !sift1 ) { return -1; } struct feature* feat1; int n1; n1 = import_features((char*)sift1,FEATURE_LOWE,&feat1); free( feat1 ); return n1; }
int main( int argc, char** argv ) { IplImage* img; struct feature* feat; char* name; int n; img = cvLoadImage( img_file, 1 ); if( ! img ) fatal_error( "unable to load image from %s", img_file ); n = import_features( feat_file, feat_type, &feat ); if( n == -1 ) fatal_error( "unable to import features from %s", feat_file ); name = feat_file; draw_features( img, feat, n ); cvNamedWindow( name, 1 ); cvShowImage( name, img ); cvWaitKey( 0 ); return 0; }
int main( int argc, char** argv ) { //IplImage* img1, * img2, * stacked; 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; /* if( argc != 3 ) fatal_error( "usage: %s <img1> <img2>", argv[0] ); img1 = cvLoadImage( argv[1], 1 ); if( ! img1 ) fatal_error( "unable to load image from %s", argv[1] ); img2 = cvLoadImage( argv[2], 1 ); if( ! img2 ) fatal_error( "unable to load image from %s", argv[2] ); stacked = stack_imgs( img1, img2 ); */ char *query_img=argv[1]; char* match_img=argv[2]; fprintf( stderr, "Finding features in %s...\n", argv[1] ); //n1 = sift_features( img1, &feat1 ); n1=import_features(match_img, FEATURE_LOWE, &feat1); fprintf( stderr, "Finding features in %s...\n", argv[2] ); //n2 = sift_features( img2, &feat2 ); n2=import_features(query_img, FEATURE_LOWE, &feat2); kd_root = kdtree_build( feat2, n2 ); int cnt1=0, cnt2=0,cnt3=0; for( i = 0; i < n1; i++ ) { feat = feat1 + i; k = kdtree_bbf_knn( kd_root, feat, 2, &nbrs, KDTREE_BBF_MAX_NN_CHKS ); cnt1++; if( k == 2 ) { d0 = descr_dist_sq( feat, nbrs[0] ); d1 = descr_dist_sq( feat, nbrs[1] ); cnt2++; 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.y += img1->height; //cvLine( stacked, pt1, pt2, CV_RGB(255,0,255), 1, 8, 0 ); m++; feat1[i].fwd_match = nbrs[0]; cnt3++; } } free( nbrs ); } fprintf( stderr, "cnt1: %d cnt2: %d cnt3:%d \n", cnt1, cnt2, cnt3 ); fprintf( stderr, "Found %d total matches\n", m ); //display_big_img( stacked, "Matches" ); //cvWaitKey( 0 ); /* UNCOMMENT BELOW TO SEE HOW RANSAC FUNCTION WORKS Note that this line above: feat1[i].fwd_match = nbrs[0]; is important for the RANSAC function to work. */ CvMat* H; //IplImage* xformed; int inliers=0; H = ransac_xform( feat1, n1, FEATURE_FWD_MATCH, lsq_homog, 4, 0.01, homog_xfer_err, 3.0, NULL, &inliers); cvReleaseMat( &H ); fprintf( stderr, "Found %d total inliers\n", inliers); /* if( H ) { xformed = cvCreateImage( cvGetSize( img2 ), IPL_DEPTH_8U, 3 ); cvWarpPerspective( img1, xformed, H, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll( 0 ) ); cvNamedWindow( "Xformed", 1 ); cvShowImage( "Xformed", xformed ); cvWaitKey( 0 ); cvReleaseImage( &xformed ); cvReleaseMat( &H ); } */ //cvReleaseImage( &stacked ); //cvReleaseImage( &img1 ); //cvReleaseImage( &img2 ); kdtree_release( kd_root ); free( feat1 ); free( feat2 ); return 0; }
// Match Thread void* matchThread(void* matchData) { // normal match local variables struct feature* feat= NULL; struct feature** nbrs = NULL; struct kd_node* kd_root = NULL; double d0, d1; int k, i, x, y, m = 0; // arguments passed to thread char file[25]; char file2[25]; struct mData* temp; temp = (struct mData*) matchData; IplImage* img1 = temp->img1; IplImage* img2 = temp->img2; int index1 = temp->index1; int index2 = temp->index2; CvMat* homography = temp->homography; sprintf(file, "features/temp%d", index1); sprintf(file2, "features/temp%d", index2); struct feature* feat1; struct feature* feat2; int numFeatures1 = import_features(file, FEATURE_LOWE, &feat1); int numFeatures2 = import_features(file2, FEATURE_LOWE, &feat2); // printf("running the matching function\n"); // matching function kd_root = kdtree_build( feat2, numFeatures2 ); for( i = 0; i < numFeatures1; 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 ) { m++; feat1[i].fwd_match = nbrs[0]; } } free( nbrs ); } if (DEBUG) printf("computing transform\n"); // Compute homography CvMat* H; H = ransac_xform(feat1, numFeatures1, FEATURE_FWD_MATCH, lsq_homog, 4, 0.01, homog_xfer_err, 3.0, NULL, NULL); if (DEBUG) printf("past ransac_xform\n"); if (!H) { if (DEBUG) printf("setting empty homography...\n"); H = cvCreateMat(3, 3, CV_64F); for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { cvmSet(H, x, y, 1000000.0); } } } kdtree_release(kd_root); if (DEBUG) printf("setting homography\n"); for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { double tempValue = cvmGet(H, x, y); cvmSet(homography, x, y, tempValue); } } if (DEBUG) printf("past setting homography\n"); free(feat1); free(feat2); pthread_exit(NULL); }
int CUtil_Sift::CmpSiftFilesF_New( const char* siftfile1,const char* siftfile2 ) { if ( !siftfile1 || !siftfile2 ) { return -1; } struct feature* feat1, * feat2, * feat; int n1, n2, k, i, m=0; /////////////////////////////////////////////////////////////// struct feature** nbrs; struct kd_node* kd_root; double d0, d1; ///////////////////////////////////////////////////////////////////// int upimgwid,upimghigh; int downimgwid,downimghigh; int upwid,uphigh; int downwid,downhigh; double tempx,tempy; int tempnwid,tempnhigh; int j; #define nwid 5 #define nhigh 8 int numdown[nwid*nhigh] = {0}; int numup[nwid*nhigh] = {0}; struct feature *upfeat[nwid*nhigh]; struct feature *downfeat[nwid*nhigh]; n1 = import_features((char*)siftfile1,FEATURE_LOWE,&feat1); if(n1<1) { return -1; } n2 = import_features((char*)siftfile2,FEATURE_LOWE,&feat2); if ( n2<1 ) { return -1; } ///////////////////////////////////////////////////////////////// // upimgwid = img1->width; // upimghigh = img1->height; upwid = upimgwid/nwid; uphigh = upimghigh/nhigh; ///////////////////////////////////////////////////////////////// // downimgwid = img2->width; // downimghigh = img2->height; downwid = downimgwid/nwid; downhigh = downimghigh/nhigh; /////////////////////////////////////////////////////////////// //将feature2按行列分块 for(i=0; i<nwid*nhigh; i++) { upfeat[i] = NULL; upfeat[i] = (struct feature *)malloc(300*sizeof(struct feature)); downfeat[i] = NULL; downfeat[i] = (struct feature *)malloc(300*sizeof(struct feature)); } for(i=0; i<n2; i++) { tempx = (*feat2).x; tempy = (*feat2).y; tempnwid = (int)(tempx/downwid); if(tempnwid == nwid) tempnwid = nwid -1; tempnhigh = (int)(tempy/downhigh); if(tempnhigh == nhigh) tempnhigh = nhigh-1; // *(*(downfeat+tempnhigh*nwid+tempnwid)) = feat2; *(downfeat[tempnhigh*nwid+tempnwid]+*(numdown+tempnhigh*nwid+tempnwid)) = *feat2; (*(numdown+tempnhigh*nwid+tempnwid))++; // (downfeat[tempnhigh*nwid+tempnwid]) ++; feat2 ++; } ///////////////////////////////////////////////////////////////// //将feature1按行列分块 for(i=0; i<n1; i++) { tempx = (*feat1).x; tempy = (*feat1).y; tempnwid = (int)(tempx/upwid); if(tempnwid == nwid) tempnwid = nwid -1; tempnhigh = (int)(tempy/uphigh); if(tempnhigh == nhigh) tempnhigh = nhigh-1; *(upfeat[tempnhigh*nwid+tempnwid]+*(numup+tempnhigh*nwid+tempnwid)) = *feat1; (*(numup+tempnhigh*nwid+tempnwid))++; // (*(upfeat+tempnhigh*nwid+tempnwid))++; feat1 ++; } //按块寻找匹配点 for(i=0; i<nwid*nhigh; i++) { if(numdown[i] == 0) continue; kd_root = kdtree_build( downfeat[i], numdown[i]); for(j=0; j<*(numup+i); j++) { if(numup[i] == 0) continue; feat = upfeat[i]+j; 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 ) { m++; feat->fwd_match = nbrs[0]; } } free(nbrs); } kdtree_release( kd_root ); free(upfeat[i]); upfeat[i] = NULL; free(downfeat[i]); downfeat[i] = NULL; } return m; }
int main( int argc, char** argv ) { struct feature* feat1, * feat2, * feat; struct feature** nbrs; struct kd_node* kd_root; double d0, d1; int n1, n2, k, i, m = 0; if( argc != 3 ) fatal_error( "usage: %s <keydescr1> <keydescr2>", argv[0] ); n1 = import_features( argv[1], feat_type, &feat1 ); n2 = import_features( argv[2], feat_type, &feat2 ); if( n1 < 0 ) fatal_error( "unable to load key descriptors from %s", argv[1] ); if( n2 < 0 ) fatal_error( "unable to load key descriptors from %s", argv[2] ); printf("%d features presenti nel modello.\n", n1); printf("%d features presenti nell'immagine.\n", n2); 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 ) { // Controlla d0 = descr_dist_sq( feat, nbrs[0] ); d1 = descr_dist_sq( feat, nbrs[1] ); if( d0 < d1 * NN_SQ_DIST_RATIO_THR ) { m++; feat1[i].fwd_match = nbrs[0]; } } free( nbrs ); } #ifdef RANSAC { CvMat* H; IplImage* xformed; struct feature ** inliers; int n_inliers; H = ransac_xform( feat1, n1, FEATURE_FWD_MATCH, lsq_homog, 4, 0.01, homog_xfer_err, 8.0, &inliers, &n_inliers ); if( H ) { if ( cvmGet( H, 1, 2 ) < 120.0 && cvmGet( H, 0, 2 ) < 120.0 && ( fabs(cvmGet( H, 0, 0)) >= 0.000001 || fabs(cvmGet( H, 0, 1)) >= 0.000001 || fabs(cvmGet( H, 1, 0)) >= 0.000001 || fabs(cvmGet( H, 1, 1)) >= 0.000001 || fabs(cvmGet( H, 2, 0)) >= 0.000001 || fabs(cvmGet( H, 2, 1)) >= 0.000001 ) ) { // TROVATO printf("RANSAC OK\n"); /* xformed = cvCreateImage( cvGetSize( img2 ), IPL_DEPTH_8U, 3 ); cvWarpPerspective( img1, xformed, H, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll( 0 ) ); cvNamedWindow( "Xformed", 1 ); cvShowImage( "Xformed", xformed ); cvWaitKey( 0 ); cvReleaseImage( &xformed ); */ printf("N. inliers: %d\n", n_inliers); } else { // Trovato ma probabilmente la matrice di trasformazione e' sbagliata. printf("RANSAC FAIL\n"); } cvReleaseMat( &H ); } else { printf("RANSAC FAIL\n"); } } #endif fprintf( stderr, "Found %d total matches\n", m ); kdtree_release( kd_root ); free( feat1 ); free( feat2 ); return 0; }