print "regular results for query %d:" % query_imid, res # Rerank by trying to fit a homography. q_locs, q_descr = sift.read_features_from_file(featlist[query_imid]) fp = homography.make_homog(q_locs[:, :2].T) model = homography.RansacModel() rank = {} for ndx in res[1:]: locs, descr = sift.read_features_from_file(featlist[ndx - 1]) # res is 1-based matches = sift.match(q_descr, descr) ind = matches.nonzero()[0] ind2 = [int(matches[i]) for i in ind] tp = homography.make_homog(locs[:, :2].T) try: H, inliers = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_threshold=4) except: inliers = [] rank[ndx] = len(inliers) sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res[0]] + [s[0] for s in sorted_rank] print "homography results for query %d" % query_imid, res_geom imagesearch.plot_results(searcher, res[:8]) imagesearch.plot_results(searcher, res_geom[:8])
fp = homography.make_homog(q_locs[:, :2].T) model = homography.RansacModel() rank = {} for ndx in res[1:]: locs, descr = sift.read_features_from_file(featlist[ndx - 1]) # res is 1-based matches = sift.match(q_descr, descr) ind = matches.nonzero()[0] ind2 = [int(matches[i]) for i in ind] tp = homography.make_homog(locs[:, :2].T) try: H, inliers = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_threshold=4) except: inliers = [] rank[ndx] = len(inliers) sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res[0]] + [s[0] for s in sorted_rank] print 'homography results for query %d' % query_imid, res_geom imagesearch.plot_results(searcher, res[:8]) imagesearch.plot_results(searcher, res_geom[:8])
def euclid(): nbr_results = 6 for n_im in xrange(len(imlist)): print "n_im=", n_im res = [w[1] for w in src.query(imlist[n_im])[:nbr_results]] imagesearch.plot_results(src, res)
import imagesearch import pickle import sift import imtools import vocabulary as voc # import PIL and pylab for plotting from PIL import Image from scipy.cluster.vq import * from numpy import * from pylab import * #Teste de apresentacao de varios clusters # get list of images imlist = imtools.get_imlist('../img/sunsets/treino/') nbr_images = len(imlist) featurefiles = [ imlist[i][:-3]+'sift' for i in range(nbr_images) ] nbr_results = 6 for i in range(2): image = imlist[i] res = [w[1] for w in src.query(image)[:nbr_results]] imagesearch.plot_results(src,res)
"""After ch07_buildindex.py has built an index in test.db, this program can query it. """ imlist = imtools.get_imlist('/Users/thakis/Downloads/ukbench/first1000')[:100] imcount = len(imlist) featlist = [imlist[i][:-3] + 'sift' for i in range(imcount)] with open('vocabulary.pkl', 'rb') as f: voc = pickle.load(f) searcher = imagesearch.Searcher('test.db', voc) locs, descr = sift.read_features_from_file(featlist[0]) imwords = voc.project(descr) print 'ask using a histogram...' print searcher.candidates_from_histogram(imwords)[:10] print 'try a query...' res = searcher.query(imlist[0])[:10] print res print 'score:' # Score a small subset, so this runs fast. print imagesearch.compute_ukbench_score(searcher, imlist[:10]) # Plot images most similar to imlist[0]. imagesearch.plot_results(searcher, [r[1] for r in res[:6]])
print 'top matches (regular):', res_reg # 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]) # 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])
# 検索結果の画像特徴量を読み込む for ndx in res_reg[1:]: locs,descr = sift.read_features_from_file(featlist[ndx-1]) locs[:,[0,1]] = locs[:,[1,0]] # 一致度を調べる matches = sift.match(q_descr,descr) ind = matches.nonzero()[0] ind2 = matches[ind] tp = homography.make_homog(locs[:,:2].T) # ホモグラフィーを計算し、インライアを数える。 # 一致度が足りなければ空リストを返す。 try: H,inliers = homography.H_from_ransac(fp[:,ind],tp[:,ind2], model,match_theshold=4) except: inliers = [] # インライアの数を格納する rank[ndx] = len(inliers) # 最もモデルに当てはまるものが先頭になるよう辞書をソートする 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])
q_locs, q_descr = sift.read_feature_from_file(feature[q_ind]) fp = homography.make_homog(q_locs[:, :2].T) model = homography.RansacModel() rank = {} # load image features for result #载入候选图像的特征 for ndx in res_reg[1:]: locs,descr = sift.read_feature_from_file(feature[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]) #重排后的结果
import imagesearch """After ch07_buildindex.py has built an index in test.db, this program can query it. """ imlist = imtools.get_imlist('/Users/thakis/Downloads/ukbench/first1000')[:100] imcount = len(imlist) featlist = [imlist[i][:-3] + 'sift' for i in range(imcount)] with open('vocabulary.pkl', 'rb') as f: voc = pickle.load(f) searcher = imagesearch.Searcher('test.db', voc) locs, descr = sift.read_features_from_file(featlist[0]) imwords = voc.project(descr) print 'ask using a histogram...' print searcher.candidates_from_histogram(imwords)[:10] print 'try a query...' res = searcher.query(imlist[0])[:10] print res print 'score:' # Score a small subset, so this runs fast. print imagesearch.compute_ukbench_score(searcher, imlist[:10]) # Plot images most similar to imlist[0]. imagesearch.plot_results(searcher, [r[1] for r in res[:6]])
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, inliers = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_theshold=4) except: inliers = [] # 存储内点数 rank[ndx] = len(inliers) # 将字典排序,以首先获取最内层的内点数 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 # 显示靠前的搜索结果 for i in range(8): imagesearch.plot_results(src, res_reg[i:i + 1]) imagesearch.plot_results(src, res_geom[i:i + 1])
def euclid(): nbr_results = 6 for n_im in xrange(len(imlist)): print "n_im=",n_im res = [w[1] for w in src.query(imlist[n_im])[:nbr_results]] imagesearch.plot_results(src,res)
import pickle imlist = imtools.get_imlist('first500') nbr_images = len(imlist) featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)] ''' for i in range(nbr_images): sift.process_image(imlist[i],featlist[i]) ''' f = open('vocabulary.pkl', 'rb') voc = pickle.load(f) f.close() src = imagesearch.Searcher('test.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] print '\n==================\n' print 'try a query...' print src.query(imlist[0])[:10] print 'score = ', imagesearch.compute_ukbench_score(src, imlist[:4]) nbr_results = 6 res = [w[1] for w in src.query(imlist[0])[:nbr_results]] imagesearch.plot_results(src, res)