/// SIFT matches static void SIFT(const Image<unsigned char> &im1, const Image<unsigned char> &im2, std::vector<Match>& vec_matchings, float fMatchRatio=0.6f) { //Convert images to float Image<float> If1, If2; libs::convertImage(im1, &If1); libs::convertImage(im2, &If2); siftPar param; default_sift_parameters(param); param.MatchRatio = fMatchRatio; param.DoubleImSize=0; keypointslist keyp1, keyp2; compute_sift_keypoints(If1.data(), keyp1, If1.Width(), If1.Height(), param); std::cout<< "sift:: 1st image: " << keyp1.size() << " keypoints"<<std::endl; compute_sift_keypoints(If2.data(), keyp2, If2.Width(), If2.Height(), param); std::cout<< "sift:: 2nd image: " << keyp2.size() << " keypoints"<<std::endl; // Find putatives matches compute_sift_matches(keyp1, keyp2, vec_matchings, param); std::cout << "sift:: matches: " << vec_matchings.size() <<std::endl; }
/// Usage: siftMatch imgIn imgIn2 fileOut [imgOut] /// Output in text file fileOut the matches evaluated by SIFT method between /// the images imgIn and imgIn2 (PNG format). If imgOut is in the argument list, /// this is an image file where matches will be shown (PNG format). int main(int argc, char **argv) { if(argc != 4 && argc != 5) { std::cerr << "Usage: " << argv[0] << " imgIn imgIn2 fileOut [imgOut]" << std::endl; return 1; } //////////////////////////////////////////////// Input int w1, h1; float* ipixels1; if(! load(argv[1], ipixels1, w1, h1)) return 1; //////////////////////////////////////////////// Input int w2, h2; float* ipixels2; if(! load(argv[2], ipixels2, w2, h2)) return 1; ///////////////////////////////////////// Applying Sift siftPar siftparameters; default_sift_parameters(siftparameters); siftparameters.DoubleImSize=0; keypointslist keyp1, keyp2; compute_sift_keypoints(ipixels1,keyp1,w1,h1,siftparameters); std::cout<< "siftMatch:: 1st image: " << keyp1.size() << " keypoints"<<std::endl; compute_sift_keypoints(ipixels2,keyp2,w2,h2,siftparameters); std::cout<< "siftMatch:: 2nd image: " << keyp2.size() << " keypoints"<<std::endl; matchingslist matchings; compute_sift_matches(keyp1,keyp2,matchings,siftparameters); std::cout << "siftMatch:: matches: " << matchings.size() <<std::endl; //////////////////////////////////////////////////////////////// Save file with matches saveMatch(argv[3], matchings); //////////////////////////////////////////////// Output image containing line matches if(argc > 4) { int wo = std::max(w1,w2); int ho = h1+h2; float *opixels = new float[wo*ho]; for(int j = 0; j < h1; j++) for(int i = 0; i < w1; i++) opixels[j*wo+i] = ipixels1[j*w1+i]; for(int j = 0; j < h2; j++) for(int i = 0; i < w2; i++) opixels[(h1 + j)*wo + i] = ipixels2[j*w2 + i]; //////////////////////////////////////////////////////////////////// Draw matches matchingslist::iterator ptr = matchings.begin(); for(; ptr != matchings.end(); ++ptr) draw_line(opixels, (int) ptr->x1, (int) ptr->y1, (int) ptr->x2, (int) ptr->y2 + h1, 255.0f, wo, ho); ///////////////////////////////////////////////////////////////// Save imgOut io_png_write_f32(argv[4], opixels, (size_t)wo, (size_t)ho, 1); delete[] opixels; } /////////////////////////////////////////////////////////////// Delete memory free(ipixels1); free(ipixels2); return 0; }
int compute_asift_matches(int num_of_tilts1, int num_of_tilts2, int w1, int h1, int w2, int h2, int verb, vector< vector< keypointslist > >& keys1, vector< vector< keypointslist > >& keys2, matchingslist &matchings, siftPar &siftparameters) // Match the ASIFT keypoints. // Input: // num_of_tilts1, num_of_tilts2: number of tilts that have been simulated on the two images. (They can be different.) // w1, h1, w2, h2: widht/height of image1/image2. // verb: 1/0 --> show/don not show verbose messages. (1 for debugging) // keys1, keys2: ASIFT keypoints of image1/image2. (They should be calculated with compute_asift_keypoints.) // matchings (output): the coordinates (col1, row1, col2, row2) of all the matching points. // // Output: the number of matching points. { float t_min, t_k, t; int num_tilt1, num_tilt2, tt, num_rot_t2, num_rot1, rr; int cc; int tt2, rr2, num_rot1_2; float t_im2; /* It stores the coordinates of ALL matches points of ALL affine simulations */ vector< vector <float> > Minfoall; int Tmin = 8; float nfa_max = -2; num_rot_t2 = 10; t_min = 1; t_k = sqrt(2.); num_tilt1 = num_of_tilts1; num_tilt2 = num_of_tilts2; if ( ( num_tilt1 < 1 ) || ( num_tilt2 < 1 ) ) { printf("Number of tilts num_tilt should be equal or larger than 1. \n"); exit(-1); } /* Initialize the vector structure for the matching points */ std::vector< vector< vector < vector < matchingslist > > > > matchings_vec(num_tilt1); std::vector< vector< vector< vector< vector< vector <float> > > > > > Minfoall_vec(num_tilt1); for (tt = 1; tt <= num_tilt1; tt++) { t = t_min * pow(t_k, tt-1); if ( t == 1 ) { num_rot1 = 1; } else { num_rot1 = round(num_rot_t2*t/2); if ( num_rot1%2 == 1 ) { num_rot1 = num_rot1 + 1; } num_rot1 = num_rot1 / 2; } matchings_vec[tt-1].resize(num_rot1); Minfoall_vec[tt-1].resize(num_rot1); for ( rr = 1; rr <= num_rot1; rr++ ) { matchings_vec[tt-1][rr-1].resize(num_tilt2); Minfoall_vec[tt-1][rr-1].resize(num_tilt2); for (tt2 = 1; tt2 <= num_tilt2; tt2++) { t_im2 = t_min * pow(t_k, tt2-1); if ( t_im2 == 1 ) { num_rot1_2 = 1; } else { num_rot1_2 = round(num_rot_t2*t_im2/2); if ( num_rot1_2%2 == 1 ) { num_rot1_2 = num_rot1_2 + 1; } num_rot1_2 = num_rot1_2 / 2; } matchings_vec[tt-1][rr-1][tt2-1].resize(num_rot1_2); Minfoall_vec[tt-1][rr-1][tt2-1].resize(num_rot1_2); } } } ///* // * setup the tilt and rotation parameters // * for all the loops, this vector will hold // * the following parameters: // * tt, num_rot1, rr, tt2, num_rot1_2, rr2 // */ //vector<int> tilt_rot; ///* loop on tilts for image 1 */ //for (int tt = 1; tt <= num_tilt1; tt++) //{ // float t = t_min * pow(t_k, tt-1); // int num_rot1; // /* if tilt t = 1, do not simulate rotation. */ // if ( 1 == tt ) // num_rot1 = 1; // else // { // /* number of rotations to simulate */ // num_rot1 = round(num_rot_t2 * t / 2); // if ( num_rot1%2 == 1 ) // num_rot1 = num_rot1 + 1; // num_rot1 = num_rot1 / 2; // } // /* loop on rotations for image 1 */ // for (int rr = 1; rr <= num_rot1; rr++ ) // { // /* loop on tilts for image 2 */ // for (int tt2 = 1; tt2 <= num_tilt2; tt2++) // { // float t_im2 = t_min * pow(t_k, tt2-1); // int num_rot1_2; // if ( tt2 == 1 ) // num_rot1_2 = 1; // else // { // num_rot1_2 = round(num_rot_t2 * t_im2 / 2); // if ( num_rot1_2%2 == 1 ) // num_rot1_2 = num_rot1_2 + 1; // num_rot1_2 = num_rot1_2 / 2; // } // /* loop on rotations for image 2 */ // for (int rr2 = 1; rr2 <= num_rot1_2; rr2++ ) // { // tilt_rot.push_back(tt); // tilt_rot.push_back(num_rot1); // tilt_rot.push_back(rr); // tilt_rot.push_back(tt2); // tilt_rot.push_back(num_rot1_2); // tilt_rot.push_back(rr2); // } // } // } //} /* Calculate the number of simulations */ #ifdef _OPENMP omp_set_nested(1); #endif // loop on tilts for image 1. #pragma omp parallel for private(tt) for (int tt = 1; tt <= num_tilt1; tt++) { float t = t_min * pow(t_k, tt-1); /* Attention: the t1, t2 do not follow the same convention as in compute_asift_keypoints */ float t1 = t; float t2 = 1; int num_rot1; // If tilt t = 1, do not simulate rotation. if ( tt == 1 ) { num_rot1 = 1; } else { // The number of rotations to simulate under the current tilt. num_rot1 = round(num_rot_t2*t/2); if ( num_rot1%2 == 1 ) { num_rot1 = num_rot1 + 1; } num_rot1 = num_rot1 / 2; } float delta_theta = PI/num_rot1; // Loop on rotations for image 1. #pragma omp parallel for private(rr) for ( int rr = 1; rr <= num_rot1; rr++ ) { float theta = delta_theta * (rr-1); theta = theta * 180 / PI; /* Read the keypoints of image 1 */ keypointslist keypoints1 = keys1[tt-1][rr-1]; // loop on tilts for image 2. #pragma omp parallel for private(tt2) for (int tt2 = 1; tt2 <= num_tilt2; tt2++) { float t_im2 = t_min * pow(t_k, tt2-1); /* Attention: the t1, t2 do not follow the same convention as in asift_v1.c */ float t_im2_1 = t_im2; float t_im2_2 = 1; int num_rot1_2; if ( tt2 == 1 ) { num_rot1_2 = 1; } else { num_rot1_2 = round(num_rot_t2*t_im2/2); if ( num_rot1_2%2 == 1 ) { num_rot1_2 = num_rot1_2 + 1; } num_rot1_2 = num_rot1_2 / 2; } float delta_theta2 = PI/num_rot1_2; #pragma omp parallel for private(rr2) // Loop on rotations for image 2. for ( int rr2 = 1; rr2 <= num_rot1_2; rr2++ ) { float theta2 = delta_theta2 * (rr2-1); theta2 = theta2 * 180 / PI; /* Read the keypoints of image2. */ keypointslist keypoints2 = keys2[tt2-1][rr2-1]; // Match the keypoints of image1 and image2. matchingslist matchings1; compute_sift_matches(keypoints1,keypoints2,matchings1,siftparameters); if ( verb ) { printf("t1=%.2f, theta1=%.2f, num keys1 = %d, t2=%.2f, theta2=%.2f, num keys2 = %d, num matches=%d\n", t, theta, (int) keypoints1.size(), t_im2, theta2, (int) keypoints2.size(), (int) matchings1.size()); } /* Store the matches */ if ( matchings1.size() > 0 ) { matchings_vec[tt-1][rr-1][tt2-1][rr2-1] = matchingslist(matchings1.size()); Minfoall_vec[tt-1][rr-1][tt2-1][rr2-1].resize(matchings1.size()); for ( int cc = 0; cc < (int) matchings1.size(); cc++ ) { ///// In the coordinates the affine transformations have been normalized already in compute_asift_keypoints. So no need to normalize here. // Normalize the coordinates of the matched points by compensating the simulate affine transformations // compensate_affine_coor(matchings1[cc], w1, h1, w2, h2, t1, t2, theta, t_im2_1, t_im2_2, theta2); matchings_vec[tt-1][rr-1][tt2-1][rr2-1][cc] = matchings1[cc]; vector<float> Minfo_1match(6); Minfo_1match[0] = t1; Minfo_1match[1] = t2; Minfo_1match[2] = theta; Minfo_1match[3] = t_im2_1; Minfo_1match[4] = t_im2_2; Minfo_1match[5] = theta2; Minfoall_vec[tt-1][rr-1][tt2-1][rr2-1][cc] = Minfo_1match; } } } } } } // Move the matches to a 1D vector for (tt = 1; tt <= num_tilt1; tt++) { t = t_min * pow(t_k, tt-1); if ( t == 1 ) { num_rot1 = 1; } else { num_rot1 = round(num_rot_t2*t/2); if ( num_rot1%2 == 1 ) { num_rot1 = num_rot1 + 1; } num_rot1 = num_rot1 / 2; } for ( rr = 1; rr <= num_rot1; rr++ ) { for (tt2 = 1; tt2 <= num_tilt2; tt2++) { t_im2 = t_min * pow(t_k, tt2-1); if ( t_im2 == 1 ) { num_rot1_2 = 1; } else { num_rot1_2 = round(num_rot_t2*t_im2/2); if ( num_rot1_2%2 == 1 ) { num_rot1_2 = num_rot1_2 + 1; } num_rot1_2 = num_rot1_2 / 2; } for ( rr2 = 1; rr2 <= num_rot1_2; rr2++ ) { for ( cc=0; cc < (int) matchings_vec[tt-1][rr-1][tt2-1][rr2-1].size(); cc++ ) { matchings.push_back(matchings_vec[tt-1][rr-1][tt2-1][rr2-1][cc]); Minfoall.push_back(Minfoall_vec[tt-1][rr-1][tt2-1][rr2-1][cc]); } } } } } if ( verb ) { printf("The number of matches is %d \n", (int) matchings.size()); } if ( matchings.size() > 0 ) { /* Remove the repetitive matches that appear in different simulations and retain only one. */ // Since tilts are simuated on both image 1 and image 2, it is normal to have repetitive matches. matchingslist matchings_unique; vector< vector<float> > Minfoall_unique; unique_match1(matchings, matchings_unique, Minfoall, Minfoall_unique); matchings = matchings_unique; Minfoall = Minfoall_unique; if ( verb ) { printf("The number of unique matches is %d \n", (int) matchings.size()); } // There often appear to be some one-to-multiple/multiple-to-one matches (one point in image 1 matches with many points in image 2/vice versa). // This is an artifact of SIFT on interpolated images, as the interpolation tends to create some auto-similar structures (steps for example). // These matches need to be removed. /* Separating the removal of multiple-to-one and one-to-multiple in two steps: - first remove multiple-to-one - then remove one-to-multiple This allows to avoid removing some good matches: multiple-to-one matches is much more frequent than one-to-multiple. Sometimes some of the feature points in image 1 that take part in "multiple-to-one" bad matches have also correct matches in image 2. The modified scheme avoid removing these good matches. */ // Remove to multiple-to-one matches matchings_unique.clear(); Minfoall_unique.clear(); clean_match2(matchings, matchings_unique, Minfoall, Minfoall_unique); matchings = matchings_unique; Minfoall = Minfoall_unique; // Remove to one-to-multiple matches matchings_unique.clear(); Minfoall_unique.clear(); clean_match1(matchings, matchings_unique, Minfoall, Minfoall_unique); matchings = matchings_unique; Minfoall = Minfoall_unique; if ( verb ) { printf("The number of final matches is %d \n", (int) matchings.size()); } // If enough matches to do epipolar filtering if ( (int) matchings.size() >= Tmin ) { //////// Use ORSA to filter out the incorrect matches. // store the coordinates of the matching points vector<Match> match_coor; for ( cc = 0; cc < (int) matchings.size(); cc++ ) { Match match1_coor; match1_coor.x1 = matchings[cc].first.x; match1_coor.y1 = matchings[cc].first.y; match1_coor.x2 = matchings[cc].second.x; match1_coor.y2 = matchings[cc].second.y; match_coor.push_back(match1_coor); } std::vector<float> index; // Guoshen Yu, 2010.09.23 // index.clear(); int t_value_orsa=10000; int verb_value_orsa=0; int n_flag_value_orsa=0; int mode_value_orsa=2; int stop_value_orsa=0; // epipolar filtering with the Moisan-Stival ORSA algorithm. // float nfa = orsa(w1, h1, match_coor, index, t_value_orsa, verb_value_orsa, n_flag_value_orsa, mode_value_orsa, stop_value_orsa); float nfa = orsa((w1+w2)/2, (h1+h2)/2, match_coor, index, t_value_orsa, verb_value_orsa, n_flag_value_orsa, mode_value_orsa, stop_value_orsa); // if the matching is significant, register the good matches if ( nfa < nfa_max ) { // extract meaningful matches matchings_unique.clear(); Minfoall_unique.clear(); for ( cc = 0; cc < (int) index.size(); cc++ ) { matchings_unique.push_back(matchings[(int)index[cc]]); Minfoall_unique.push_back(Minfoall[(int)index[cc]]); } matchings = matchings_unique; Minfoall = Minfoall_unique; cout << "The two images match! " << matchings.size() << " matchings are identified. log(nfa)=" << nfa << "." << endl; } else { matchings.clear(); Minfoall.clear(); cout << "The two images do not match. The matching is not significant: log(nfa)=" << nfa << "." << endl; } } else { matchings.clear(); Minfoall.clear(); cout << "The two images do not match. Not enough matches to do epipolar filtering." << endl; } } else { cout << "The two images do not match.\n" << endl; } return matchings.size(); }