图像检索
什么是图像检索:图像检索如今已经应用在很多的领域,生活中我们最常见的例子就是百度识图,我们输入一张图片,它就可以帮我们搜索到很多相类似的图片。那么问题来了,类似百度识图这种图像搜索引擎,它是怎么识别出我们输入的图片的呢?同时在识别出我们的图片后,它又是从哪里把类似的图片显示给用户的呢?这就是后面要解释的图像检索。图像检索主要有两个方面,一个是基于文本的图像检索(TBIR),另一个是基于内容的图像检索(CBIR)。
TBIR
基于文本的图像检索主要是利用文本标注的方式为图像添加关键词,这种方式需要人为给每一张图像标注,非常耗费人工。
CBIR
基于内容的图像检索免去了人工标注。这种检索方式首先需要准备一个数据集(dateset)作为训练,通过某种算法提取数据集中每张图像的特征(SIFT特征)向量,然后将这些特征存储起来,组成一个数据库,当需要搜索某张图片的时候,就输入这张图片,然后提取输入图片的特征,用某种匹配准则将提取的输入图片的特征和数据库中的特征进行比较,最后从数据库中按照相似度从大到小输出相似的图片。
图像检索代码
数据
共24张图片

代码
# -*- 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('D:/pythonProjects/ImageRetrieval/first500/')
imlist = get_imlist('D:/pythonProjects/ImageRetrieval/animaldb/')
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)
#保存词汇
# saving vocabulary
with open('D:/pythonProjects/ImageRetrieval/animaldb/vocabulary.pkl', 'wb') as f:
pickle.dump(voc, f)
print('vocabulary is:', voc.name, voc.nbr_words)
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
#获取图像列表
imlist = get_imlist('D:/pythonProjects/ImageRetrieval/animaldb/')
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
# load vocabulary
#载入词汇
with open('D:/pythonProjects/ImageRetrieval/animaldb/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())
//
# -*- 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:/Python37_course/test7/first1000/')
imlist = get_imlist('D:/pythonProjects/ImageRetrieval/animaldb/')
nbr_images = len(imlist)
#载入特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
#载入词汇
with open('D:/pythonProjects/ImageRetrieval/animaldb/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[:3]) #常规查询
imagesearch.plot_results(src,res_geom[:3]) #重排后的结果
检索图像

类别K=100

本文介绍了图像检索的基本概念,包括基于文本的图像检索(TBIR)和基于内容的图像检索(CBIR)。CBIR通过提取图像特征并建立数据库进行搜索。文中给出了使用Python实现CBIR的代码示例,涉及SIFT特征提取、词汇生成、数据库索引和查询。通过RANSAC模型进行匹配优化,提高检索效果。
2530

被折叠的 条评论
为什么被折叠?



