匹配地理标记图像

匹配地理标记图像
1、安装配置Graphviz和pydot
首先安装Graphviz,从官网进行下载,进行安装就可以,(安装过程一定要保证网络稳定,不然安装过程会出现问题)win+R进入cmd就可以进行安装,安装后打开文件夹,找到dot文件,将它的路径添加到系统环境变量path中,如下图所示匹配地理标记图像
环境配置完成后,进行pydot的安装,在cmd命令框进行安装之后 还需在pycharm中进行安装,打开settings并且点击右侧“+”,在搜索框搜索“pydot”,再进行安装如下图匹配地理标记图像
下图为安装成功提示匹配地理标记图像
接下来就可以运行代码了
2、运行代码以及结果
将所有的照片素材放在一个文件夹中,注意图片如果过大的话,运行过程会过于长,所以在运行之前可以将图片的像素进行更改,保证其流畅运行,下面为代码部分

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 = "C:\\Users\lianx\PycharmProjects\\untitled2\zsjng"  # set this to the path where you downloaded the panoramio images
path = "C:\\Users\lianx\PycharmProjects\\untitled2\zsjng"  # 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')

下图为运行结果匹配地理标记图像
匹配地理标记图像
由上图可见,匹配过程中有个别图片的匹配效果很差,这些结果与图片拍摄的外部环境有很大的关系,图片主体不鲜明或者色彩差别很大都会影响结果。