一、图像检索
从20世纪70年代开始,有关图像检索的研究就已开始,当时主要是 基于文本的图像检索技术 (Text-based Image Retrieval,简称 TBIR),利用文本描述的方式描述图像的特征,如绘画作品的作者、年代、流派、尺寸等。到90年代以后,出现了对图像的内容语义,如图像的颜色、纹理、布局等进行分析和检索的图像检索技术,即 基于内容的图像检索 (Content-based Image Retrieval,简称 CBIR)技术。
因此按描述图像内容方式的不同可以分为两类:
1.基于文本的图像检索(TBIR, Text Based Image Retrieval)
2.基于内容的图像检索(CBIR, Content Based Image Retrieval)
1.基于文本的图像检索
基于文本的图像检索主要是利用文本标注的方式为图像添加关键词,这种方式需要人为给每一张图像标注,非常耗费人工。
2.基于内容的图像检索
基于内容的图像检索免去了人工标注。这种检索方式首先需要准备一个数据集(dateset)作为训练,通过某种算法提取数据集中每张图像的特征(SIFT特征)向量,然后将这些特征存储起来,组成一个数据库,当需要搜索某张图片的时候,就输入这张图片,然后提取输入图片的特征,用某种匹配准则将提取的输入图片的特征和数据库中的特征进行比较,最后从数据库中按照相似度从大到小输出相似的图片
基于文本的图像检索
基于文本的图像检索主要是利用文本标注的方式为图像添加关键词,这种方式需要人为给每一张图像标注,非常耗费人工。
基于内容的图像检索
基于内容的图像检索免去了人工标注。这种检索方式首先需要准备一个数据集(dateset)作为训练,通过某种算法提取数据集中每张图像的特征(SIFT特征)向量,然后将这些特征存储起来,组成一个数据库,当需要搜索某张图片的时候,就输入这张图片,然后提取输入图片的特征,用某种匹配准则将提取的输入图片的特征和数据库中的特征进行比较,最后从数据库中按照相似度从大到小输出相似的图片
3.Bag of features
Bag of Feature 是一种图像特征提取方法,它借鉴了文本分类的思路(Bag of Words),从图像抽象出很多具有代表性的「关键词」,形成一个字典,再统计每张图片中出现的「关键词」数量,得到图片的特征向量。
Bag of features 图像检索流程
特征提取
学习 “视觉词典(visual vocabulary)”
针对输入特征集,根据视觉词典进行量化
把输入图像转化成视觉单词(visual words)的频率直方图
构造特征到图像的倒排表,通过倒排表快速索引相关图像
根据索引结果进行直方图匹配
单词的TF-IDF权重
在上述介绍中,在矢量空间模型中提到了单词权重,在文本检索中,不同单词对文本检索的贡献有差异,所以在将输入图像转换为频率直方图时需要根据TF-IDF赋予权值。具体流程在上述视觉单词模块中提及。
某一特定文件内的高词语频率,以及该词语在整个文件集合中的低文件频率,可以产生出高权重的TF-IDF。因此,TF-IDF倾向于过滤掉常见的词语,保留重要的词语
倒排表
倒排表是一种逆向的索引方法,构造倒排表可以快速索引图像。倒排索引,通过搜索要查询的关键字,查询到跟该关键字相关的所有文档。倒排表可以获得是各视觉单词出现在图像库的哪些图像中
直方图匹配
最后,根据索引的结果进行直方图匹配,就完成了图像索引
二、实验结果与分析
1.代码
生成词汇字典. 提取SIFT特征
import pickle
from PCV.imagesearch import vocabulary
from PCV.tools.imtools import get_imlist
from PCV.localdescriptors import sift
from PCV.imagesearch import imagesearch
from PCV.geometry import homography
from sqlite3 import dbapi2 as sqlite
#获取图像列表
imlist = get_imlist('datasets/')
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, 888, 10) # 使用k-means算法在featurelist里边训练处一个词汇
# 注意这里使用了下采样的操作加快训练速度
# 将描述子投影到词汇上,以便创建直方图
#保存词汇
# saving vocabulary
with open('BOW/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
from sqlite3 import dbapi2 as sqlite
from PCV.tools.imtools import get_imlist
#获取图像列表
imlist = get_imlist('D:/pythonProjects/ImageRetrieval/')
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
# load vocabulary
#载入词汇
with open('./BOW/vocabulary.pkl', 'rb') as f:
voc = pickle.load(f)
#创建索引
indx = imagesearch.Indexer('testImaAdd.db',voc)
indx.create_tables()
# go through all images, project features on vocabulary and insert
#遍历所有的图像,并将它们的特征投影到词汇上
for i in range(nbr_images)[:500]:
locs,descr = sift.read_features_from_file(featlist[i])
indx.add_to_index(imlist[i],descr)
# commit to database
#提交到数据库
indx.db_commit()
con = sqlite.connect('testImaAdd.db')
print(con.execute('select count (filename) from imlist').fetchone())
print(con.execute('select * from imlist').fetchone())
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
# load image list and vocabulary
#载入图像列表
#imlist = get_imlist('E:/Python/first1000/')
imlist = get_imlist('D:/python/animaldb/')
nbr_images = len(imlist)
#载入特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
#载入词汇
with open('D:/python/vocabulary.pkl', 'rb') as f:
voc = pickle.load(f)
src = imagesearch.Searcher('testImaAdd.db',voc)# Searcher类读入图像的单词直方图执行查询
# index of query image and number of results to return
#查询图像索引和查询返回的图像数
q_ind = 0 # 匹配的图片下标
nbr_results = 148 # 数据集大小
# regular query
# 常规查询(按欧式距离对结果排序)
res_reg = [w[1] for w in src.query(imlist[q_ind])[:nbr_results]] # 查询的结果
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
#用单应性进行拟合建立RANSAC模型
model = homography.RansacModel()
rank = {}
# load image features for result
#载入候选图像的特征
for ndx in res_reg[1:]:
try:
locs,descr = sift.read_features_from_file(featlist[ndx]) # because 'ndx' is a rowid of the DB that starts at 1
except:
continue
# 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[:6]) #常规查询
imagesearch.plot_results(src,res_geom[:6]) #重排后的结果
2.图片库:
3.运行结果
输入的图片:
运行结果:
三、实验 总结
这此实验的图片是在网上爬虫下载的,我使用了PhotoShop将张图片统一改成了500✖400的大小。减小图片的大小已减少运行时间。输入一只公鸡的图像,在数据库中检索,结果显示了不止有鸡的图像,可能原因是出现的那只鸟的图片的背景与本身与输入的鸡的图片相似。说明实验还是不够成功,是有缺陷的。