# 总代码
import urllib
from PIL import Image
from numpy import *
from pylab import *
import harris
import sift
import imtools
from pylab import *
from PIL import Image
import pydot
import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files/Graphviz/bin/'
download_path="E:/Jupter_Doc/ComputerVersion/Chapt2/whitehouse/"
path="E:/Jupter_Doc/ComputerVersion/Chapt2/whitehouse/"
# 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('2.png')
"2.png"无法输出正确的匹配连接图
观察匹配矩阵知 除对角线外没有大于2的值 因此输出的是空白图片
此实际为SIFT算法描述子匹配的常见问题
....
for i in range(nbr_images):
for j in range(i,nbr_images):
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=harris.match_twosided(d1,d2) # 将sift.match改为harris.match
nbr_matches=sum(matches>0)
print("number of matches=",nbr_matches)
matchscores[i,j]=nbr_matches
....
import pydot
threshold=100 # 将源代码中的threshold=2改为100
g=pydot.Dot(graph_type="graph")
得到的匹配矩阵如下
此时将thrshold改为100即可得如图所示的匹配结果
sift算法原理
SIFT(LOWE)论文翻译