SIFT原理及图像特征匹配

1.SIFT(尺度不变特征变换)原理
SIFT包括兴趣点检测器和描述子。SIFT描述子具有非常强的稳健性,经常和许多不同的兴趣点检测器结合使用。SIFT特征对于尺度,旋转和亮度都具有不变性,因此,它可用于三维视角和噪声的可靠匹配。
SIFT 描述子h(x, y,θ)是对特征点附近邻域内高斯图像梯度统计结果的一种表示,它是一个三维的阵列,但通常将它表示成一个矢量。矢量是通过对三维阵列按一定规律进行排列得到的。特征描述子与特征点所在的尺度有关,因此,对梯度的求取应在特征点对应的高斯图像上进行。
SIFT算法的实现步骤:
1、构建尺度空间,检测极值点,获得尺度不变性;
2、特征点过滤并进行精确定位,剔除不稳定的特征点;
3、在特征点处提取特征描述符,为特征点分配方向值;
4、生成特征描述子,利用特征描述符寻找匹配点;
5、计算变换参数。
2.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/crans_1_small.jpg’
im2f = ‘…/data/crans_2_small.jpg’

im1 = array(Image.open(im1f))
im2 = array(Image.open(im2f))

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

SIFT原理及图像特征匹配
(下各种库各种包啊简直要烦死)
3.harris图像特征匹配
代码为:
# -- coding: utf-8 --
import cv2
import scipy.misc
import numpy as np
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://Program Files//image//09.jpg”).convert(“L”))
im2 = array(Image.open(“D://Program Files//image//10.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()
‘’‘发生错误:1.TypeError: integer argument expected, got float
解决方式:将
im1 = imresize(im1, (im1.shape[1]/2, im1.shape[0]/2))
im2 = imresize(im2, (im2.shape[1]/2, im2.shape[0]/2))中的“/”改成“//”
原因是imresize()支持浮点型
2.python3版本中print后一定要加()
3.运行好慢呀’’’
效果如图:SIFT原理及图像特征匹配
4.Harris & sift 图像特征匹配的区别
Sift匹配是一种电脑视觉的算法用来侦测与描述影像中的局性特征,它在空间尺度中寻找极值点,并提取出其位置、尺度、旋转不变量。SIFT特征是基于物体上的一些局部外观的兴趣点而与影像的大小和旋转无关.对于光线、噪声、些微视角改变的容忍度。SIFT特征的信息量大,适合在海量数据库中快速准确匹配.也相当高.
Harris匹配分别绘制出的图像,可使用线段连接的像素点来直观地视化,此匹配图像像素块的互相关矩阵具有较弱的描述性,并且此描述不具有尺度不变性和旋转不变性,而算法中像素块的大小也会影响对应匹配的结果。
5.图片地理标记
代码为:
# -- 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 = “F:/dropbox/Dropbox/translation/pcv-notebook/data/panoimages” # set this to the path where you downloaded the panoramio images
path = “F:/dropbox/Dropbox/translation/pcv-notebook/data/panoimages/” # 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 triangle
print '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 diagonal
matchscores[j, i] = matchscores[i, j]

#可视化

threshold = 2 # min number of matches needed to create link

g = pydot.Dot(graph_type=‘graph’) # don’t want the default directed graph

for i in range(nbr_images):
for j in range(i + 1, nbr_images):
if matchscores[i, j] > threshold:
# first image in pair
im = Image.open(imlist[i])
im.thumbnail((100, 100))
filename = path + str(i) + ‘.png’
im.save(filename) # need temporary files of the right size
g.add_node(pydot.Node(str(i), fontcolor=‘transparent’, shape=‘rectangle’, image=filename))

        # second image in pair
        im = Image.open(imlist[j])
        im.thumbnail((100, 100))
        filename = path + str(j) + '.png'
        im.save(filename)  # need temporary files of the right size
        g.add_node(pydot.Node(str(j), fontcolor='transparent', shape='rectangle', image=filename))

        g.add_edge(pydot.Edge(str(i), str(j)))

g.write_png(‘whitehouse.png’)
如图:
SIFT原理及图像特征匹配
绝望三连!我爱学习