计算机视觉--BOF图像检索

目录

 

一.Bag Of Features简介

二.BOF图像检索流程

三.代码实现

四.实验结果与分析

五.实验遇到的问题


一.Bag Of Features简介

特征包(Bag-of-Features)模型来源于文本检索中的Bag of Words模型,所谓的Bag of Words模型意思是一篇文档是山很多单词组成的,描述这篇文档的方法是将这篇文档映射到对应的词典(codebook)中,通过观察每个单词出现的频率确定文档主题。图像也是由很多视觉单词组成,描述一幅图像的内容通过计算图像映射到对应视觉单词上的频率来表达。

二.BOF图像检索流程

1.特征提取:通过sift算法提取图像特征。 

特征必须具有较高的区分度,而且要满足旋转不变性以及尺寸不变性等,因此,我们通常都会采用sift特征(有时为了降低计算量,也会采用其他特征,如:SURF )。sift会从图片上提取出很多特征点,每个特征点都是 128 维的向量,因此,如果图片足够多的话,我们会提取出一个巨大的特征向量库。

2.学习“视觉词典”。

提取完特征后,我们会采用一些聚类算法对这些特征向量进行聚类。最常用的聚类算法是 k-means。聚类是学习视觉词典的重点操作。将聚类出来的聚类中心称为视觉单词(codevector)。而将视觉单词组成的集合称为视觉词典/码本(codebook)

3.针对输入特征集,根据视觉词典进行量化。

应选择合适的视觉词典/码本的规模,因为:
•太少:视觉单词无法覆盖所有可能出现的情况
•太多: 计算量大,容易过拟合

4.把输入图像,根据TF-IDF转化成视觉单词(visual words)的频率直方图。

这一步骤通过对图像特征提取,然后将提取出来的特征点,根据第四步,转换为频率直方图。

5.构造特征到图像的倒排表,通过倒排表快速索引相关图像

获取具有相似单词的图像列表,可以检索每个单词获得候选集并将其合并到列表中,获取单词 id后,寻找候选图像,而后获取所有唯一的单词,并按出现次数反向排序,得到按匹配度由高到低的候选图像列表,通过这个倒排表可快速索引相关图像的id

6.根据索引结果进行直方图匹配。

利用已建好的索引找到包含特定单词的所有图像。为了获得包含多个单词的候选图像,有两种解决方法。

  • 在每个单词上进行遍历,得到包含该单词的所有图像,然后合并这些列表。接着对在合并了的列表中,对每一个图像id出现的次数进行跟踪排序,排在列表最前面的是最好的匹配图像。
  • 如果不想遍历所有的单词,可以根据其倒排序文档频率权重进行排序,并使用那些权重最高的单词,在这些单词上进行遍历,减少计算量,提高运行的效率。
     

三.代码实现

1.数据集

该数据集有100张图片组成

2.特征提取,建立视觉单词词汇

# -*- 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:/CVphoto/05241/')
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)
#保存词汇
# saving vocabulary
with open('D:/CVphoto/05241/vocabulary.pkl', 'wb') as f:
    pickle.dump(voc, f)
print ('vocabulary is:', voc.name, voc.nbr_words)

2.建立数据库,将得到的数据模型存放到数据库testImaAdd.db中

 

# -*- 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('first1002/')
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]

# load vocabulary
#载入词汇
with open('first1002/vocabulary.pkl', 'rb') as f:
    voc = pickle.load(f)
#创建索引
indx = imagesearch.Indexer('testImaAdd3.db',voc)
indx.create_tables()
# go through all images, project features on vocabulary and insert
    #遍历所有的图像,并将它们的特征投影到词汇上
for i in range(nbr_images)[:1000]:
    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('testImaAdd3.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('first1002/')
nbr_images = len(imlist)
#载入特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]

#载入词汇
with open('first1002/vocabulary.pkl', 'rb') as f:
    voc = pickle.load(f)

src = imagesearch.Searcher('testImaAdd3.db',voc)

# index of query image and number of results to return
#查询图像索引和查询返回的图像数
q_ind = 47
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
    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[:8]) #常规查询
imagesearch.plot_results(src,res_geom[:8]) #重排后的结果

 

四.实验结果与分析

(1)第一组

测试图片:

 

50维度:

100维度:

1000维度:

 

 

 

(2)第二组

测试图片:

50维度:

100维度:

1000维度:

 

 

结果分析:在进行第一张图片搜索时,在不同维度的情况下,能够匹配到的图像还是比较精确的,可能是图片角点多,能够检测到的特征点比较多,然后这组图片数量比较多,字典频率比较高,容易检索到,所以匹配效果比较好。但是进行第二张图片检索时,当维度为1000的时候,匹配到的蹄片都是错的,可能是因为维度大了,过度拟合,特征分的类别太多了,特征性不强,对一些图片的检索会出现误差。

五.实验遇到的问题与总结

可以到 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl下载安装pysqlite。

(1)不同维度下检索效果不同,维度过大过小都会影响检测效果。维度过小,图片的类别划分小且模糊,维度过大,会过度拟合,检测容易出现误差。

(2)图片类别数量较多时,字典频率较高,容易检索到。

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值