python图像特征检测方法——SIFT,实现过程无法找到sift文件,请大神帮助
from PIL import Image
from numpy import *
from numpy.core.multiarray import ndarray
from pylab import *
from scipy.ndimage import filters
import os
def process_image(imagename, resultname, params = "--edge-thresh 10 "
"--peak-thresh 5"):
"""处理一幅图像,然后将结果保存在文件中"""
if imagename[-3:] != 'pgm':
#创建一个pgm文件
im = Image.open(imagename).convert('L')
im.save('tmp.pgm')
imagename = 'tmp.pgm'
cmmd = str("C:/Users/qiy8/PycharmProjects/learning_computer_version"
"/VLFeat/win32/sift.exe " + imagename +
"--output=" + resultname
+ " " + params)
os.system(cmmd)
print('processed', imagename, 'to', resultname)
def read_features_from_file(filename):
"""读取特征属性值,然后将其以矩阵的形式返回"""
f = loadtxt(filename)
return f[:, :4], f[:, 4:] #特征位置,描述子
def write_features_to_file(filename, locs, desc):
"""将特征位置和描述子保存在文件中"""
savetxt(filename, hstack((locs, desc)))
def plot_features(im, locs, circle = False):
"""显示带有特征的图像
输入:im(数组图像),locs(每个特征的行、列、尺度和朝向"""
def draw_circle(c, r):
t = arange(0, 1.01, .01)*2*pi
x = r*cos(t) + c[0]
y = r*sin(t) + c[1]
plot(x, y, 'b', linewidth=2)
imshow(im)
if circle:
for p in locs:
draw_circle(p[:2], p[2])
else:
plot(locs[:, 0], locs[:, 1], 'ob')
axis('off')
def match(desc1, desc2):
"""对于第一幅图像中的每个描述子,选取其在第二幅图像中的匹配
输入:desc1(第一幅图像中的描述子),desc2(第二幅图像中的描述子)"""
desc1 = array([d / linalg.norm(d) for d in desc1])
desc2 = array([d / linalg.norm(d) for d in desc2])
dist_ratio = 0.6
desc1_size = desc1.shape
matchscores = zeros((desc1_size[0], 1), 'int')
desc2t = desc2.T #预先计算矩阵转置
for i in range(desc1_size[0]):
dotprods = dot(desc1[i, :], desc2t) #向量点乘
dotprods = 0.9999 * dotprods
#反余弦和反排序,返回第二幅图像中特征的索引
indx = argsort(arccos(dotprods))
#检查最相邻的角度是否小于dist_radio乘以第二近邻的角度
if arccos(dotprods)[indx[0]] < dist_ratio * arccos(dotprods)[indx[1]]:
matchscores[i] = int(indx[0])
return matchscores
def match_twosided(desc1, desc2):
"""双向对称版本的match"""
matches_12 = match(desc1, desc2)
matches_21 = match(desc2, desc1)
ndx_12 = matches_12.nonzero()[0]
#去除不对称的匹配
for n in ndx_12:
if matches_12[int(matches_12[n])] != n:
matches_12[n] = 0
return matches_12
def appendimages(im1, im2):
"""返回将两幅图像并列拼接成一幅的新图像"""
#选取具有最少行数的图像,然后填充足够的空行
rows1 = im1.shape[0]
rows2 = im2.shape[0]
if rows1 < rows2:
im1 = concatenate((im1,zeros((rows2 - rows1, im1.shape[1]))), axis=0)
else:
im2 = concatenate((im2, zeros((rows1 - rows2, im2.shape[1]))), axis=0)
#如果这些情况都没有,那么他们的行数相同,不需要进行填充
return concatenate((im1, im2), axis=1)
def plot_matches(im1, im2, locs1, locs2, matchscores, show_below = True):
"""显示一幅带有连接匹配之间连线的图片
输入:im1,im2(数组图像),locs1,locs2(特征位置),matchscores(match()的输出),
show_below(如果图像应该先是在匹配的下方)"""
im3 = appendimages(im1, im2)
if show_below:
im3 = vstack((im3, im3))
imshow(im3)
cols1 = im1.shape[1]
for i, m in enumerate(matchscores):
if m > 0:
plot([locs1[i][1], locs2[m][1] + cols1], [locs1[i][0], locs2[m][
0]], 'c')
axis('off')
以下是我用来做验证实践的代码
from PIL import Image
from numpy import *
from numpy.core.multiarray import ndarray
from pylab import *
from scipy.ndimage import filters
import sift
imname = 'empire.jpg'
im1 = array(Image.open(imname).convert('L'))
sift.process_image(imname, 'empire.sift')
l1, d1 = sift.read_features_from_file('empire.sift')
figure()
gray()
sift.plot_featrues(im1, l1, circle=True)
show()
这是我的代码报错截图

已经尝试过https://blog.csdn.net/lilai619/article/details/48523647该帖中最后提到的解决方法 但是实在是没用,从错误上看,貌似还是cmmd那一行没有正常工作,我的电脑是win764位系统,而且在公司做实习生这个电脑的权限有很大的约束,不知道对我这个程序的实现有没有什么影响,希望各位大佬帮忙解决一下
相关推荐
python实现特征检测算法SIFT、SURF、ORB
python实现特征检测算法SIFT、SURF、ORB等特征检测的算子
图像特征算法(一)——SIFT算法简述及Python标记SIFT特征检测实践
计算机视觉专栏传送 上一篇:无 下一篇:图像特征算法(一)——SURF算法简述及Python标记SURF特征检测实践 前言 SIFT算法作为图像局部特征的里程碑式发明被广泛应用于各个领域,David Lowe的思想简单却深邃。 本文将结合笔者的一点简单理解,从问题出发,由理论到实践,充分掌握SIFT中心思想,了解通过python的SIFT算法简单实现。 一、SIFT算法 1.算法简介 SIFT算法,Scale-invariant feature transform,中文含义就是尺度不变特征变换。该算法自19
图像特征检测—SIFT算法应用(Python)
目录 一:SIFT算法的特征原理描述 1.尺度空间的搭建 2. DOG局部极值检测 3. 对上一过程中得出的候选极值点进行筛选,去除边缘响应点,得到真正的特征点 4. 特征点方向匹配 5.关键特征点的描述 6. 通过特征点的两两比较,找出相互匹配的若干个特征点,建立对应的关系 二:分别运用SIFT和Harris算法,实现图像的特征提取 三:对两张图片进行SIFT特征匹配...
利用 SIFT 实现图像拼接 python 代码
利用 SIFT 实现图像拼接:https://goodgoodstudy.blog.csdn.net/article/details/89157849
基于PYTHON+OPENCV的SIFT SURF图像特征匹配.rar
使用SIFT或者SURF特征进行图像匹配,实现语言为python2.7+opencv3.1,代码有注释,有文档说明,并配有测试图像资源


