# -*- coding: utf-8 -*- import pickle from PCV.imagesearch import vocabulary from PCV.tools.imtools import get_imlist from PCV.localdescriptors import sift ###创建词汇 # 获取图像列表 imlist = get_imlist('C:/Users/y3420/Downloads/newjin/newjin/') nbr_images = len(imlist) # 获取特征列表 featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)] # 提取文件夹下图像的sift特征 #for i in range(nbr_images): # sift.process_image(imlist[i], featlist[i]) # 生成词汇 voc = vocabulary.Vocabulary('ukbenchtest') # 创建词汇类 voc.train(featlist, 1000, 10) # 单词数为1000,进行k-means训练 # 保存词汇 with open('C:/Users/y3420/Downloads/newjin/newjin/vocabulary.pkl', 'wb') as f: pickle.dump(voc, f) print('vocabulary is:', voc.name, voc.nbr_words) # -*- coding: utf-8 -*- import pickle from PCV.imagesearch import imagesearch from PCV.localdescriptors import sift import sqlite3 as sqlite from PCV.tools.imtools import get_imlist # 获取图像列表 nbr_images = len(imlist) # 获取特征列表 featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)] # 载入词汇 with open('C:/Users/y3420/Downloads/newjin/newjin/vocabulary.pkl', 'rb') as f: voc = pickle.load(f) # 创建索引 indx = imagesearch.Indexer('testImaAdd.db', voc) # 创建索引器Indexer类 #indx.create_tables() # 遍历所有的图像,并将它们的特征投影到词汇上 for i in range(nbr_images)[:1000]: locs, descr = sift.read_features_from_file(featlist[i]) indx.add_to_index(imlist[i], descr) # 提交到数据库 indx.db_commit() con = sqlite.connect('testImaAdd.db') print(con.execute('select count (filename) from imlist').fetchone()) print(con.execute('select * from imlist').fetchone()) # -*- coding: utf-8 -*- import pickle from PCV.localdescriptors import sift from PCV.imagesearch import imagesearch from PCV.geometry import homography from PCV.tools.imtools import get_imlist # 载入图像列表 nbr_images = len(imlist) # 载入特征列表 featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)] # 载入词汇 with open('C:/Users/y3420/Downloads/newjin/newjin/vocabulary.pkl', 'rb') as f: voc = pickle.load(f) src = imagesearch.Searcher('testImaAdd.db', voc) # 查询图像索引和查询返回的图像数 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) # 用单应性进行拟合建立RANSAC模型 model = homography.RansacModel() rank = {} # 载入候选图像的特征 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 # 获取匹配数 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]) # 重排后的结果
——————————————————————————————————————————
由于原版使用的python2已经过时了,修改后才可以运行,比如:
imagesearch.py中
tmp.sort(cmp=lambda x,y:cmp(x[1],y[1]))
要改为
tmp=sorted(tmp, key=cmp_to_key(lambda x,y:operator.eq(x[1],y[1])))
其他问题网上都能找到解决方案,就不一一罗列了