基于bow的图像检索

图像检索与识别

目录

图像检索与识别

Bag-of-words模型

Bag of features:基础流程

K-means聚类算法

Bag of features:学习特征词典

Bag of features:图像检索

实验过程

1.生成sift特征集并保存模型:

2.载入词汇,遍历图像并把特征投影到词汇上

3.测试


Bag-of-words模型


Bag-of-words模型的基本思想是假定对于一个文本,忽略其词序和语法、句法,仅仅将其看做是一些词汇的集合,而文本中的每个词汇都是独立的。简单说就是讲每篇文档都看成一个袋子(因为里面装的都是词汇,所以称为词袋,Bag of words即因此而来),然后看这个袋子里装的都是些什么词汇,将其分类。

比如说,通过一定数量(比如说100张)的猫或狗的图片,通过网络爬虫,算出特征描述值(假设有1000个特征值),再对特征向量进行聚类(如K-means聚类算法),从1000个聚类成几个特征,并且每个特征都是128维的,把聚类质心作为视觉词典。输入一张想要搜索的图片,用SIFT算法提取特征点,将几千个128维的特征向量进行归类,通过查找视觉词典,用视觉单词直方图表示图像,该模型便称为BOW模型(希望的结果是类内差距小,类间差距大的直方图)。


Bag of features:基础流程

 

  1. 特征提取
  2. 学习“视觉词典visual vocabulary” 
  3. 针对输入特征集,根据视觉词典进行量化 
  4. 把输入图像转化成视觉词典(visual words)的频率直方图 
  5. 构造特征到图像的倒排表,通过倒排表快速索引相关图像
  6. 根据索引结果进行直方图匹配

K-means聚类算法

最小化每个特征值X_{i}与其相对应聚类中心m_{k}之间的欧氏距离

算法流程:

  • 随机初始化K个聚类中心
  • 重复一下步骤直至算法收敛:
  1. 对应每个特征,根据距离关系赋值给某个中心/类别
  2. 对每个类别,根据其对应的特征集重新计算聚类中心

 

Bag of features:学习特征词典

聚类是实现visual vocabulary/codebook的关键:k-means算法获取的聚类中心作为codevector(视觉词典)

        视觉词典示例:

        

字典用于对输入图片的特征集进行量化:对于输入特征,量化的过程是将该特征映射到距离最近的codevector,并实现计数

Bag of features:图像检索

给定输入图像的BOW直方图,在数据库中查找 k 个最近邻的图像,对于图像分类问题,可以根据这k个近邻图像的分类标签, 投票获得分类结果:

参数设置: 

  • 视觉单词数量(K-means算法获取的聚类中心)一般为 K=3000~10000. 即图像整体描述的直方图维度为 3000~10000. 
  • 求解近邻的方法一般采用L2-范数:即 Euclidean 距离
  • 目前普适的视觉单词采用 Lowe 的SIFT特征描述子. 特征 点检测采用 DOG (Difference of Gaussians).

实验过程

数据集:pictures文件夹中分别有20张皮卡丘、11张可达鸭、10张小火龙、28张杰尼龟、31张胖丁的图片

1.生成sift特征集并保存模型:

保存词汇:在当前目录下生成vocabulary.pkl文件

代码:

# -*- 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('pictures/')
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('pictures/vocabulary.pkl', 'wb') as f:
    pickle.dump(voc, f)
print('vocabulary is:', voc.name, voc.nbr_words)

2.载入词汇,遍历图像并把特征投影到词汇上

在当前目录下生成文件 testlmAdd.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('pictures/')
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]

# load vocabulary
#载入词汇
with open('pictures/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)[: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('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('pictures/')
nbr_images = len(imlist)
#载入特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]

#载入词汇
with open('pictures/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 =29
nbr_results = 10

# 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[:20]) #常规查询
imagesearch.plot_results(src,res_geom[:20]) #重排后的结果

测试结果:

  • 皮卡丘:

分别用不同图片进行测试,查询结果:

两次实验的结果前四张都是皮卡丘,第五章不约而同出现了一张胖丁的图片,这张胖丁的图片可能和皮卡丘具有某些相似的特点(难道是耳朵吗?迷惑。。。)

  • 接下来换胖丁:

查询结果:

这次显示了前十幅图片的查找结果,十张结果中有五张是正确的。

  • 换杰尼龟:

查询结果:

十张结果中只有三张正确。

  • 换小火龙:

查询结果:

只有第一张和第四章是正确的。

实验分析:因为图库中小火龙只有十张图片,所以搜索结果不理想。相比之前图库中图片较多的皮卡丘、胖丁等的查询结果,说明增加图库中的图片,会丰富特征字典,进而搜索效果也更加理想。

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值