Python计算机视觉实验四——图像全景拼接

图像全景拼接

  • 1.RANSAC
  • 2.稳健的单应性矩阵估计
  • 3.拼接图像

1.RANSAC

PANSAC是“RANdom SAmple Consensus”(随机一致性采样)的缩写。该方法是用来找到正确模型来拟合带有噪声数据的迭代方法。给定一个模型,例如点集之间的单应性矩阵,RANSAC基本的思想是,数据中包含着正确的点和噪声点,合理的模型应该能够在描述正确数据点的同时摒弃噪声点。
RANSAC算法的步骤:
选择4对匹配特征点(选择4对特征点因为单应性矩阵有8个自由度,需要4个特征点,每个特征点都能构造2个方程,然后求解单应性矩阵,但是任意3个点或4个点不能在同一条直线上)
根据直接线性变换解法DLT计算单应性矩阵H
对所匹配点,计算映射误差
根据误差阈值,确定inliers数量
针对最大的inliers集合,重新计算单应性矩阵H

2.稳健的单应性矩阵估计

我们在任何模型中都可以使用 RANSAC 模块。在使用 RANSAC 模块时,我们只需要在相应 Python 类中实现 fit() 和 get_error() 方法,剩下就是正确地使用 ransac.py,我们这里使用可能的对应点集来自动找到用于全景图像的单应性矩阵。下面是使用SIFT特征自动找到匹配对应。

3.拼接图像

from numpy import *
from matplotlib.pyplot import *
from PIL import Image
from PCV.geometry import warp, homography
# import homography
from PCV.localdescriptors import sift

featname = ['imag3/' + str(i + 1) + '.sift' for i in range(5)]
imname = ['imag3/' + str(i + 1) + '.jpg' for i in range(5)]
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)

# 将匹配转换成齐次坐标点的函数
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


# 估计单应性矩阵
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 = 100  # 用于填充和平移 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()

拼接的图片:
Python计算机视觉实验四——图像全景拼接_第1张图片

Python计算机视觉实验四——图像全景拼接_第2张图片
运行结果:
Python计算机视觉实验四——图像全景拼接_第3张图片
Python计算机视觉实验四——图像全景拼接_第4张图片
从上面的结果可以看到在拼接中山纪念馆图片时,拼接的接口处出现了很明显的断层,甚至在第一张图与第二张图之间出现了建筑物的重影,并且对于地板上有纹路的这些地方它的拼接效果并不是十分的理想。

出现的一些代码编译问题:
Python计算机视觉实验四——图像全景拼接_第5张图片
参考博客:https://blog.csdn.net/weixin_42648848/article/details/88667243

你可能感兴趣的:(python,计算机视觉)