搜索图片
一、BOF图像检索1.1 简介
Bof,即Bag of features图像检索的流程及其关键技术,中文翻译为“词袋”,是一种用于图像或视频检索的技术。搜索是为了比较。如何比较两个不同的图像,比较什么,这需要提取每张图像中的精炼的东西进行比较。就像超市里的条码一样,它可以很好地反映一个商品的所有特征。所以简而言之,bof就是为每张图片生成一个“条形码”用于检索。
1.2进程
1.特征提取:通过sift算法提取图像特征。
2.学习视觉词典。
3.对于输入的特征集,根据视觉词典进行量化。
4.根据TF-IDF将输入图像转化为视觉词的频率直方图。
5.对图像构建特征倒排表,通过倒排表快速索引相关图像。根据索引结果进行直方图匹配。
二、代码2.1sift 特征匹配
# -*- coding: utf-8 -*
import pickle
from PCV.imagesearch import vocabulary
from PCV.tools.imtools import get_imlist
from PCV.localdescriptors import sift
# 要记得将PCV放置在对应的路径下
# 获取图像列表
imlist = get_imlist('D:/pycharm/untitled1/image/') # 存放数据集的路径
nbr_images = len(imlist) # 获取数据集的长度
# nbr_images = 300 # 可以是自己选择用多少张图片作为训练数据集
# 获取特征列表
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('imglltest')
voc.train(featlist, 300, 10)
# 保存词汇
with open('D:/pycharm/untitled1/vocabulary.pkl', 'wb') as f:
pickle.dump(voc, f)
print 'vocabulary is:', voc.name, voc.nbr_words
2.2图像集特征提交到数据库
# -*- coding: utf-8 -*-
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
# 要记得将PCV放置在对应的路径下
# 获取图像列表
imlist = get_imlist('D:/pycharm/untitled1/image/') # 存放数据集的路径
nbr_images = len(imlist)
![图片[1]-图像检索的流程及注意事项有哪些?-乐题库-唐朝资源网](https://images.43s.cn/wp-content/uploads//2022/09/1662480028469_1.gif)
# nbr_images = 300
# 获取特征列表
featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]
# 载入词汇
with open('D:/pycharm/untitled1/vocabulary.pkl', 'rb') as f: # 读取再上一步中保存的.pkl文件
voc = pickle.load(f)
# 创建索引
index = imagesearch.Indexer('testImaAdd.db', voc) # 创建数据库
index.create_tables() # 创建数据库表单
# 遍历所有的图像,并将它们的特征投影到词汇上
for i in range(nbr_images)[:300]:
locs, descr = sift.read_features_from_file(featlist[i])
index.add_to_index(imlist[i], descr)
# 提交到数据库
index.db_commit()
con = sqlite.connect('testImaAdd.db') # 连接到数据库
print con.execute('select count (filename) from imlist').fetchone() # 数据库操作
print con.execute('select * from imlist').fetchone()
2.3图像检索
# -*- 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
# 要记得将PCV放置在对应的路径下
# 载入图像列表
imlist = get_imlist('D:/pycharm/untitled1/image/') # 存放数据集的路径
nbr_images = len(imlist)
# nbr_images = 300
# 载入特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
# 载入词汇
with open('D:/pycharm/untitled1/vocabulary.pkl', 'rb') as f: # 存放模型的路径
voc = pickle.load(f)
src = imagesearch.Searcher('testImaAdd.db', voc)
imlist = get_imlist('D:/pycharm/untitled1/image/') # 存放数据集的路径
# 查询图像索引和查询返回的图像数
![图片[3]-图像检索的流程及注意事项有哪些?-乐题库-唐朝资源网](https://images.43s.cn/wp-content/uploads//2022/09/1662480028469_3.gif)
q_ind = 030 # 查询图片的索引
nbr_results = 5
# 常规查询(按欧式距离对结果排序)
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])
# 获取匹配数
# get matches执行完后会出现两张图片
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[:5]) # 常规查询
imagesearch.plot_results(src, res_geom[:5]) # 重排后的结果
三、图片库
四、结果
使用第一张图像检索输入
匹配结果为:
有三张图片没有匹配成功。结果可能是在创建词汇表时图像检索的流程及其关键技术,特征描述比较相似,数据集不够大,这也是造成不匹配的原因。
© 版权声明
本站下载的源码均来自公开网络收集转发二次开发而来,
若侵犯了您的合法权益,请来信通知我们1413333033@qq.com,
我们会及时删除,给您带来的不便,我们深表歉意。
下载用户仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,访问及下载者下载默认同意本站声明的免责申明,请合理使用切勿商用。
THE END
暂无评论内容