from pylab import* from PIL import Image from PCV.localdescriptors import harris""" Example of detecting Harris corner points (Figure 2-1 in the book). """# 读入图像 im = array(Image.open('1.jpg').convert('L'))# 检测harris角点 harrisim = harris.compute_harris_response(im)# Harris响应函数 harrisim1 =255- harrisimfigure() gray()#画出Harris响应图 subplot(141) imshow(harrisim1) print(harrisim1.shape) axis('off') axis('equal')threshold =[0.01,0.05,0.1] for i, thres inenumerate(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()
原图:
运行结果:
原图:
运行结果:
1.5. Harris角点匹配实现
代码如下:
#Harris角点匹配 from pylab import* from PIL import Imagefrom PCV.localdescriptors import harris from PCV.tools.imtools import imresize""" This is the Harris point matching example in Figure 2-2. """# Figure 2-2上面的图 #im1 = array(Image.open("../data/crans_1_small.jpg").convert("L")) #im2= array(Image.open("../data/crans_2_small.jpg").convert("L"))# Figure 2-2下面的图 im1 = array(Image.open('3.jpg').convert("L")) im2 = array(Image.open('4.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()