import pickle from PCV.imagesearch import imagesearch from PCV.localdescriptors import sift from sqlite3 import dbapi2 as sqlite from PCV.tools.imtools import get_imlist # 获取图像列表 imlist = get_imlist('./first500/') nbr_images = len(imlist) # 获取特征列表 featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)] # 载入词汇 f = open('./first500/vocabulary.pkl', 'rb') voc = pickle.load(f) f.close() src = imagesearch.Searcher('testImaAdd.db', voc) locs, descr = sift.read_features_from_file(featlist[0]) iw = voc.project(descr) print('ask using a histogram...') print(src.candidates_from_histogram(iw)[:10]) src = imagesearch.Searcher('testImaAdd.db', voc) print('try a query...') nbr_results = 12 res = [w[1] for w in src.query(imlist[0])[:nbr_results]] imagesearch.plot_results(src, res)
import pickle from PCV.imagesearch import imagesearch from PCV.localdescriptors import sift from sqlite3 import dbapi2 as sqlite from PCV.tools.imtools import get_imlist imlist = get_imlist('./first500/') nbr_images = len(imlist) featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)] f = open('./first500/vocabulary.pkl', 'rb') voc = pickle.load(f) f.close() src = imagesearch.Searcher('web.db',voc) locs,descr = sift.read_features_from_file(featlist[0]) iw = voc.project(descr) print 'ask using a histogram...' print src.candidates_from_histogram(iw)[:10] src = imagesearch.Searcher('web.db',voc) print 'try a query...' nbr_results = 12 res = [w[1] for w in src.query(imlist[39])[:nbr_results]] imagesearch.plot_results(src,res)
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] # 显示查询结果 imagesearch.plot_results(src, res_reg[:8]) #常规查询 imagesearch.plot_results(src, res_geom[:8]) #重排后的结果
# 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 # plot the top results imagesearch.plot_results(src,res_reg[:8]) imagesearch.plot_results(src,res_geom[:8])
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 # plot the top results imagesearch.plot_results(src, res_reg[:8]) imagesearch.plot_results(src, res_geom[:8])
# RANSAC model for homography fitting #用单应性进行拟合建立RANSAC模型 model = homography.RansacModel() rank = {} # load image features for result #载入候选图像的特征 for ndx in res_reg[1:]: locs,descr = sift.read_features_from_file(featlist[ndx]) # because 'ndx' is a rowid of the DB that starts at 1 # 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 # 显示查询结果 imagesearch.plot_results(src,res_reg[:8]) #常规查询 imagesearch.plot_results(src,res_geom[:8]) #重排后的结果