PCV库链接:https://github.com/Li-Shu14/PCV
将下载的文件解压
打开cmd,执行如下指令:
(1)执行cd命令,转到你所解压到的PCV的文件夹中。
(2)输入python setup.py install
# RANSAC算法拼接2张图片 from pylab import * from numpy import * from PIL import Image from PCV.geometry import homography, warp from PCV.localdescriptors import sift # 获取图片 from skimage.transform import rescale featname = [r'D:\python_test\ttt\SLAM-ransac-master\S' + str(i + 1) + '.sift' for i in range(2)] imname = [r'D:\python_test\ttt\SLAM-ransac-master\S' + str(i + 1) + '.jpg' for i in range(2)] # 提取特征并匹配 l = {} d = {} for i in range(2): sift.process_image(imname[i], featname[i]) l[i], d[i] = sift.read_features_from_file(featname[i]) matches = {} for i in range(1): matches[i] = sift.match(d[i + 1], d[i]) # visualize the matches for i in range(1): im1 = array(Image.open(imname[i])) im2 = array(Image.open(imname[i + 1])) figure() sift.plot_matches(im2, im1, l[i + 1], l[i], matches[i], show_below=True) # 将匹配转换成齐次坐标点的函数 def convert_points(j): ndx = matches[j].nonzero()[0] fp1 = homography.make_homog(l[j + 1][ndx, :2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp1 = homography.make_homog(l[j][ndx2, :2].T) # switch x and y - TODO this should move elsewhere fp1 = vstack([fp1[1], fp1[0], fp1[2]]) tp1 = vstack([tp1[1], tp1[0], tp1[2]]) return fp1, tp1 # 估计单应性矩阵 model = homography.RansacModel() # RANSAC稳健性估计点对应间的单应性矩阵Hv fp, tp = convert_points(0) print("fp:", fp ) print("tp:", tp ) H_01 = homography.H_from_ransac(fp, tp, model)[0] # im 0 to 1 print(H_01 ) # warp the images(扭曲图像) delta = 200 # for padding and translation im1 = array(Image.open(imname[0]), "uint8") im2 = array(Image.open(imname[1]), "uint8") im_12 = warp.panorama(H_01, im1, im2, delta, delta) rgbn_interpolation[:,:,i] = rescale(img, (4,4)) figure() imshow(array(im_12, "uint8")) axis('off') savefig("ps.jpg", dpi=300) show()