""" # set paths to data folder featname = ['../data/wanren/uu' + str(i + 1) + '.sift' for i in range(5)] imname = ['../data/wanren/uu' + str(i + 1) + '.jpg' for i in range(5)] # extract features and match l = {} d = {} for i in range(5): sift.process_image(root + imname[i], root + featname[i]) l[i], d[i] = sift.read_features_from_file(featname[i]) matches = {} for i in range(4): matches[i] = sift.match(d[i + 1], d[i]) # visualize the matches (Figure 3-11 in the book) for i in range(4): im1 = array(Image.open(imname[i])) im2 = array(Image.open(imname[i + 1])) figure() sift.plot_matches(im2, im1, l[i + 1], l[i], matches[i], show_below=True) # function to convert the matches to hom. points def convert_points(j): ndx = matches[j].nonzero()[0] fp = homography.make_homog(l[j + 1][ndx, :2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(l[j][ndx2, :2].T)
""" # set paths to data folder featname = ['../data/Univ'+str(i+1)+'.sift' for i in range(5)] imname = ['../data/Univ'+str(i+1)+'.jpg' for i in range(5)] # extract features and match l = {} d = {} for i in range(5): # sift.process_image(imname[i],featname[i]) l[i],d[i] = sift.read_features_from_file(featname[i]) matches = {} for i in range(4): matches[i] = sift.match(d[i+1],d[i]) # visualize the matches (Figure 3-11 in the book) for i in range(4): im1 = array(Image.open(imname[i])) im2 = array(Image.open(imname[i+1])) figure() sift.plot_matches(im2,im1,l[i+1],l[i],matches[i],show_below=True) # function to convert the matches to hom. points def convert_points(j): ndx = matches[j].nonzero()[0] fp = homography.make_homog(l[j+1][ndx,:2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(l[j][ndx2,:2].T)
# 载入查询图像特征 # load image features for query image q_locs,q_descr = sift.read_features_from_file(featlist[q_ind]) fp = homography.make_homog(q_locs[:,:2].T) # RANSAC model for homography fitting model = homography.RansacModel() rank = {} # load image features for result for ndx in res_reg[1:]: locs,descr = sift.read_features_from_file(featlist[ndx-1]) # because 'ndx' is a rowid of the DB that starts at 1. # locs,descr = sift.read_features_from_file(featlist[ndx]) # get matches matches = sift.match(q_descr,descr) ind = matches.nonzero()[0] ind2 = matches[ind] tp = homography.make_homog(locs[:,:2].T) # compute homography, count inliers. if not enough matches return empty list try: H,inliers = homography.H_from_ransac(fp[:,ind],tp[:,ind2],model,match_theshold=4) except: inliers = [] # store inlier count rank[ndx] = len(inliers) # sort dictionary to get the most inliers first sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res_reg[0]]+[s[0] for s in sorted_rank] print 'top matches (homography):', res_geom
q_ind = 0 nbr_results = 20 res_reg = [w[1] for w in src.query(imlist[q_ind])[:nbr_results]] print 'top matches (regular) : ', res_reg q_locs, q_descr = sift.read_features_from_file(featlist[q_ind]) fp = homography.make_homog(q_locs[:, :2].T) model = homography.RansacModel() rank = {} for ndx in res_reg[1:]: locs, descr = sift.read_features_from_file(featlist[ndx]) matches = sift.match(q_descr, descr) ind = matches.nonzero()[0] ind2 = matches[ind] tp = homography.make_homog(locs[:, :2].T) try: H, inliners = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_theshold=4) except: inliners = [] rank[ndx] = len(inliners) sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res_reg[0]] + [s[0] for s in sorted_rank] # 显示查询结果
im1f, im2f = sys.argv[1], sys.argv[2] else: im1f = r'e:\Study\pythonxyProject\sift\images/02.jpg' im2f = r'e:\Study\pythonxyProject\sift\images/04.jpg' # im1f = '../data/crans_1_small.jpg' # im2f = '../data/crans_2_small.jpg' # im1f = '../data/climbing_1_small.jpg' # im2f = '../data/climbing_2_small.jpg' im1 = array(Image.open(im1f)) im2 = array(Image.open(im2f)) sift.process_image(im1f, 'out_sift_1.txt') l1, d1 = sift.read_features_from_file('out_sift_1.txt') figure() gray() subplot(121) sift.plot_features(im1, l1, circle=False) sift.process_image(im2f, 'out_sift_2.txt') l2, d2 = sift.read_features_from_file('out_sift_2.txt') subplot(122) sift.plot_features(im2, l2, circle=False) matches = sift.match(d1, d2) matches = sift.match_twosided(d1, d2) print '{} matches'.format(len(matches.nonzero()[0])) figure() gray() sift.plot_matches(im1, im2, l1, l2, matches, show_below=True) show()