SIFT特征提取与检索

主宰稳场 提交于 2020-03-09 04:41:15

1.sift简介

SIFT,即尺度不变特征变换(Scale-invariant feature transform,SIFT),是用于图像处理领域的一种描述。这种描述具有尺度不变性,可在图像中检测出关键点,是一种局部特征描述子。
SIFT特征是基于物体上的一些局部外观的兴趣点而与影像的大小和旋转无关。对于光线、噪声、微视角改变的容忍度也相当高。基于这些特性,它们是高度显著而且相对容易撷取,在母数庞大的特征数据库中,很容易辨识物体而且鲜有误认。使用SIFT特征描述对于部分物体遮蔽的侦测率也相当高,甚至只需要3个以上的SIFT物体特征就足以计算出位置与方位。在现今的电脑硬件速度下和小型的特征数据库条件下,辨识速度可接近即时运算。SIFT特征的信息量大,适合在海量数据库中快速准确匹配。

1.1兴趣点

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

1.3描述子

为了对图像亮度具有稳健性,SIFT 描述子使用图像梯度。 SIFT 描述子在每个像素点附近选取子区域网格,在每个子区域内计算图像梯度方向直方图。每个子区域的直方图拼接起来组成描述子向量。 SIFT 描述子的标准设置使用 4× 4 的子区域,每个子区域使用 8 个小区间的方向直方图,会产生共128 个小区间的直方图(4× 4× 8=128)。

2.实验代码

sift特征点

# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.localdescriptors import sift

# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

imname = 'D:/photo/1_1.jpg'
im = array(Image.open(imname).convert('L'))
sift.process_image(imname, 'empire.sift')
l1, d1 = sift.read_features_from_file('empire.sift')

figure()
gray()
subplot(121)
sift.plot_features(im, l1, circle=False)
title(u'SIFT特征',fontproperties=font)
subplot(122)
sift.plot_features(im, l1, circle=True)
title(u'用圆圈表示SIFT特征尺度',fontproperties=font)

show()

SIFT特征匹配

from PIL import Image
from pylab import *
import sys
from PCV.localdescriptors import sift
 
 
if len(sys.argv) >= 3:
  im1f, im2f = sys.argv[1], sys.argv[2]
else:
#  im1f = '../data/sf_view1.jpg'
#  im2f = '../data/sf_view2.jpg'
  im1f = 'D:\JMU\computer_vision\SIFTcode\SIFT\hle\JMU6.jpg'
  im2f = 'D:\JMU\computer_vision\SIFTcode\SIFT\hle\JMU14.jpg'
#  im1f = '../data/climbing_1_small.jpg'
#  im2f = '../data/climbing_2_small.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()

Harris特征匹配

 # -*- coding: utf-8 -*-
from pylab import *
from PIL import Image
 
from PCV.localdescriptors import harris
from PCV.tools.imtools import imresize
 
"""
This is the Harris point matching example in Figure 2-2.
"""
 
# Figure 2-2上面的图
#im1 = array(Image.open("../data/crans_1_small.jpg").convert("L"))
#im2= array(Image.open("../data/crans_2_small.jpg").convert("L"))
 
# Figure 2-2下面的图
im1 = array(Image.open("D:\JMU\computer_vision\SIFTcode\SIFT\hle\JMU3.jpg").convert("L"))
im2 = array(Image.open("D:\JMU\computer_vision\SIFTcode\SIFT\hle\JMU111.jpg").convert("L"))
 
# resize加快匹配速度
im1 = imresize(im1, (im1.shape[1]/2, im1.shape[0]/2))
im2 = imresize(im2, (im2.shape[1]/2, im2.shape[0]/2))
 
wid = 5
harrisim = harris.compute_harris_response(im1, 5)
filtered_coords1 = harris.get_harris_points(harrisim, wid+1)
d1 = harris.get_descriptors(im1, filtered_coords1, wid)
 
harrisim = harris.compute_harris_response(im2, 5)
filtered_coords2 = harris.get_harris_points(harrisim, wid+1)
d2 = harris.get_descriptors(im2, filtered_coords2, wid)
 
print 'starting matching'
matches = harris.match_twosided(d1, d2)
 
figure()
gray() 
harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches)
show()

3.实验结果

3.1
在这里插入图片描述
在这里插入图片描述
3.2
在这里插入图片描述
3.3
在这里插入图片描述

4.结果分析

1、SIFT特征提取和检测算法提取到的特征点明显多于Harris角点检测
2、SIFT算法的匹配正确性要高于Harris算法,Harris对旋转角识别性不佳。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!