地理标记图像匹配debug

代码段

# 总代码
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')


运行结果:

地理标记图像匹配debug_第1张图片

输出

地理标记图像匹配debug_第2张图片
没有任何输出结果

Summary

"2.png"无法输出正确的匹配连接图
观察匹配矩阵知 除对角线外没有大于2的值 因此输出的是空白图片
此实际为SIFT算法描述子匹配的常见问题

  • 此例中有7734个匹配
    地理标记图像匹配debug_第3张图片
  • 此处仅为1个
    地理标记图像匹配debug_第4张图片
  • 此处甚至没有匹配
    地理标记图像匹配debug_第5张图片
    由此产生了上述近似的对角线矩阵 导致threshould设置为2时没有输出
    将描述子匹配算法由sift.match改为harris.match 即从角距离到欧式距离
....
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即可得如图所示的匹配结果
地理标记图像匹配debug_第6张图片

Improvement

sift算法原理
SIFT(LOWE)论文翻译

你可能感兴趣的:(python,计算机视觉,开发语言)