图像搜索

一、图像检索的概述
从20世纪70年代开始,有关图像检索的研究就已开始,当时主要是基于文本的图像检索技术(Text-based Image Retrieval,简称TBIR),利用文本描述的方式描述图像的特征,如绘画作品的作者、年代、流派、尺寸等。到90年代以后,出现了对图像的内容语义,如图像的颜色、纹理、布局等进行分析和检索的图像检索技术,即基于内容的图像检索(Content-based Image Retrieval,简称CBIR)技术。CBIR属于基于内容检索(Content-based Retrieval,简称CBR)的一种,CBR中还包括对动态视频、音频等其它形式多媒体信息的检索技术。

二、图像搜索的运行流程及代码
1.在开始搜索之前,我们需要建立图像数据库和图像的视觉单词表示
import pickle
from PCV.imagesearch import vocabulary
from PCV.tools.imtools import get_imlist
from PCV.localdescriptors import sift
##要记得将PCV放置在对应的路径下
#获取图像列表
imlist = get_imlist(’…/…/data/first1000/’)
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)
#保存词汇
with open(’…/…/data/first1000/vocabulary.pkl’, ‘wb’) as f:
pickle.dump(voc, f)
print ‘vocabulary is:’, voc.name, voc.nbr_words

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放置在对应的路径下
##要记得将PCV放置在对应的路径下
#获取图像列表
imlist = get_imlist(‘first1000/’)##记得改成自己的路径
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+‘sift’ for i in range(nbr_images)]
#载入词汇
with open(‘first1000/vocabulary.pkl’, ‘rb’) as f:
voc = pickle.load(f)
#创建索引
indx = imagesearch.Indexer(‘testImaAdd.db’,voc)
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()

2.在数据库中搜索图像
import pickle
from PCV.localdescriptors import sift
from PCV.imagesearch import imagesearch
from PCV.geometry import homography
from PCV.tools.imtools import get_imlist
#载入图像列表
imlist = get_imlist(‘first1000/’) ##要改成自己的地址
nbr_images = len(imlist)
#载入特征列表
featlist = [imlist[i][:-3]+‘sift’ for i in range(nbr_images)]
#载入词汇
with open(‘first1000/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)
t
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]) #重排后的结果

3.使用几何特性对结果排序

4.建立演示程序及Web应用
import cherrypy
import pickle
import urllib
import os
from numpy import *
#from PCV.tools.imtools import get_imlist
from PCV.imagesearch import imagesearch
“”"
This is the image search demo in Section 7.6.
“”"
class SearchDemo:
def init(self):
self.path = ‘first1000/’
#self.path = ‘D:/python_web/isoutu/first500/’
self.imlist = [os.path.join(self.path,f) for f in os.listdir(self.path) if
f.endswith(’.jpg’)]
#self.imlist = get_imlist(’./first500/’)
#self.imlist = get_imlist(‘E:/python/isoutu/first500/’)
self.nbr_images = len(self.imlist)
print str(len(self.imlist))+"###############"
self.ndx = range(self.nbr_images)
with open(‘first1000/vocabulary.pkl’,‘rb’) as f:
self.voc = pickle.load(f)
#f.close()
self.maxres = 10
self.header = “”"
<!doctype html>

Image search """ self.footer = """ """ def index(self, query=None): self.src = imagesearch.Searcher('testImaAdd.db', self.voc) html = self.header html += """
Click an image to search. Random selection of images.

""" if query: 运行这个代码还需要一个配置文件 service.conf 这个是web服务器的配置文件,内容如下: 配置文件中的第一部分为IP地址和端口,第二部分为我们的图库的地址 #查询数据库,并获取前面的图像 res = self.src.query(query)[:self.maxres] for dist, ndx in res: imname = self.src.get_filename(ndx) html += " " html += ""+imname+"" print imname+"################" html += "" else: random.shuffle(self.ndx) for i in self.ndx[:self.maxres]: imname = self.imlist[i] html += " " html += ""+imname+"" print imname+"################" html += "" html += self.footer return html index.exposed = True #conf_path = os.path.dirname(os.path.abspath(__file__)) #conf_path = os.path.join(conf_path, "service.conf") #cherrypy.config.update(conf_path) #cherrypy.quickstart(SearchDemo()) cherrypy.quickstart(SearchDemo(), '/', config=os.path.join(os.path.dirname(__file__), 'service.conf'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值