基于BOF的图像检索

2023-11-09 00:50
文章标签 图像 检索 bof

本文主要是介绍基于BOF的图像检索,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

BOF

Bag of Feature (BOF)算法简介

BOF的本质是提出一种图像的特征表示方法。按照BOF算法的思想,首先我们要找到图像中的关键词,而且这些关键词必须具备较高的区分度。实际过程中,通常会采用SIFT特征。
有了特征之后,我们会将这些特征通过聚类算法得出很多聚类中心。这些聚类中心通常具有较高的代表性,比如,对于人脸来说,虽然不同人的眼睛、鼻子等特征都不尽相同,但它们往往具有共性,而这些聚类中心就代表了这类共性。我们将这些聚类中心组合在一起,形成一部字典。
对于图像中的每个「SIFT」特征,我们能够在字典中找到最相似的聚类中心,统计这些聚类中心出现的次数,可以得到一个向量表示(有些文章称之为直方图),如本文开篇的图片所示。这些向量就是所谓的Bag。这样,对于不同类别的图片,这个向量应该具有较大的区分度,基于此,我们可以训练出一些分类模型,并用其对图片进行分类。

BOF 算法过程

1、创建词汇
2、创建图像索引
3、在数据库中搜索图像

TF-IDF
对于直方图向量,我们引入 TF-IDF 权值
我们需要对每一个词给一个权重。而且这个权重必须满足以下两个条件:
1、一个词对主题预测能力越强,权重越大;
2、停止词权重为 0;

实验代码

创建数据库

from numpy import *
import pickle
import sqlite3
from functools import cmp_to_key
import operatorclass Indexer(object):def __init__(self, db, voc):""" Initialize with the name of the databaseand a vocabulary object. """self.con = sqlite3.connect(db)self.voc = vocdef __del__(self):self.con.close()def db_commit(self):self.con.commit()def get_id(self, imname):""" Get an entry id and add if not present. """cur = self.con.execute("select rowid from imlist where filename='%s'" % imname)res = cur.fetchone()if res == None:cur = self.con.execute("insert into imlist(filename) values ('%s')" % imname)return cur.lastrowidelse:return res[0]def is_indexed(self, imname):""" Returns True if imname has been indexed. """im = self.con.execute("select rowid from imlist where filename='%s'" % imname).fetchone()return im != Nonedef add_to_index(self, imname, descr):""" Take an image with feature descriptors,project on vocabulary and add to database. """if self.is_indexed(imname): returnprint('indexing', imname)# get the imidimid = self.get_id(imname)# get the wordsimwords = self.voc.project(descr)nbr_words = imwords.shape[0]# link each word to imagefor i in range(nbr_words):word = imwords[i]# wordid is the word number itselfself.con.execute("insert into imwords(imid,wordid,vocname) values (?,?,?)", (imid, word, self.voc.name))# store word histogram for image# use pickle to encode NumPy arrays as stringsself.con.execute("insert into imhistograms(imid,histogram,vocname) values (?,?,?)",(imid, pickle.dumps(imwords), self.voc.name))def create_tables(self):""" Create the database tables. """self.con.execute('create table imlist(filename)')self.con.execute('create table imwords(imid,wordid,vocname)')self.con.execute('create table imhistograms(imid,histogram,vocname)')self.con.execute('create index im_idx on imlist(filename)')self.con.execute('create index wordid_idx on imwords(wordid)')self.con.execute('create index imid_idx on imwords(imid)')self.con.execute('create index imidhist_idx on imhistograms(imid)')self.db_commit()class Searcher(object):def __init__(self, db, voc):""" Initialize with the name of the database. """self.con = sqlite3.connect(db)self.voc = vocdef __del__(self):self.con.close()def get_imhistogram(self, imname):""" Return the word histogram for an image. """im_id = self.con.execute("select rowid from imlist where filename='%s'" % imname).fetchone()s = self.con.execute("select histogram from imhistograms where rowid='%d'" % im_id).fetchone()# use pickle to decode NumPy arrays from stringreturn pickle.loads(s[0])def candidates_from_word(self, imword):""" Get list of images containing imword. """im_ids = self.con.execute("select distinct imid from imwords where wordid=%d" % imword).fetchall()return [i[0] for i in im_ids]def candidates_from_histogram(self, imwords):""" Get list of images with similar words. """# get the word idswords = imwords.nonzero()[0]# find candidatescandidates = []for word in words:c = self.candidates_from_word(word)candidates += c# take all unique words and reverse sort on occurrencetmp = [(w, candidates.count(w)) for w in set(candidates)]tmp.sort(key=cmp_to_key(lambda x, y: operator.gt(x[1], y[1])))tmp.reverse()# return sorted list, best matches firstreturn [w[0] for w in tmp]def query(self, imname):""" Find a list of matching images for imname. """h = self.get_imhistogram(imname)candidates = self.candidates_from_histogram(h)matchscores = []for imid in candidates:# get the namecand_name = self.con.execute("select filename from imlist where rowid=%d" % imid).fetchone()cand_h = self.get_imhistogram(cand_name)cand_dist = sqrt(sum(self.voc.idf * (h - cand_h) ** 2))matchscores.append((cand_dist, imid))# return a sorted list of distances and database idsmatchscores.sort()return matchscoresdef get_filename(self, imid):""" Return the filename for an image id. """s = self.con.execute("select filename from imlist where rowid='%d'" % imid).fetchone()return s[0]def tf_idf_dist(voc, v1, v2):v1 /= sum(v1)v2 /= sum(v2)return sqrt(sum(voc.idf * (v1 - v2) ** 2))def compute_ukbench_score(src, imlist):""" Returns the average number of correctimages on the top four results of queries. """nbr_images = len(imlist)pos = zeros((nbr_images, 4))# get first four results for each imagefor i in range(nbr_images):pos[i] = [w[1] - 1 for w in src.query(imlist[i])[:4]]# compute score and return averagescore = array([(pos[i] // 4) == (i // 4) for i in range(nbr_images)]) * 1.0return sum(score) / (nbr_images)# import PIL and pylab for plotting
from PIL import Image
from pylab import *def plot_results(src, res):""" Show images in result list 'res'. """figure()nbr_results = len(res)for i in range(nbr_results):imname = src.get_filename(res[i])subplot(1, nbr_results, i + 1)imshow(array(Image.open(imname)))axis('off')show()

搜索图像

# -*- 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('E:/test_pic/BOF/')
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]#载入词汇
f = open('E:/test_pic/BOF/vocabulary.pkl', 'rb')
voc = pickle.load(f)
f.close()src = imagesearch.Searcher('testImaAdd.db',voc)
locs,descr = sift.read_features_from_file(featlist[0])
iw = voc.project(descr)print ('ask using a histogram...')
print (src.candidates_from_histogram(iw)[:5])src = imagesearch.Searcher('testImaAdd.db',voc)
print ('try a query...')
print(src.query(imlist[0])[:5])nbr_results = 5
res = [w[1] for w in src.query(imlist[0])[:nbr_results]]
imagesearch.plot_results(src,res)

实验结果

输入图片:
在这里插入图片描述
得到结果:
在这里插入图片描述
结果分析
可以看出,得出的结果是有一定的相似度的。
不选去有文字的图片尽心检索应该会使结果更好一些。数据集中也最好不要出现文字图片,我的数据集是表情包,所以难免会有文字在其中。
在图像特征比较明显,或者数据集中图片相似的很多,则图像的匹配效果越好
BOF算法还有一个明显的不足,就是它完全没有考虑到特征之间的位置关系,而位置信息对于人理解图片来说,作用是很明显的。
而且在提取特征时不需要相关的 label 进行学习,因此是一种弱监督的学习方法。

这篇关于基于BOF的图像检索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/373337

相关文章

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

Verybot之OpenCV应用一:安装与图像采集测试

在Verybot上安装OpenCV是很简单的,只需要执行:         sudo apt-get update         sudo apt-get install libopencv-dev         sudo apt-get install python-opencv         下面就对安装好的OpenCV进行一下测试,编写一个通过USB摄像头采

【python计算机视觉编程——7.图像搜索】

python计算机视觉编程——7.图像搜索 7.图像搜索7.1 基于内容的图像检索(CBIR)从文本挖掘中获取灵感——矢量空间模型(BOW表示模型)7.2 视觉单词**思想****特征提取**: 创建词汇7.3 图像索引7.3.1 建立数据库7.3.2 添加图像 7.4 在数据库中搜索图像7.4.1 利用索引获取获选图像7.4.2 用一幅图像进行查询7.4.3 确定对比基准并绘制结果 7.

【python计算机视觉编程——8.图像内容分类】

python计算机视觉编程——8.图像内容分类 8.图像内容分类8.1 K邻近分类法(KNN)8.1.1 一个简单的二维示例8.1.2 用稠密SIFT作为图像特征8.1.3 图像分类:手势识别 8.2贝叶斯分类器用PCA降维 8.3 支持向量机8.3.2 再论手势识别 8.4 光学字符识别8.4.2 选取特征8.4.3 多类支持向量机8.4.4 提取单元格并识别字符8.4.5 图像校正

HalconDotNet中的图像特征与提取详解

文章目录 简介一、边缘特征提取二、角点特征提取三、区域特征提取四、纹理特征提取五、形状特征提取 简介   图像特征提取是图像处理中的一个重要步骤,用于从图像中提取有意义的特征,以便进行进一步的分析和处理。HalconDotNet提供了多种图像特征提取方法,每种方法都有其特定的应用场景和优缺点。 一、边缘特征提取   边缘特征提取是图像处理中最基本的特征提取方法之一,通过检

超越IP-Adapter!阿里提出UniPortrait,可通过文本定制生成高保真的单人或多人图像。

阿里提出UniPortrait,能根据用户提供的文本描述,快速生成既忠实于原图又能灵活调整的个性化人像,用户甚至可以通过简单的句子来描述多个不同的人物,而不需要一一指定每个人的位置。这种设计大大简化了用户的操作,提升了个性化生成的效率和效果。 UniPortrait以统一的方式定制单 ID 和多 ID 图像,提供高保真身份保存、广泛的面部可编辑性、自由格式的文本描述,并且无需预先确定的布局。

Winfrom中解决图像、文字模糊的方法

1.添加清单 2.将清单中的下面内容取消注释

使用亚马逊Bedrock的Stable Diffusion XL模型实现文本到图像生成:探索AI的无限创意

引言 什么是Amazon Bedrock? Amazon Bedrock是亚马逊云服务(AWS)推出的一项旗舰服务,旨在推动生成式人工智能(AI)在各行业的广泛应用。它的核心功能是提供由顶尖AI公司(如AI21 Labs、Anthropic、Cohere、Meta、Mistral AI、Stability AI以及亚马逊自身)开发的多种基础模型(Foundation Models,简称FMs)。

利用PL/SQL工具连接Oracle数据库的时候,报错:ORA-12638: 身份证明检索失败的解决办法

找到相对应的安装目录:比如:E:\oracle\product\10.2.0\client_1\NETWORK\ADMIN 在里面找到:SQLNET.AUTHENTICATION_SERVICES= (NTS) 将其更改为:SQLNET.AUTHENTICATION_SERVICES= (BEQ,NONE) 或者注释掉:#SQLNET.AUTHENTICATION_SERVICES= (N

Matplotlib图像读取和输出及jpg、png格式对比,及透明通道alpha设置

图像像素值 图像像素值一般size为3,也就是通道数,分别代表R,G,B,如果只有单一 一个值则表示灰度值,也就是说一张二维图片,当长和宽都为1080时,那么若是灰度图像,图像尺寸为(1080,1080,1)若是RGB图像则为(1080,1080,3), jpg、png图像格式 jpg图像的灰度值范围和RGB范围为[0,255],数值类型为uint8,也就是无符号整数 png图像的灰度值范