一、全景拼接原理
此次全景拼接使用RANSAC算法估计出图像间的单应性矩阵,然后将所有图像扭曲到一个公共的平面上。
由于我们所有的图像是由相机水平旋转拍摄的,因此我们可以将中心图像左右的区域填充为0,以便为扭曲的图像腾出空间。
RANSAC(Random Sample Consensus)算法是一种简单且有效的去除噪声影响,估计模型的一种方法。与普通的去噪算法不同,RANSAC算法是使用尽可能少的点来估计模型参数,然后尽可能的扩大得到的模型参数的影响范围。 由下图可以看到,该算法只选择了和直线一致的数据点,成功地找到了正确的解。
RANSAC详解:https://blog.csdn.net/vict_wang/article/details/81027730
二、不同场景的全景拼接
1.代码
仅适用于五张图片从左到右降序命名:
from pylab import *
from numpy import *
from PIL import Image
# If you have PCV installed, these imports should work
from PCV.geometry import homography, warp
from PCV.localdescriptors import sift
"""
This is the panorama example from section 3.3.
"""
# set paths to data folder
#featname = ['D:/data/Univ'+str(i+1)+'.sift' for i in range(5)]
#imname = ['D:/data/Univ'+str(i+1)+'.jpg' for i in range(5)]
featname = ['D:/图片/life6/'+str(i+1)+'.sift' for i in range(5)]
imname = ['D:/图片/life6/'+str(i+1)+'.jpg' for i in range(5)]
# extract features and m
# match
l = {}
d = {}
for i in range(5):
sift.process_image(imname[i],featname[i])
l[i],d[i] = sift.read_features_from_file(featname[i])
matches = {}
for i in range(4):
matches[i] = sift.match(d[i+1],d[i])
# visualize the matches (Figure 3-11 in the book)
for i in range(4):
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)
# function to convert the matches to hom. points
# 将匹配转换成齐次坐标点的函数
def convert_points(j):
ndx = matches[j].nonzero()[0]
fp = homography.make_homog(l[j+1][ndx,:2].T)
ndx2 = [int(matches[j][i]) for i in ndx]
tp = homography.make_homog(l[j][ndx2,:2].T)
# switch x and y - TODO this should move elsewhere
fp = vstack([fp[1],fp[0],fp[2]])
tp = vstack([tp[1],tp[0],tp[2]])
return fp,tp
# estimate the homographies
# 估计单应性矩阵
model = homography.RansacModel()
fp,tp = convert_points(1)
H_12 = homography.H_from_ransac(fp,tp,model)[0] #im 1 to 2
fp,tp = convert_points(0)
H_01 = homography.H_from_ransac(fp,tp,model)[0] #im 0 to 1
tp,fp = convert_points(2) #NB: reverse order
H_32 = homography.H_from_ransac(fp,tp,model)[0] #im 3 to 2
tp,fp = convert_points(3) #NB: reverse order
H_43 = homography.H_from_ransac(fp,tp,model)[0] #im 4 to 3
# 扭曲图像
delta = 2000 # 用于填充和平移 for padding and translation
im1 = array(Image.open(imname[1]), "uint8")
im2 = array(Image.open(imname[2]), "uint8")
im_12 = warp.panorama(H_12,im1,im2,delta,delta)
im1 = array(Image.open(imname[0]), "f")
im_02 = warp.panorama(dot(H_12,H_01),im1,im_12,delta,delta)
im1 = array(Image.open(imname[3]), "f")
im_32 = warp.panorama(H_32,im1,im_02,delta,delta)
im1 = array(Image.open(imname[4]), "f")
im_42 = warp.panorama(dot(H_32,H_43),im1,im_32,delta,2*delta)
figure()
imshow(array(im_42, "uint8"))
axis('off')
show()
2.结果展示
1.室内场景
2.室外景深落差小场景
3.室外景深落差大场景
三、实验结果分析
室内场景拼接结果较为连贯,但是柜子部分出现“鬼影“现象。
室外景深落差小拼接效果较好,但是部分图像扭曲变形过度影响整体观感。
室外景深落差大图像拼接效果不甚理想,湖岸没有连上,代码需要改进。