目录
一.图像检索相关介绍:
1.起因:
2.参数介绍:
图像纹理:
词序无关的文本表述:
图像分类:
图像特征词典:
图像整体描述子:
二. 图像分类/检索具体内容:
图像分类/检索:
单词的TF-IDF权重:
倒排表(Inverted file):
图像检索流程:
图像检索结果:
三.代码实现:
1.数据集:
2.创建数据:
3.建立数据库:
4.检索:
一.图像检索相关介绍:
1.起因:
2.参数介绍:
图像纹理:
纹理是指图像中的重复模式,或纹理基元组成的结构

词序无关的文本表述:
根据文本中的词频分布,构造文本描述子;

图像分类:
对于图像我们可以将它分类成很多小组成;
图像特征词典:
Bag of features ( BOF)一种适用于图像和视频检索的算法。BOF借鉴了文本分类的思路(也就是BOW),从图像抽象出很多具有代表性的「关键词」,形成一个字典,再统计每张图片中出现的「关键词」数量,得到图片的特征向量。
基础流程:
1. 特征提取;
2. 学习 “视觉词典(visual vocabulary)” ;
3. 针对输入特征集,根据视觉词典进行量化;
对于输入特征,量化的过程是将该特征映射到距离其最接近的 codevector ,并实现 计数 ,码本 = 视觉词典 ,Codevector = 视觉单词 。

算法流程: • 随机初始化 K 个聚类中心
4. 把输入图像转化成视觉单词(visual words)的频率直方图;

视觉词典存在的问题:
图像整体描述子:

二. 图像分类/检索具体内容:
图像分类/检索:

单词的TF-IDF权重:

倒排表(Inverted file):
图像检索流程:
图像检索结果:

三.代码实现:
1.数据集:
55张集美大学图片
59张厦门图片
2.创建数据:
import pickle
from PCV.imagesearch import vocabulary
from PCV.tools.imtools import get_imlist
from PCV.localdescriptors import sift# 获取图像列表
imlist = get_imlist('D:\\pythonProject\\computer5\\picture\\sumpicture\\')
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('training')
voc.train(featlist, 114, 10)# 保存词汇
# saving vocabulary
with open('D:\\pythonProject\\computer5\\picture\\sumpicture\\vocabulary.pkl', 'wb') as f:pickle.dump(voc, f)
print('vocabulary is:', voc.name, voc.nbr_words)
结果:
3.建立数据库:
import pickle
from PCV.imagesearch import imagesearch
from PCV.localdescriptors import sift
import sqlite3
from PCV.tools.imtools import get_imlist# 获取图像列表
# imlist = get_imlist('E:/Python37_course/test7/first1000/')
imlist = get_imlist('D:\\pythonProject\\computer5\\picture\\sumpicture\\')
nbr_images = len(imlist)
# 获取特征列表
featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]# load vocabulary
# 载入词汇
with open('D:\\pythonProject\\computer5\\picture\\sumpicture\\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
# 遍历所有的图像,并将它们的特征投影到词汇上(比如我的是180张图片)
for i in range(nbr_images)[:179]:locs, descr = sift.read_features_from_file(featlist[i])indx.add_to_index(imlist[i], descr)
# commit to database
# 提交到数据库
indx.db_commit()con = sqlite3.connect('testImaAdd.db')
print(con.execute('select count (filename) from imlist').fetchone())
print(con.execute('select * from imlist').fetchone())
结果:
4.检索:
建立好图像的索引,就可以在数据库中搜索相似的图像了。这里,使用BOW(词袋模型)来表示整个图像,这是通用的,可以应用于寻找相似的物体、相似的脸、相似的颜色等,它完全取决于图像及所用的描述子。
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('D:\\pythonProject\\computer5\\picture\\sumpicture\\') # 存放数据集的路径
nbr_images = len(imlist)
# 载入特征列表
featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]# 载入词汇
with open('D:\\pythonProject\\computer5\\picture\\sumpicture\\vocabulary.pkl', 'rb') as f: # 存放模型的路径voc = pickle.load(f)
src = imagesearch.Searcher('testImaAdd.db', voc)# index of query image and number of results to return
# 查询图像索引和查询返回的图像数
q_ind = 18
nbr_results = 5# 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:]:locs, descr = sift.read_features_from_file(featlist[ndx]) # because 'ndx' is a rowid of the DB that starts at 1# get matches# 获取匹配数 # 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 countrank[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]) # 重排后的结果
结果: