计算机视觉——全景拼接原理及其应用

一、全景拼接理论

1、全景拼接介绍

  所谓全景拼接,指的是对于同一个语义场景,对不同视角下的图像进行拼接,结合成一张完整图像的操作过程。

2、全景拼接原理

2.1、单应性变换

为了了解全景拼接原理,我们首先需要知道一个很重要的概念,单应性变换。

通俗的说,单应性变换指的是将一个平面内的点映射到另一个平面内的二维投影变换。单应性变换的目的是,将两张不同视角但有联系的图像对应起来,以便找出相同的对应点,方便之后的匹配以及拼接。

2.1.1、单应性矩阵

单应性矩阵是我们用来进行单应性变换的数学工具,而我们在任何模型中都可以通过使用TANSAC算法来求解对应的单应性矩阵。

2.2、全景拼接流程

          根据目标图像,实现特征匹配

          计算目标图像之间的变换结构

          根据图像之间的变换结构,实现图像映射

         针对叠加之后的图像,采用APAP等算法,实现特征点对齐

         通过“最大流最小割”等图割法,自动选取拼接缝

        实现融合

二、全景拼接应用

        在这篇文章里,将对于室内场景以及室外景深落差大小差异的两幅场景进行前景拼接。

      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 = ['C:/Users/ASUS/Desktop/a/Univ'+str(i+1)+'.sift' for i in range(5)] 
imname = ['C:/Users/ASUS/Desktop/a/Univ'+str(i+1)+'.jpg' for i in range(5)]

# extract features and 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    


# warp the images
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')
savefig("example5.png",dpi=300)
show()

     2、室内场景   

对于这组图片,会出现以下报错

Warning (from warnings module):
  File "D:\python\lib\site-packages\PCV\geometry\homography.py", line 154
    row /= points[-1]
RuntimeWarning: invalid value encountered in true_divide

Warning (from warnings module):
  File "D:\python\lib\site-packages\PCV\tools\ransac.py", line 87
    also_idxs = test_idxs[test_err < t] # select indices of rows with accepted points
RuntimeWarning: invalid value encountered in less

Warning (from warnings module):
  File "D:\python\lib\site-packages\PCV\geometry\homography.py", line 154
    row /= points[-1]
RuntimeWarning: divide by zero encountered in true_divide
Traceback (most recent call last):
  File "C:\Users\ASUS\Desktop\a\qjpj.py", line 53, in <module>
    H_12 = homography.H_from_ransac(fp,tp,model)[0] #im 1 to 2
  File "D:\python\lib\site-packages\PCV\geometry\homography.py", line 60, in H_from_ransac
    H,ransac_data = ransac.ransac(data.T,model,4,maxiter,match_theshold,10,return_all=True)
  File "D:\python\lib\site-packages\PCV\tools\ransac.py", line 106, in ransac
    raise ValueError("did not meet fit acceptance criteria")
ValueError: did not meet fit acceptance criteria
>>> 

第一个超时可以估计是由于图片本身格式大小的原因。而下面的这个错误提示我们遇到了无效值

divide by zero encountered in true_divide

这个方法可以用通过导入numpy库里的方法来解决

import numpy as np
 np.seterr(divide='ignore', invalid='ignore')

但是另一个报错

did not meet fit acceptance criteria

  在室内场景会出现这里报错,而室外不会,十分费解,待解决后,会贴上拼接结果。

 

 

3、室外场景

下面是这次所用的图片,被拍摄地点是集美大学嘉庚图书馆的后楼。

 

实验结果如下:

 

 

由于我的拍摄手法实在是粗糙,以及移动尺度过大,拼接结果有点不堪入目。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值