python图片拼接算法实现_python实现单张图像拼接与批量图片拼接

本文实例为大家分享了python实现图像拼接的具体代码,供大家参考,具体内容如下

一、效果

二、代码

1、单张图片拼接

# 图片拼接

from PIL import Image

# pil paste可以进行图片拼接

import cv2

import numpy as np

path="F:/out/"+str(0)+".jpg"

img_out=cv2.imread(path)

num=5

for i in range(1,num):

path="F:/out/"+str(i)+".jpg"

img_tmp=cv2.imread(path)

#横向

img_out = np.concatenate((img_out, img_tmp), axis=1)

# 纵向

# img_out = np.concatenate((img_out, img_tmp))

cv2.imshow("IMG",img_out)

cv2.imwrite("F:/out/merge.jpg",img_out)

cv2.waitKey(0)

2、批量图片拼接

# 图片拼接

from PIL import Image

# pil paste可以进行图片拼接

import cv2

import numpy as np

import glob as glob

import os

num=5

os.chdir(r"F:\out")

img_name=[]

for file_name in glob.glob("*.jpg"):

print(file_name)

img_name.append(file_name)

# 批量处理图片

img_path = glob.glob("F:/out/*jpg")

for i in range(int(len(img_path)/num)):

path = img_path[i*num]

print(path)

img_out = cv2.imread(path)

for j in range(1, num):

path = img_path[i*num+j]

img_tmp = cv2.imread(path)

# 横向

img_out = np.concatenate((img_out, img_tmp), axis=1)

# cv2.imshow("IMG", img_out)

cv2.imwrite("F:/out/"+img_name[i*num+j][0:-6]+"_out.jpg", img_out)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2020-03-23

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答您的问题!以下是基于SIFT算法Python图片拼接代码: ```python import cv2 import numpy as np def sift_alignment(img1, img2): sift = cv2.xfeatures2d.SIFT_create() kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) good_matches = matches[:int(len(matches) * 0.10)] src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], max(img1.shape[0], img2.shape[0]))) result[0:img2.shape[0], 0:img2.shape[1]] = img2 return result if __name__ == '__main__': img1 = cv2.imread('path_to_image_1') img2 = cv2.imread('path_to_image_2') aligned_image = sift_alignment(img1, img2) cv2.imshow('Aligned Image', aligned_image) cv2.waitKey(0) ``` 这段代码使用OpenCV中的SIFT算法进行图片拼接,具体步骤包括以下几步: - 使用SIFT算法提取两张图片中的关键点和特征描述子 - 使用暴力匹配算法和交叉匹配策略(crossCheck=True)进行特征点匹配 - 使用基于RANSAC的单应矩阵估计方法(cv2.findHomography)计算两张图片之间的单应矩阵 - 使用单应矩阵对第一张图片进行透视变换,然后在结果图像上叠加第二张图片 最后,您可以根据需要将代码嵌入到自己的项目中,或者根据自己的需求进行细微的调整。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值