python SIFT特征匹配

  • SIFT(尺度不变特征变换)
    • 兴趣点
    • 描述子
    • 检测兴趣点
    • 匹配描述子
  • 匹配地理标记图像
    • 用局部描述子进行匹配
    • 可视化连接的图像
    • 实验代码
    • 实验结果
    • 结果分析

SIFT(尺度不变特征变换)

SIFT(Scale-Invariant Feature Transform,尺度不变特征变换)是过去十年中最成功的图像局部描述子之一。SIFT 特征包括兴趣点检测器和描述子。SIFT 描述子具有非常强的稳健性,这在很大程度上也是 SIFT 特征能够成功和流行的主要原因。自从 SIFT 特征的出现,许多其他本质上使用相同描述子的方法也相继出现。现在,SIFT 描述符经常和许多不同的兴趣点检测器相结合使用(有些情况下是区域检测器),有时甚至在整幅图像上密集地使用。SIFT 特征对于尺度、旋转和亮度都具有不变性,因此,它可以用于三维视角和噪声的可靠匹配。

兴趣点

SIFT 特征使用高斯差分函数来定位兴趣点:

其中,Gσ 是二维高斯核,Iσ 是使用 Gσ 模糊的灰度图像,κ 是决定相差尺度的常数。兴趣点是在图像位置和尺度变化下 D(x,σ) 的最大值和最小值点。这些候选位置点通过滤波去除不稳定点。基于一些准则,比如认为低对比度和位于边上的点不是兴趣点,我们可以去除一些候选兴趣点。

描述子

上面讨论的兴趣点(关键点)位置描述子给出了兴趣点的位置和尺度信息。为了实现旋转不变性,基于每个点周围图像梯度的方向和大小,SIFT 描述子又引入了参考方向。SIFT 描述子使用主方向描述参考方向。主方向使用方向直方图(以大小为权重)来度量。

下面我们基于位置、尺度和方向信息来计算描述子。为了对图像亮度具有稳健性,SIFT 描述子使用图像梯度(之前 Harris 描述子使用图像亮度信息计算归一化互相关矩阵)。SIFT 描述子在每个像素点附近选取子区域网格,在每个子区域内计算图像梯度方向直方图。每个子区域的直方图拼接起来组成描述子向量。SIFT 描述子的标准设置使用 4×4 的子区域,每个子区域使用 8 个小区间的方向直方图,会产生共128 个小区间的直方图(4×4×8=128)

检测兴趣点

我们使用开源工具包 VLFeat 提供的二进制文件来计算图像的 SIFT 特征 。VLFeat 工具包可以从 http://www.vlfeat.org/ 下载,二进制文件可以在所有主要的平台上运行。VLFeat 库是用 C 语言来写的,但是我们可以使用该库提供的命令行接口。

# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.localdescriptors import sift
from PCV.localdescriptors import harris# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)imname = '2.jpg'
im = array(Image.open(imname).convert('L'))
sift.process_image(imname, '2.sift')
l1, d1 = sift.read_features_from_file('2.sift')figure()
gray()
subplot(131)
sift.plot_features(im, l1, circle=False)
title(u'SIFT特征',fontproperties=font)
subplot(132)
sift.plot_features(im, l1, circle=True)
title(u'用圆圈表示SIFT特征尺度',fontproperties=font)# 检测harris角点
harrisim = harris.compute_harris_response(im)subplot(133)
filtered_coords = harris.get_harris_points(harrisim, 6, 0.1)
imshow(im)
plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
axis('off')
title(u'Harris角点',fontproperties=font)show()

为了比较 Harris 角点和 SIFT 特征的不同,上图显示的是同一幅图像的Harris 角点。你可以看到,两个算法所选择特征点的位置不同。

匹配描述子

对于将一幅图像中的特征匹配到另一幅图像的特征,一种稳健的准则(同样是由Lowe 提出的)是使用这两个特征距离和两个最匹配特征距离的比率。相比于图像中的其他特征,该准则保证能够找到足够相似的唯一特征。使用该方法可以使错误的匹配数降低。

from PIL import Image
from pylab import *
import sys
from PCV.localdescriptors import siftif len(sys.argv) >= 3:im1f, im2f = sys.argv[1], sys.argv[2]
else:im1f = 'D:/image/sift1.jpg'im2f = 'D:/image/sift2.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_twosided(d1, d2)
print('{} matches'.format(len(matches.nonzero()[0])))figure()
gray()
sift.plot_matches(im1, im2, l1, l2, matches, show_below=True)
show()


匹配地理标记图像

使用局部描述子来匹配带有地理标记的图像。

用局部描述子进行匹配

对图像提取局部描述子。在这种情况下,我们将使用前面部分讲述的 SIFT 特征描述子。我们假设已经对这些图像使用 SIFT 特征提取代码进行了处理,并且将特征保存在和图像同名(但文件名后缀是 .sift,而不是 .jpg)的文件中。假设 imlist 和 featlist 列表中包含这些文件名。我们可以对所有组合图像对进行逐个匹配

# -*- coding: utf-8 -*-
from pylab import *
from PIL import Image
from PCV.localdescriptors import sift
from PCV.tools import imtools
import pydot""" This is the example graph illustration of matching images from Figure 2-10.
To download the images, see ch2_download_panoramio.py."""download_path = "D:/image"  # set this to the path where you downloaded the panoramio images
path = "D:/image/"  # path to save thumbnails (pydot needs the full system path)# list of downloaded filenames
imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)# extract features
featlist = [imname[:-3] + 'sift' for imname in imlist]
for i, imname in enumerate(imlist):sift.process_image(imname, featlist[i])matchscores = zeros((nbr_images, nbr_images))for i in range(nbr_images):for j in range(i, nbr_images):  # only compute upper triangleprint 'comparing ', imlist[i], imlist[j]l1, d1 = sift.read_features_from_file(featlist[i])l2, d2 = sift.read_features_from_file(featlist[j])matches = sift.match_twosided(d1, d2)nbr_matches = sum(matches > 0)print 'number of matches = ', nbr_matchesmatchscores[i, j] = nbr_matches
print "The match scores is: \n", matchscores# copy values
for i in range(nbr_images):for j in range(i + 1, nbr_images):  # no need to copy diagonalmatchscores[j, i] = matchscores[i, j]

我们将每对图像间的匹配特征数保存在 matchscores 数组中。因为该“距离度量”是对称的,所以我们可以不在代码的最后部分复制数值,来将 matchscores 矩阵填充完整;填充完整后的 matchscores 矩阵只是看起来更好。使用该 matchscores 矩阵作为图像间简单的距离度量方式(具有相似内容的图像间拥有更多的匹配特征数),下面我们可以使用相似的视觉内容来将这些图像连接起来。

可视化连接的图像

我们首先通过图像间是否具有匹配的局部描述子来定义图像间的连接,然后可视化这些连接情况。为了完成可视化,我们可以在图中显示这些图像,图的边代表连接。我们将会使用 pydot 工具包(http://code.google.com/p/pydot/),该工具包是功能强大的 GraphViz 图形库的 Python 接口。Pydot 使用 Pyparsing(http://pyparsing.wiki spaces.com/)和 GraphViz(http://www.graphviz.org/)。

import pydotg = pydot.Dot(graph_type='graph')g.add_node(pydot.Node(str(0), fontcolor='transparent'))
for i in range(5):g.add_node(pydot.Node(str(i + 1)))g.add_edge(pydot.Edge(str(0), str(i + 1)))for j in range(5):g.add_node(pydot.Node(str(j + 1) + '0' + str(i + 1)))g.add_edge(pydot.Edge(str(j + 1) + '0' + str(i + 1), str(j + 1)))
g.write_png('../images/ch02/ch02_fig2-9_graph.png', prog='neato')

实验代码

我们接下来继续探讨地理标记图像处理的例子。为了创建显示可能图像组的图,如果匹配的数目高于一个阈值,我们使用边来连接相应的图像节点。为了得到图中的图像,需要使用图像的全路径(在下面例子中,使用 path 变量表示)。下面是具体实现代码:

# -*- coding: utf-8 -*-
from pylab import *
from PIL import Image
from PCV.localdescriptors import sift
from PCV.tools import imtools
import pydot""" This is the example graph illustration of matching images from Figure 2-10.
To download the images, see ch2_download_panoramio.py."""#download_path = "panoimages"  # set this to the path where you downloaded the panoramio images
#path = "/FULLPATH/panoimages/"  # path to save thumbnails (pydot needs the full system path)download_path = "D:/image"  # set this to the path where you downloaded the panoramio images
path = "D:/image"  # path to save thumbnails (pydot needs the full system path)# list of downloaded filenames
imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)# extract features
featlist = [imname[:-3] + 'sift' for imname in imlist]
for i, imname in enumerate(imlist):sift.process_image(imname, featlist[i])matchscores = zeros((nbr_images, nbr_images))for i in range(nbr_images):for j in range(i, nbr_images):  # only compute upper triangleprint('comparing ', imlist[i], imlist[j])l1, d1 = sift.read_features_from_file(featlist[i])l2, d2 = sift.read_features_from_file(featlist[j])matches = sift.match_twosided(d1, d2)nbr_matches = sum(matches > 0)print('number of matches = ', nbr_matches)matchscores[i, j] = nbr_matches
print("The match scores is: \n", matchscores)# copy values
for i in range(nbr_images):for j in range(i + 1, nbr_images):  # no need to copy diagonalmatchscores[j, i] = matchscores[i, j]#可视化threshold = 2  # min number of matches needed to create linkg = pydot.Dot(graph_type='graph')  # don't want the default directed graphfor i in range(nbr_images):for j in range(i + 1, nbr_images):if matchscores[i, j] > threshold:# first image in pairim = Image.open(imlist[i])im.thumbnail((100, 100))filename = path + str(i) + '.png'im.save(filename)  # need temporary files of the right sizeg.add_node(pydot.Node(str(i), fontcolor='transparent', shape='rectangle', image=filename))# second image in pairim = Image.open(imlist[j])im.thumbnail((100, 100))filename = path + str(j) + '.png'im.save(filename)  # need temporary files of the right sizeg.add_node(pydot.Node(str(j), fontcolor='transparent', shape='rectangle', image=filename))g.add_edge(pydot.Edge(str(i), str(j)))
g.write_png('jmu.png')

实验结果

结果分析


一共设置了7张图片,结果只有6张,有一张图片没有显示出来。可能是因为在代码中设置了阈值为2

当匹配到的特征点小于2时就不会显示图片。这张图可能是因为光线和角度变化太大,导致匹配到的特征点为0。
整体来说,SIFT特征匹配效果不错,可能因为图片不够多,没有出现匹配错误的情况。

python SIFT特征匹配相关推荐

  1. 图片SIFT特征匹配处理

    1.SIFT特征原理描述 SIFT的全称是Scale Invariant Feature Transform,由加拿大教授David G.Lowe提出的.SIFT特征不只具有尺度不变性,即使改变旋转角 ...

  2. 利用RANSAC算法筛选SIFT特征匹配

    关于RANSAC算法的基本思想,可从网上搜索找到,这里只是RANSAC用于SIFT特征匹配筛选时的一些说明. RANSAC算法在SIFT特征筛选中的主要流程是: (1) 从样本集中随机抽选一个RANS ...

  3. matlab怎么匹配特征参数,sift特征匹配matlab

    首先对彩色壁画图像提取 SIFT 特征 点与特征向量,然后对每个特征点提取 HSI 彩色特征,最后按定义的相似性度 量公式计算两个特征点之间的距离,确定二者是否匹配.... 通过计算机中 的 Matl ...

  4. (三)计算机视觉 --SIFT特征匹配、地理标记图像匹配及RANSAC图像拼接

    目录 一.sift特征检测概述 1.1特征点 1.2sift特征检测 二.sift特征提取与匹配 2.1特征提取并展示 2.2对两张图片进行特征匹配计算 2.3给定一张图片,输出与其匹配最多的三张图片 ...

  5. ubuntu使用python opencv_Ubuntu中“利用Opencv + python进行特征匹配”的环境搭建

    2.安装Python插件:点击Vscode左侧第五个方框对应的功能,搜索Python并安装 3.安装pip (以下步骤均在终端Terminal中执行) sudo apt install pytho-p ...

  6. sift特征匹配源码matlab,SIFT特征匹配 MATLAB 实现

    [实例简介] matlab实现的SIFT特征提取的全代码,可运行 可测试 很不错的sift原代码 [实例截图] [核心代码] sift的matlab代码,是最新的,效果不错,其中有很多详细说明,可以试 ...

  7. Matlab实现 sift 特征匹配(代码源自网络)

    源码下载: 链接:https://pan.baidu.com/s/191COIwM515XP1rEDEP5H1Q 提取码:owqt 注:解压后将siftWin32放至matlab的bin文件夹下. 脚 ...

  8. Python计算机视觉 sift和Harris特征匹配处理对比

    一.SIFT(尺度不变特征变换)原理分析 在过去的十年间,最成功的图像局部描述子之一是尺度不变特征变换(SIFT),它是由David Lowe发明的.SIFT是用于图像处理领域的一种描述,SIFT特征 ...

  9. SIFT特征点匹配中KD-tree与Ransac算法的使用

    转自:http://blog.csdn.net/ijuliet/article/details/4471311 Step1:BBF算法,在KD-tree上找KNN.第一步做匹配咯~ 1.什么是KD-t ...

最新文章

  1. 深度学习中tensorflow框架的学习
  2. arcgis python脚本筛选与线共边的面_ArcGis Python脚本——遍历输出面或折线要素的折点坐标...
  3. python opengl 截图_初试PyOpenGL二 (Python+OpenGL)基本地形生成与高度检测
  4. 每天进步一点点——Linux系统时间来处理
  5. Tesla对德国政府的审批流程表示受够了
  6. php中创建函数的正确方法,如何在PHP中创建一个函数
  7. KVM Virtio: An I/O virtualization framework for Linux(Linux虚拟IO框架)
  8. mtk2502和nrf52832哪个好_蓝牙芯片NRF51822与NRF52832的性能对比
  9. 应用ruby打造个性化的有道单词本 (二)
  10. 红米7android9miui11,红米Note7Pro MIUI11安卓9 解账户锁 可登小米账号 永不反锁 完美ROOT 解锁包...
  11. ESP8266-Arduino编程实例-BME280环境传感器驱动
  12. Qt三方库开发技术:二维码生成、识别以及条码识别
  13. geforce experience出现错误尝试重启PC
  14. 【网络安全】信息收集 CDN绕过方法
  15. 计算机机房一般在几楼,21层的楼房设备层一般在几楼
  16. ​从微信后端仓库发展史谈谈单仓和多仓
  17. 电气火灾监控系统技术分析
  18. 白话理解java多线程之setDaemon()方法
  19. 【定位不准的烦心事系列】第1篇:谈谈卫星定位的位置干扰
  20. 了解“新基建”、读罢IDC报告后,还请查收来自浪潮的硬核实力!

热门文章

  1. 511遇见易语言API模块视进入许可证(EnterCriticalSection)
  2. Discrete VS Continuous Control
  3. T58:序列化二叉树(Java)
  4. 收藏:优秀的个人博客
  5. Unsupervised Night Image Enhancement: When Layer Decomposition Meets Light-Effects Suppression论文阅读笔记
  6. 双对数坐标(log-log)下“斜率”“幅值”等概念对应到线性坐标下的实际含义
  7. freeswitch自动增加号码
  8. LinearLayout(线性布局)的基本使用
  9. 阿伦:NBA应重评50大巨星 美记:热刺必数人入围
  10. STM32F407霸天虎FreeRTOS学习笔记——移植FreeRTOS到开发板上