ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python计算机视觉编程 - 第二章 图像局部描述符 -SIFT特征匹配

2019-03-17 21:55:26  阅读:620  来源: 互联网

标签:Python nbr 描述符 matches SIFT harris sift import


本次测试以sift特征匹配处理结果做对比为主,Harris特征匹配处理结果为辅做对比用途。
本次测试中使用的所有图片均为500x375(或375x500)像素的jpg图片。

1.SIFT特征原理描述

SIFT是尺度不变特征变换,其过程包括兴趣点的采集和描述子。SIFT的描述子具有很强的稳健性,这也是SIFT特征成功和流行的主要原因。
SIFT具体可以分为以下几个步骤。
1.使用高斯差分函数来定位兴趣点:
在这里插入图片描述
Gσ是二维高斯核,Iσ是Gσ模糊的灰度图像,K是决定相差尺度的常数。兴趣点是在图像位置和尺度变化下D(x,σ)的最大值和最小值点。
SIFT描述子在兴趣点附近选取子区域网格,在每个区域内计算图像梯度方向直方图,每个子区域的直方图拼接起来组成描述子向量。SIFT描述子的标准设置使用4x4的子区域,每个子区域使用8个小区间的方向直方图,共计产生128个小区间直方图。最终确定特征方向。
具体原理参照

2.SIFT特征和Harris特征匹配处理结果对比(两张图片)

2.1二者检测感兴趣点的不同

#- 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 = ‘21.jpg’
im = array(Image.open(imname).convert(‘L’))
sift.process_image(imname, ‘21.sift’)
l1, d1 = sift.read_features_from_file(‘21.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()
在这里插入图片描述

2.2Harris特征匹配处理

#- 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.
“”"
im1 = array(Image.open(“21.jpg”).convert(“L”))
im2 = array(Image.open(“22.jpg”).convert(“L”))

resize加快匹配速度
im1 = imresize(im1, (int(im1.shape[1]/2), int(im1.shape[0]/2)))
im2 = imresize(im2, (int(im2.shape[1]/2), int(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()
在这里插入图片描述

2.3SIFT特征匹配处理

在使用sift算法进行实验前,我们首先要准备一个VLFeat工具包,利用其提供的二进制文件进行计算特征。
解压完后,我们从其中的bin文件夹里找到如下图所示的蓝色选中文件
在这里插入图片描述
将他们复制到工程所在的目录下。
在这里插入图片描述
而后修改PCV包中的sift.py文件中process_image方法中蓝色选中代码的路径,记得在sift.exe后加一个空格,不然会出现编译错误。接下来我们求可以开始进行下面的实验了。
下面是使用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 = ‘21.jpg’
im2f = ‘22.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_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算法存在一定误差,

3 地理标记图像匹配

实验的素材是厦门市集美大学内的一座标志性建筑“尚大楼”范围。通过不同角度拍摄了17张照片,以此进行地理标记图像匹配。
在进行实验前要先安装完GraphViz,否则会出现如下错误
在这里插入图片描述
参照 https://blog.csdn.net/sinat_38653840/article/details/84776806
在该行代码imlist = imtools.get_imlist(download_path) 出现错误,导致程序运行结果为空
在这里插入图片描述

-- coding: utf-8 --

from pylab import *
from PIL import Image
from PCV.localdescriptors import sift
from PCV.tools import imtools
import pydot

download_path = “D:\JetBrains\PyCharm2018.1\work1\sans” # set this to the path where you downloaded the panoramio images
path = “D:\JetBrains\PyCharm2018.1\work1” # 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(‘b.png’)

通过print(imlist)输出可知列表中并没有图片名称。
在将图片后缀的大写修改为小写后,成功输出结果。
在这里插入图片描述
从这张图可以看出,由于图片中有着尚大楼这一标志性建筑物,其特征明显,匹配结果图片就十分狭长。

标签:Python,nbr,描述符,matches,SIFT,harris,sift,import
来源: https://blog.csdn.net/weixin_43848274/article/details/88604963

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有