匹配地理标记图像
在imlist和featlist列表中保存图像文件名,对所有组合图像进行逐个匹配,并将每对图像间的匹配特征数保存在matchscores矩阵中:
import sift
nbr_images = len(imlist)
matchscores = zeros((nbr_images,nbr_images))
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 = sift.match_twosided(d1,d2)
nbr_matches = sum(matches>0)
print 'number of matches = ', nbr_matches
matchscores[i,j] = nbr_matches
#复制值
for i in range(nbr_images):
for j in range(i+1,nbr_images): #不需要复制对角线
matchscores[j,i] = matchscores[i,j]
[pydot][7]工具包是功能强大的[GraphViz][8]图形库的Python接口,使用pydot创建一个图:
import pydot
g = pydot.Dot(graph_type='graph')
g.add_node(pydot.Node(str(0),fontcolor='transparent'))
for i in range(5):
g.add_node(pydot.Node(str(i+1)))
g.add_edge(pydot.Edge(str(0),str(i+1)))
for j in range(5):
g.add_node(pydot.Node(str(j+1)+'-'+str(i+1)))
g.add_edge(pydot.Edge(str(j+1)+'-'+str(i+1),str(j+1)))
g.write_png('graph.png',prog='neato')
效果图:
使用pydot可视化地连接匹配的图像,为了得到图像需要使用图像的全路径,为了使图像更美观将每幅图像尺度化为缩略图形式:
import pydot
threshold = 2 #创建关联需要的最小匹配数目
g = pydot.Dot(graph_type='graph') #不使用默认的有向图
for i in range(nbr_images):
for j in range(i+1,nbr_images):
if matchscores[i,j] > threshold:
#图像对中的第一幅图像
im = Image.open(imlist[i])
im.thumbnail((100,100))
filename = str(i)+'.png'
im.save(filename) #需要一定大小的临时文件
g.add_node(pydot.Node(str(i),fontcolor='transparent',shape='rectangle',image=path+filename))
#图像对中的第二幅图像
im = Image.open(imlist[j])
im.thumbnail((100,100))
filename = str(j)+'.png'
im.save(filename) #需要一定大小的临时文件
g.add_node(pydot.Node(str(j),fontcolor='transparent',shape='rectangle',image=path+filename))
g.add_edge(pydot.Edge(str(i),str(j)))
g.write_png('whitehouse.png')