图像检索

一、Bag of features算法

1.1Bag of features算法原理

1.2算法流程

二、基于Bag of features算法的图像检索

2.1代码

2.2结果截图

2.3小结

三、图像源

四、实验总结

五、遇到的问题以及解决方法

一、Bag of features算法

1.1Bag of features算法原理

此算法的思想是在我们先做一个数据集,然后找到图像中的关键词,这些关键词必须具备较高的区分度,最主要的操作就是提取sift特征,然后对这些特征点进行聚类算法,然后得到聚类中心,聚类中心就具有很高的代表性,这些聚类中心形成字典,然后自取一张图片,进行sift特征提取,就可以在字典里找到最相似的聚类中心,统计这些聚类中心出现的次数,然后就直方图表示出来,对于不同类别的图片,就可以训练处一些分类模型,然后就可以进行图片分类。

1.2算法流程

1.2.1收集数据集

1.2.2提取sift特征

1.2.3根据sift特征提取结果,进行聚类,得到一个字典

1.2.4根据字典将图片表示成向量(直方图);

1.2.5训练分类器或者用 KNN 进行检索

提取特征:

我们为了是图片具有较高的分辨度,我们使用sift特征提取,保证旋转不变性和尺度不变性,每个特征点都是128维的向量,将会提取很多的特征点

得到字典:

我们再次之前提取了很多的特征向量,然后就对这些特征向量进行k-means聚类,k值根据实际情况而定。聚类完成后,我们就得到了这 k 个向量组成的字典,这 k 个向量有一个通用的表达,叫 visual word。

直方图表示:

聚类之后,我们匹配图片的「SIFT」向量与字典中的 visual word,统计出最相似的向量出现的次数,最后得到这幅图片的直方图向量。

训练分类器:

当我们得到每幅图片的直方图向量后,剩下的这一步跟以往的步骤是一样的。无非是根据数据库图片的向量以及图片的标签,训练分类器模型。然后对需要预测的图片,我们仍然按照上述方法,提取「SIFT」特征,再根据字典量化直方图向量,用分类器模型对直方图向量进行分类。当然,也可以直接根据 KNN 算法对直方图向量做相似性判断。

二、图像源

三、基于Bag of features算法的图像检索

3.1读取图片,提取特征,建立字典

代码

1 #-*- coding: utf-8 -*-

2 importpickle3 from PCV.imagesearch importvocabulary4 from PCV.tools.imtools importget_imlist5 from PCV.localdescriptors importsift6

7 #获取图像列表

8 imlist = get_imlist('D:/new/feng/')9 nbr_images =len(imlist)10 #获取特征列表

11 featlist = [imlist[i][:-3]+'sift' for i inrange(nbr_images)]12

13 #提取文件夹下图像的sift特征

14 for i inrange(nbr_images):15 sift.process_image(imlist[i], featlist[i])16

17 #生成词汇

18 voc = vocabulary.Vocabulary('ukbenchtest')19 voc.train(featlist, 100, 10)20 #保存词汇

21 #saving vocabulary

22 with open('D:/new/feng//vocabulary.pkl', 'wb') as f:23 pickle.dump(voc, f)24 print ('vocabulary is:', voc.name, voc.nbr_words)

3.1.2结果截图

sift文件:

PKL文件:

3.2遍历图像,然后将向量特征投影到字典里并提交给数据库

代码

1 importpickle2 from PCV.imagesearch importimagesearch3 from PCV.localdescriptors importsift4 from sqlite3 importdbapi2 as sqlite5 from PCV.tools.imtools importget_imlist6

7 #获取图像列表

8 imlist = get_imlist('D:/new/feng/')9 nbr_images =len(imlist)10 #获取特征列表

11 featlist = [imlist[i][:-3]+'sift' for i inrange(nbr_images)]12

13 #载入词汇

14 with open('D:/new/feng//vocabulary.pkl', 'rb') as f:15 voc =pickle.load(f)16 #创建索引

17 indx = imagesearch.Indexer('testImaAdd.db',voc)18 indx.create_tables()19

20 #遍历所有的图像,并将它们的特征投影到词汇上

21 for i in range(nbr_images)[:1000]:22 locs,descr =sift.read_features_from_file(featlist[i])23 indx.add_to_index(imlist[i],descr)24

25 #提交到数据库

26 indx.db_commit()27 con = sqlite.connect('testImaAdd.db')28 print(con.execute('select count (filename) from imlist').fetchone())29 print(con.execute('select * from imlist').fetchone())

实验结果截图:

数据库

4、进行查询测试

代码

1 #-*- coding: utf-8 -*-

2 importpickle3 from PCV.localdescriptors importsift4 from PCV.imagesearch importimagesearch5 from PCV.geometry importhomography6 from PCV.tools.imtools importget_imlist7

8 #载入图像列表

9 imlist = get_imlist('D:/new/feng/')10 nbr_images =len(imlist)11 #载入特征列表

12 featlist = [imlist[i][:-3] + 'sift' for i inrange(nbr_images)]13

14 #载入词汇

15 with open('D:/new/feng/vocabulary.pkl', 'rb') as f:16 voc =pickle.load(f)17

18 src = imagesearch.Searcher('testImaAdd.db', voc)19

20 #查询图像索引和查询返回的图像数

21 q_ind = 5

22 nbr_results = 20

23

24 #常规查询(按欧式距离对结果排序)

25 res_reg = [w[1] for w insrc.query(imlist[q_ind])[:nbr_results]]26 print('top matches (regular):', res_reg)27

28 #载入查询图像特征

29 q_locs, q_descr =sift.read_features_from_file(featlist[q_ind])30 fp = homography.make_homog(q_locs[:, :2].T)31

32 #用单应性进行拟合建立RANSAC模型

33 model =homography.RansacModel()34 rank ={}35

36 #载入候选图像的特征

37 for ndx in res_reg[1:]:38 locs, descr = sift.read_features_from_file(featlist[ndx])

结果截图:

四、实验总结

sift特征提取具有旋转不变性和尺寸不变性,还有数据源的像素不能过大,否则运行速度会很慢,

五、遇到的问题以及解决方法

我用了这个代码 ,还是会报错

pip install pysqlite

然后我复制了报错的最后一样在cmd中运行

再次输入上面的代码,依然出错,后面我发现我少加了一个3

加了3之后,还要再imagesearch.py中加入

from numpy import *

import pickle

import sqlite3

from functools import cmp_to_key

import operator

class Indexer(object):

def __init__(self,db,voc):

""" Initialize with the name of the database

and a vocabulary object. """

self.con = sqlite3.connect(db)

self.voc = voc

def __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.lastrowid

else:

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 != None

def add_to_index(self,imname,descr):

""" Take an image with feature descriptors,

project on vocabulary and add to database. """

if self.is_indexed(imname): return

print ('indexing', imname)

# get the imid

imid = self.get_id(imname)

# get the words

imwords = self.voc.project(descr)

nbr_words = imwords.shape[0]

# link each word to image

for i in range(nbr_words):

word = imwords[i]

# wordid is the word number itself

self.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 strings

self.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()

第二个报错是

我看了一个学姐的是加入一些代码,但是仍然报同样的错误,这个问题还没有解决,所以没有截图,实验总结是分析不出来

图像检索代码python_python-图像检索相关推荐

  1. 树和二叉树的转换代码python_python数据结构之二叉树的统计与转换实例

    一.获取二叉树的深度就是二叉树最后的层次,如下图: 实现代码: def getheight(self): ''' 获取二叉树深度 ''' return self.__get_tree_height(s ...

  2. 程序员关机代码python_Python实现远程开关机【高薪必备技术】

    用 Python 关机你肯定听过或者实践过,那么用 Python 开机呢?这是一个神奇的方法,教你如何用 Python 来开机. PS注意:很多人学Python过程中会遇到各种烦恼问题,没有人解答容易 ...

  3. 人机猜拳代码python_python实现人机猜拳小游戏

    今天的这篇文章呢是对人机猜拳小游戏--石头剪刀布的一个描述以及代码展现 石头剪刀布游戏代码的简介:关于石头剪刀布这个小游戏,大致得到思路就是,玩家出一个手势,然后电脑再随机出一个手势,最后再判断是玩家 ...

  4. 微信抢红包代码 python_python实现红包裂变算法

    本文实例介绍了python实现红包裂变算法,分享给大家供大家参考,具体内容如下 Python语言库函数 安装:pip install redpackets使用: import redpackets r ...

  5. cython加密代码python_python通过cython加密代码

    #coding=utf-8 import getopt import os, sys import zipfile from crypto.cipher import aes import rando ...

  6. 父爱动画代码python_python表白实现代码(可视化与动画版)

    原博文 2019-08-08 09:53 − python表白实现代码(可视化与动画版)如何优雅而又高大上地对自己的心爱女神表白了? ? ? 试试python表白的实现方式吧,是动画版的哦,保证可以如 ...

  7. 人机猜拳代码python_python 实现人和电脑猜拳的示例代码

    完成人机猜拳互动游戏的开发,用户通过控制台输入实现出拳,电脑通过程序中的随机数实现出拳,每一局结束后都要输出结果.当用户输入n时停止游戏,并输出总结果. import random all = ['石 ...

  8. 人脸检测算法代码python_Python有多强大!20行代码实现人脸检测与识别

    题记:大二的时候发现人生苦短,所以信了拍神,开始学Python.学了大半年之后成功转行做前端了.来写个教程帮助大家入门Python. Python优点 Python是一种简单易学.功能强大的编程语言, ...

  9. 时钟壁纸代码python_Python实现系统桌面时钟 | 学步园

    用Python + PyQT写的一个系统桌面时钟,刚学习Python,写的比较简陋,但是基本的功能还可以. 功能: ①窗体在应用程序最上层,不用但是打开其他应用后看不到时间 ②左键双击全屏,可以做小屏 ...

  10. 电脑代码python_python发 代码分享 -电脑资料

    复制代码代码如下: import urllib.parse,os.path,time,sys,re,urllib.request from http.client import HTTPSConnec ...

最新文章

  1. 理解并取证:以太通道的动态协商机制的工作原理
  2. 28名公交驾乘人员昨食物中毒
  3. HTML DOCTYPE 标签 -- DTDs 文档类型声明
  4. LeetCode(109):有序链表转换二叉搜索树
  5. Synchronized 和 Lock 区别
  6. STM32之串口原理
  7. 2021-2025年中国点状插头装置行业市场供需与战略研究报告
  8. go bson转换成json_如何快速提升 Go 程序性能?
  9. DIV 内滚动条 样式的写法
  10. 粗读《构建之法》后的问题
  11. 电信华为 HG526 破解 - 简易启用USB恢复
  12. mBlock机器人组装教程_mbot机器人如何搭建?mBot机器人套件快速搭建指南
  13. python车牌识别_Python+Tensorflow+CNN实现车牌识别的示例代码
  14. HP大中华区总裁孙振耀退休感言
  15. 计算机蓝屏无法启动代码50,win7系统出现蓝屏代码0x0000050怎么解决
  16. EasyRecovery15最新好用的电脑免费数据恢复软件
  17. ZCMU--1930: 帽子戏法(C语言)
  18. 【CodeForces 1255E1 --- Send Boxes to Alice [Easy Version]】
  19. 故障树算法JAVA实现
  20. 二次剩余问题x的求解及代码实现(python)

热门文章

  1. 圆通快递单css样式
  2. HTML——1.Sublime快捷键、HTML常用标签
  3. Java毕业设计-小区物业管理系统
  4. 商业分析的50个网站和分析方法
  5. mac php开发集成环境,MAC OS X下php集成开发环境mamp
  6. 智能客服、聊天机器人的应用和架构、算法分享和介绍
  7. html5常用的小控件,收藏了
  8. 创新检查技术,赋能保密监管 ,您需要一款这样的数据库内容保密检查系统!
  9. k均值聚类算法考试例题_k均值算法(k均值聚类算法计算题)
  10. 阿里、京东、拼多多电商三巨头财报大比拼:拼多多用户数上拼了 京东营收超过阿里...