计算机视觉编程 BOF图像检索(Python)

您所在的位置:网站首页 cctv2今天的节目表 计算机视觉编程 BOF图像检索(Python)

计算机视觉编程 BOF图像检索(Python)

2023-08-21 01:02| 来源: 网络整理| 查看: 265

BOF图像检索 一、基本原理1.什么是图像检索2.基于文本的图像检索3.基于内容的图像检索4.BOF(Bag Of Feature)4.1 原理4.2 BOF算法的流程 二、实验数据三、实验代码四、运行结果五、实验总结

一、基本原理 1.什么是图像检索

从20世纪70年代开始,有关图像检索的研究就已开始,当时主要是基于文本的图像检索技术(Text-based Image Retrieval,简称TBIR),利用文本描述的方式描述图像的特征,如绘画作品的作者、年代、流派、尺寸等。到90年代以后,出现了对图像的内容语义,如图像的颜色、纹理、布局等进行分析和检索的图像检索技术,即基于内容的图像检索(Content-based Image Retrieval,简称CBIR)技术。

2.基于文本的图像检索

基于文本的图像检索主要是利用文本标注的方式为图像添加关键词,这种方式需要人为给每一张图像标注,非常耗费人工。

3.基于内容的图像检索

基于内容的图像检索免去了人工标注。这种检索方式首先需要准备一个数据集(dataset)作为训练,通过某种算法提取数据集中每张图像的特征(SIFT特征)向量,然后将这些特征存储起来,组成一个数据库,当需要搜索某张图片的时候,就输入这张图片,然后提取输入图片的特征,用某种匹配准则将提取的输入图片的特征和数据库中的特征进行比较,最后从数据库中按照相似度从大到小输出相似的图片。

4.BOF(Bag Of Feature) 4.1 原理

Bag of features(Bof)一种是用于图像和视频检索的算法。此算法对于不同角度,光照的图像,基本都能在图像库中正确检索。检索就要进行比对。两幅不同的图像如何比对,比对什么,这就需要提炼出每幅图像中精练的东西出来进行比较。正如超市中的条形码,就能很好的反映出一件商品的所有特征。因此概括的来说,bof就是生成每幅图像的“条形码”来进行检索。

4.2 BOF算法的流程

1、准备一个训练图片集,提取这些图片的SIFT特征

2、对这些特征进行K-Means聚类(将相似的特征点用K-Means聚类归到一个类别中),创建视觉词典(所有训练图像所提取的所有特征向量的集合)。

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

4、输入测试图片,提取测试图片的SIFT特征,把输入图像转成视觉单词的频率直方图。

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

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

二、实验数据

在这里插入图片描述

三、实验代码

1、训练图像集生成词汇字典(图像的 SIFT特征点已提取)。

# -*- 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('C:/Users/VIVACIOUS/PycharmProjects/JiSuanJiShiJue/shiyan6/image/') 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('C:/Users/VIVACIOUS/PycharmProjects/JiSuanJiShiJue/shiyan6/image/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('C:/Users/VIVACIOUS/PycharmProjects/JiSuanJiShiJue/shiyan6/image/') nbr_images = len(imlist) #获取特征列表 featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)] # load vocabulary #载入词汇 with open('C:/Users/VIVACIOUS/PycharmProjects/JiSuanJiShiJue/shiyan6/image/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())

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('E:/Python37_course/test7/first1000/') imlist = get_imlist('C:/Users/VIVACIOUS/PycharmProjects/JiSuanJiShiJue/shiyan6/image/') nbr_images = len(imlist) #载入特征列表 featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)] #载入词汇 with open('C:/Users/VIVACIOUS/PycharmProjects/JiSuanJiShiJue/shiyan6/image/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 = 14 # 数据集大小 # 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[:6]) #常规查询 imagesearch.plot_results(src,res_geom[:6]) #重排后的结果 四、运行结果

1、输入检索图像 在这里插入图片描述

2、常规查询结果 在这里插入图片描述 3、重排后的结果 在这里插入图片描述

五、实验总结

从结果不难看到,检索出来的图片和输入图片的形状非常相似,证明视觉词汇可能主要是从这个方面进行匹配。不过,检索出的图像还存在一定的差异,提高检索准确率是我们需要解决的问题。 此外,我还了解了图像检索的一些问题: 使用k-means聚类,除了其K和初始聚类中心选择的问题外,对于大量数据,输入矩阵的巨大将使得内存溢出及效率低下。 字典大小的选择也是问题,字典过大,单词缺乏一般性,对噪声敏感,计算量大,关键是图象投影后的维数高;字典太小,单词区分性能差,对相似的目标特征无法表示。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3