图像局部描述符

Harris角点检测

Harris角点检测算法是最简单的角点检测方法之一。


from pylab import *
from PIL import Image
from PCV.localdescriptors import harris


#读入图像
im = array(Image.open('C:/Users/Administrator/35.jpg').convert('L'))

#检测harris角点
harrisim = harris.compute_harris_response(im)

#Harris响应函数
harrisim1 = 255 - harrisim

figure()
gray()

#画出Harris响应图
subplot(141)
imshow(harrisim1)
print harrisim1.shape
axis('off')
axis('equal')

threshold = [0.01, 0.05, 0.1]
for i, thres in enumerate(threshold):
   filtered_coords = harris.get_harris_points(harrisim, 6, thres)
   subplot(1, 4, i+2)
   imshow(im)
   print im.shape
   plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
   axis('off')

#原书采用的PCV中PCV harris模块
#harris.plot_harris_points(im, filtered_coords)

#plot only 200 strongest
#harris.plot_harris_points(im, filtered_coords[:200])

show()

原图
图像局部描述符

得到图片
图像局部描述符

在图像间寻找对应点

Harris角点检测器可以给出图像中检测到兴趣点,但它并没有提供在图像间对兴趣点进行比较的方法,我们需要在每个角点添加描述子,以及对这些描述子进行比较。

from pylab import *
from PIL import Image
 
from PCV.localdescriptors import harris
from PCV.tools.imtools import imresize
 
"""
This is the Harris point matching example in Figure 2-2.
"""
 

im1 = array(Image.open("C:/Users/Administrator/36.jpg").convert("L"))
im2= array(Image.open("C:/Users/Administrator/37.jpg").convert("L"))
 

 
# resize加快匹配速度
im1 = imresize(im1, (im1.shape[1]//2, im1.shape[0]//2))
im2 = imresize(im2, (im2.shape[1]//2, im2.shape[0]//2))
##因为我的一直报错说整型变成浮点型所以这里把/改为//
wid = 5
harrisim = harris.compute_harris_response(im1, 5)
filtered_coords1 = harris.get_harris_points(harrisim, wid+1)
d1 = harris.get_descriptors(im1, filtered_coords1, wid)
 
harrisim = harris.compute_harris_response(im2, 5)
filtered_coords2 = harris.get_harris_points(harrisim, wid+1)
d2 = harris.get_descriptors(im2, filtered_coords2, wid)
 
print 'starting matching'
matches = harris.match_twosided(d1, d2)
 
figure()
gray() 
harris.plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches)
show()

得到结果
图像局部描述符

sift描述子

在过去的十年间,最成功的图像局部描述子之一是尺度不变特征变换(SIFT),它是由David Lowe发明的。SIFT在2004年由Lowe完善并经受住了时间的考验。


# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.localdescriptors import sift
from PCV.localdescriptors import harris
 

imname = 'C:/Users/Administrator/37.jpg'
im = array(Image.open(imname).convert('L'))
sift.process_image(imname, 'empire.sift')
l1, d1 = sift.read_features_from_file('empire.sift')
 
figure()
gray()
subplot(131)
sift.plot_features(im, l1, circle=False)
title(u'Sift')
subplot(132)
sift.plot_features(im, l1, circle=True)
title(u'Circle')
 
# 检测harris角点
harrisim = harris.compute_harris_response(im)
 
subplot(133)
filtered_coords = harris.get_harris_points(harrisim, 6, 0.1)
imshow(im)
plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
axis('off')
title(u'Harris't)
 
show()

得到图片
图像局部描述符

描述子匹配


from PIL import Image
from pylab import *
import sys
from PCV.localdescriptors import sift
 
 
if len(sys.argv) >= 3:
  im1f, im2f = sys.argv[1], sys.argv[2]
else:

  im1f = 'C:/Users/Administrator/36.jpg'
  im2f = 'C:/Users/Administrator/38.jpg'

im1 = array(Image.open(im1f))
im2 = array(Image.open(im2f))
 
sift.process_image(im1f, 'out_sift_1.txt')
l1, d1 = sift.read_features_from_file('out_sift_1.txt')
figure()
gray()
subplot(121)
sift.plot_features(im1, l1, circle=False)
 
sift.process_image(im2f, 'out_sift_2.txt')
l2, d2 = sift.read_features_from_file('out_sift_2.txt')
subplot(122)
sift.plot_features(im2, l2, circle=False)
 
#matches = sift.match(d1, d2)
matches = sift.match_twosided(d1, d2)
print '{} matches'.format(len(matches.nonzero()[0]))
 
figure()
gray()
sift.plot_matches(im1, im2, l1, l2, matches, show_below=True)
show()

得到结果
图像局部描述符
图像局部描述符

地理标记图像匹配

使用图片:
图像局部描述符

# -*- coding: utf-8 -*-
from pylab import *
from PIL import Image
from PCV.localdescriptors import sift
from PCV.tools import imtools
import os
import pydot
os.environ['PATH'] = os.environ['PATH'] + (';C:\\Program Files\\Graphviz\\bin\\')
""" 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\\Administrator\\pipei"  # set this to the path where you downloaded the panoramio images
path = "C:\\Users\\Administrator\\pipei"  # path to save thumbnails (pydot needs the full system path)

# 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('1.png')

得到结果:
图像局部描述符

上一篇:AssertionError: Input is not a training model (no 'regression' and 'classification


下一篇:Nmap扫描工具