影象拼接,單應性矩陣計算
阿新 • • 發佈:2022-03-17
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 importrescale 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()