匹配地理标记图像
学会SIFT之后,我们开始学习地理区域的匹配
首先,我在运行代码的时候,出现错误import pydot的错误
所以首先要配置环境。
第一步 安装graphviz
在vscode里使用终端,进入powershell,输入pip install graphviz
然后进入官网下载https://graphviz.gitlab.io/_pages/Download/Download_windows.html 下载graphviz的时候,一定要选择后缀是.msi的文件,然后安装在D盘。进入该文件夹的bin
复制路径配置环境变量
第二步 安装pydot
在vscode终端的powershell中输入pip install pydot
接着在python的文件夹中搜索pydot.py文件,将其中的代码换成Graphviz中bin的路径复制到上去,例如:D:\pypackage\Graphviz-2.38\bin\dot.exe
然后保存即可
第三步 运行代码
根据代码
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 = r"C:\Users\dell\Documents\VS_code workplace\SDL" # set this to the path where you downloaded the panoramio images
path = r"C:\Users\dell\Documents\VS_code workplace\SDLsave" # path to save thumbnails (pydot needs the full system path)
imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)
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)
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('jmu.png')
得到效果