利用Python实现图像拼接

利用python语言实现多张图像拼接_小饼干cookie的博客-CSDN博客_python拼接图像

主要参考上述博文,只是对于复现过程遇到的小问题的解决做一些补充。

拼接程序:

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.

"""

featname = ['D:/pythonCode/test/data/testimages/' + str(i + 1) + '.sift' for i in range(5)]   //需要根据自己的图像地址和图像数量修改地址和循环次数
imname = ['D:/pythonCode/test/data/testimages/' + 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):    //循环次数=图像数量-1
    matches[i] = sift.match(d[i + 1], d[i])

visualize the matches (Figure 3-11 in the book)
for i in range(4):    //循环次数=图像数量-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)

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 = 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、需要安装PCV包

参考手把手解决解决Python安装PCV_日拱一卒不慌忙的博客-CSDN博客

2、代码中的读取图像,无.sift文件

imname使我们要拼接的原图

featname是sift文件,这个文件是需要根据原图进行生成的

具体代码参考

-*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.localdescriptors import sift
from PCV.localdescriptors import harris

添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:/windows/fonts/SimSun.ttc", size=14)

imname = 'D:/ComputerVision_code/img/sdl11.jpg'   //需要拼接的图像的位置
im = array(Image.open(imname).convert('L'))
sift.process_image(imname, 'empire.sift')        //生成名称为empire的sift文件
l1, d1 = sift.read_features_from_file('empire.sift')

figure()
gray()
subplot(121)
sift.plot_features(im, l1, circle=False)
title(u'SIFT特征',fontproperties=font)

检测harris角点
harrisim = harris.compute_harris_response(im)

subplot(122)
filtered_coords = harris.get_harris_points(harrisim, 6, 0.1)
imshow(im)
plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
axis('off')
title(u'Harris角点',fontproperties=font)

show()

报错:empire.sift not found

原因:未修改PCV文件夹下的sift.py中的process_image函数。

解决方案:下载VLFeat0.9.20的版本,将三个相关的文件复制到自己项目环境中,再将Anaconda中sift.py的文件打开地址修改一下就可以了

主要参考:【python】 OSError:sift not found 问题解决_Lin-CT的博客-CSDN博客

博主讲的很详细,我就不画蛇添足了。

3、成功生成了sift文件,可以继续运行图像拼接程序,报错did not meet fit acceptance criteria

原因:图片问题,输入的图片太暗或太糊,导致系统无法进行特征提取或特征点太少无法匹配(有时候图片正常也会出现这种情况,反正出现这个问题只需换图片即可解决)。

主要参考:python计算机视觉常见报错及解决方案(不断更新中)_Lin-CT的博客-CSDN博客

个人感受:采用上述方式进行图像拼接,需要图像特征点较多并明显才能达到好的拼接效果,否则要么特征点太少程序无法运行,要么拼接效果很差。

我的图像的特征点较少,并且位移信息基本已知,我打算通过平移叠加进行拼图,在想是直接用PS拼(真香),还是再找找新的图像处理方法[哭笑不得]

快看我美丽的拼接成果。

利用Python实现图像拼接

再附两个参考博文:

SIFT特征提取+匹配_Deer dolphin的博客-CSDN博客_sift特征提取和匹配

实验四——图像拼接_cyh_first的博客-CSDN博客_图像拼接测试图片

OpenCV图像处理–常用图像拼接方法_C君莫笑的博客-CSDN博客_opencv 图像拼接

感谢在解决问题过程中各位大佬的帮助,如果我的回答帮助到你,请给我点赞加油呀~~

Original: https://blog.csdn.net/MOZHOUH/article/details/124948274
Author: 日拱一卒不慌忙
Title: 利用Python实现图像拼接

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/518609/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球