图像特征词典原理及实现
图像特征词典原理及实现
原理
⼀.Bag of features: 基础流程
1. 特征提取
2. 学习 “视觉词典(visual vocabulary)”
3. 针对输⼊特征集,根据视觉词典进⾏量化
聚类是实现 visual vocabulary /codebook的关
· ⽆监督学习策略
· k-means 算法获取的聚类中⼼作为 codevector
· Codebook 可以通过不同的训练集协同训练获得
· ⼀旦训练集准备⾜够充分, 训练出来的码本( codebook)将
具有普适性
码本/字典⽤于对输⼊图⽚的特征集进⾏量化
· 对于输⼊特征,量化的过程是将该特征映射到距离其最接近
的 codevector ,并实现计数
·
码本 = 视觉词典
· Codevector = 视觉单词
4.把输⼊图像转化成视觉单词(visual words)
的频率直⽅图
5.构造特征到图像的倒排表,通过倒排表快速索引相关图像
给定图像的bag-of-features直⽅图特征,如何
实现图像分类/检索?
给定输⼊图像的BOW直⽅图, 在数据库中查 k 个最近邻的图像
对于图像分类问题,可以根据这k个近邻图像的分类标签,投票获得分类结果
当训练数据⾜以表述所有图像的时候,检索/分类效果良
6.根据索引结果进⾏直⽅图匹配
代码及实现
from PCV.imagesearch import vocabulary
ls.imtools import get_imlist
from PCV.localdescriptors import sift
##要记得将PCV放置在对应的路径下
#获取图像列表
imlist = get_imlist(‘D:/Visual_Studio_Code/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])
import pickle#⽣成词汇
voc = vocabulary.Vocabulary(‘ukbenchtest’)
#保存词汇
#saving vocabulary
with open(r’D:\Visual_Studio_Code\data\first1000\vocabulary.pkl’, ‘wb’) as f: pickle.dump(voc, f)
print (‘vocabulary is:’, , 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
ls.imtools import get_imlist
##要记得将PCV放置在对应的路径下
#获取图像列表
imlist = get_imlist('D:/Visual_Studio_Code/data/first1000/')##记得改成⾃⼰的路径
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
# load vocabulary
#载⼊词汇
with open(r'D:\Visual_Studio_Code\data\first1000\vocabulary.pkl', 'rb') as f:
voc = pickle.load(f)
#创建索引
indx = imagesearch.Indexer('testImaAdd.db',voc)
# go through all images, project features on vocabulary and insert
#遍历所有的图像,并将它们的特征投影到词汇上
for i in range(nbr_images)[:1000]:
locs,descr = ad_features_from_file(featlist[i])
indx.add_to_index(imlist[i],descr)
# commit to database
#提交到数据库
indx.db_commit()
con = t('testImaAdd.db')
print (ute('select count (filename) from imlist').fetchone())
print (ute('select * from imlist').fetchone())
from PCV.localdescriptors import sift
from PCV.imagesearch import imagesearch
ry import homography
ls.imtools import get_imlist
load image list and vocabulary
#载⼊图像列表
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)
#index of query image and number of results to return
#查询图像索引和查询返回的图像数
q_ind = 0
nbr_results = 20
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 = ad_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 = ad_features_from_file(featlist[ndx]) # because ‘ndx’ is a rowid of the DB that starts at 1 # get matches
matches = sift.match(q_descr,descr)
ind = o()[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)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。