Bag of features:图像检索
目 录
构造不小于100张图片的数据集做SIFT特征提取匹配
1、SIFT特征提取 :提取训练集中所有图像的SIFT特征,设有M幅图像,共得到N个SIFT特征
2、构建视觉词汇表 对提取到的N个SIFT特征进行聚类,得到K个聚类中心,组成图像的视觉词汇表。
3、图像的视觉词向量表示,统计每幅图像中视觉词汇的出现的次数,得到图像的特征向量。在检索时,该特征向量就代表该幅图像。统计时,计算图像中提取到的SIFT特征点到各个视觉词(聚类中心)的距离,将其归类到聚类最近的视觉词中。
sift的原理之前已经说过,这里不再赘述,代码如下:
#SIFT算法
from PIL import Image
from pylab import *
import sys
from PCV.localdescriptors import sift
im1f = 'C:/Users/ace/Pictures/ims/17.jpg'
im2f = 'C:/Users/ace/Pictures/ims/18.jpg'
im1 = array(Image.open(im1f).convert('L'))
im2 = array(Image.open(im2f).convert('L'))
sift.process_image(im1f, 'out_sift_1.txt')
l1, d1 = sift.read_features_from_file('out_sift_1.txt')
figure()
gray()
subplot(121)
sift.plot_features(im1, l1, circle=False)
sift.process_image(im2f, 'out_sift_2.txt')
l2, d2 = sift.read_features_from_file('out_sift_2.txt')
subplot(122)
sift.plot_features(im2, l2, circle=False)
#matches = sift.match(d1, d2)
matches = sift.match_twosided(d1, d2)
print ('{} matches'.format(len(matches.nonzero()[0])))
figure()
gray()
sift.plot_matches(im1, im2, l1, l2, matches, show_below=True)
show()
当匹配点过多,系统会报错,这时候需要更多的图片:
Bag-of-features基础流程
1、 特征提取
2、 学习 “视觉词典(visual vocabulary)”
这一步的流程是通过K-means算法找到聚类中心:
K-means聚类算法实现visual vocabulary/codebook的关键
其中,聚类算法获得的聚类中心作为codevector(视觉单词&#x