Python计算机视觉-BoF图像检索

Bag of Words 词袋模型

在信息检索中,Bag of words model 假定对于一个文本,忽略其词序和语法,句法,将其仅仅看做是一个词的集合,或者说是词的一个组合,文本中每个词的出现都是独立的,不依赖于其他词是否出现,或者说当这篇文章的作者在任意一个位置选择一个词汇都不受前面句子的影响而独立选择,通过单词计数来构建文档直方图向量,从而建立文档索引。

Bag of Feature

Fei-fei Li 提出用BoW模型表达图像的方法,他们认为图像可以类比为文档(document), 图像中的单词(words)可以定义为一个图像块(image patch)的特征向量,那么图像的BoW模型即是 “图像中所有图像块的特征向量得到的直方图”。这就转变成了现有一幅图像的基础上对已有的码本(codebook)上进行投票的过程,投票结果与数据库里某图像投票结果越接近,则认为两幅图像的关联程度越高。

BoF大致流程

  1. 图像特征提取
    假设有N张图像, 第i张图像图像可由n(i)个image patch组成, 也即可以由n(i)个特征向量表达. 则总共能得到sum(n(i))个特征向量(即单词)。特征描述子可自由选取,要找到图像中的关键词,而这些关键词必须具备较高的区分度,因此我们采用SIFT描述子提取特征;
  2. 特征聚类生成码本
    如果单纯使用SIFT特征来描述图像,那每幅图像包含许多个特征点,每个特征点都有128维向量,计算相似度时数据量过于庞大,因此我们用聚类算法(例如采用k-means算法)对这些矢量数据进行聚类,然后用聚类中心代表BoW中的一个视觉词,将同一幅图像的SIFT矢量映射到视觉词序列生成码本,这样每一幅图像只用一个码本来描述,就大大缩小了计算量;
  3. 投票生成直方图
    对每张图片, 通过最近邻计算该图片的每个 “单词”应该属于码本中的 “哪一类”单词, 从而得到该图片对应于该码本的BoF表示,统计出最相似的向量出现的次数,最后得到这幅图片的直方图向量。

TF-IDF 词频-逆向文档频率

在BoW模型中,不同单词对文本检索的贡献有差异,一些跟主题不相关的词可能占据较大的比重,例如介词、代词(“这”、“和”、“是”),而这些词对主题的检索几乎没有作用。这种词我们称为停止词(Stop Word),表明在度量相关性时不考虑它们的频率。为排除停止词对索引结果的影响,可采用TF-IDF的策略,通常是一个单词的重要性与它在文档中出现次数成正比,与它在数据集中出现的频率成反比:

单词w在文档d中的词频 —— tfw,d = nw / ∑j nj ,(nw为单词出现次数)
逆向文档频率 —— idfw,d = log( |(D)|/ |{ d:w ∈ d }|) ,|(D)|是数据集D中文档数目,|{ d:w ∈ d }|则是数据集中包含单词w的文档数d。

tfidf 相乘可得矢量对应单词的权重,这个权重满足:

  1. 一个词对主题预测能力越强,权重越大;
  2. 停止词权重为 0;

开始创建词汇

在开始之前,我们需要配置好SIFT文件,将PCV代码文件放到要运行的代码的目录下;

然后开始提取特征,k-means聚类生成词汇:
这里k-means聚类时 voc.train 方法中可以自定义不同数量的聚类中心和子采样个数,针对不同数据集可进行调优

# -*- 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('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])

#生成词汇
voc = vocabulary.Vocabulary('ukbenchtest')
voc.train(featlist, 1000, 10)
#保存词汇
# saving vocabulary
with open('first1000/vocabulary.pkl', 'wb') as f:
    pickle.dump(voc, f)
print ('vocabulary is:', voc.name, voc.nbr_words)

在这里插入图片描述

图像索引:建立数据库并添加图像

此处使用了python中的pysqlite库,需要自己安装:
安装包下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl

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

# load vocabulary
#载入词汇
with open('first1000/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())

测试图片索引

因单词中不包含特征的位置信息,所以使用单应性对靠前的结果重排可以提高准确度:

# -*- 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('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 = 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]) #重排后的结果

常规查询以及重排结果显示:
在这里插入图片描述
Python计算机视觉-BoF图像检索_第1张图片
Python计算机视觉-BoF图像检索_第2张图片

建立演示程序及Web应用

需要安装 cherrypy ,可用 pip 直接安装:

pip install cherrypy

service.conf 配置文件
第一部分为IP地址和端口,第二部分为我们的图库的地址
因此第二部分中 tools.staticdir.root 要定位到文件夹绝对路径下:

[global]
server.socket_host = "127.0.0.1"
server.socket_port = 8080
server.thread_pool = 50
tools.sessions.on = True
[/]
tools.staticdir.root = ""
tools.staticdir.on = True
tools.staticdir.dir = ""

最后我们运行的时候会将我们设置的图库的地址和我们保存在数据库中的地址(first1000/xxx.jpg)连接起来,用于显示图片。

# -*- coding: utf-8 -*-
import cherrypy
import pickle
import urllib
import os
from numpy import *
#from PCV.tools.imtools import get_imlist
from PCV.imagesearch import imagesearch
import random

"""
This is the image search demo in Section 7.6.
"""


class SearchDemo:

    def __init__(self):
        # 载入图像列表
        self.path = 'first1000/'
        #self.path = 'D:/python_web/isoutu/first500/'
        self.imlist = [os.path.join(self.path,f) for f in os.listdir(self.path) if f.endswith('.jpg')]
        #self.imlist = get_imlist('./first500/')
        #self.imlist = get_imlist('E:/python/isoutu/first500/')
        self.nbr_images = len(self.imlist)
        print (self.imlist)
        print (self.nbr_images)
        self.ndx = list(range(self.nbr_images))
        print (self.ndx)

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

        # 显示搜索返回的图像数
        self.maxres = 10

        # header and footer html
        self.header = """
            
            
            Image search
            
            
            """
        self.footer = """
            
            
            """

    def index(self, query=None):
        self.src = imagesearch.Searcher('testImaAdd.db', self.voc)

        html = self.header
        html += """
            
Click an image to search. Random selection of images.

""" if query: # query the database and get top images #查询数据库,并获取前面的图像 res = self.src.query(query)[:self.maxres] for dist, ndx in res: imname = self.src.get_filename(ndx) html += "" html += ""+imname+"" print (imname+"################") html += "" # show random selection if no query # 如果没有查询图像则随机显示一些图像 else: random.shuffle(self.ndx) for i in self.ndx[:self.maxres]: imname = self.imlist[i] html += "" html += ""+imname+"" print (imname+"################") html += "" html += self.footer return html index.exposed = True #conf_path = os.path.dirname(os.path.abspath(__file__)) #conf_path = os.path.join(conf_path, "service.conf") #cherrypy.config.update(conf_path) #cherrypy.quickstart(SearchDemo()) cherrypy.quickstart(SearchDemo(), '/', config=os.path.join(os.path.dirname(__file__), 'service.conf'))

运行结果:
打开对应IP地址加端口就会显示我们搜索的图片
Python计算机视觉-BoF图像检索_第3张图片
Python计算机视觉-BoF图像检索_第4张图片
在这里插入图片描述

你可能感兴趣的:(计算机视觉)