python图像拼接_pencv-python4.2多个图片拼接成一个图片

两个原始图片

拼接后的结果

代码

import os

import cv2

import numpy as np

img_dir = './images' #原始文件目录

names = os.listdir(img_dir)

images = []

for name in names:

img_path = os.path.join(img_dir, name)

image = cv2.imread(img_path)

images.append(image)

for i,img in enumerate(images):

cv2.imshow(f"img {i}",img)

stitcher = cv2.Stitcher_create()

status, stitched = stitcher.stitch(images)

# 四周填充黑色像素,再得到阈值图

stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0))

gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)

cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnt = max(cnts, key=cv2.contourArea)

mask = np.zeros(thresh.shape, dtype="uint8")

x, y, w, h = cv2.boundingRect(cnt)

cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1)

minRect = mask.copy()

sub = mask.copy()

# 开始while循环,直到sub中不再有前景像素

while cv2.countNonZero(sub) > 0:

minRect = cv2.erode(minRect, None)

sub = cv2.subtract(minRect, thresh)

cnts, hierarchy = cv2.findContours(minRect.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnt = max(cnts, key=cv2.contourArea)

x, y, w, h = cv2.boundingRect(cnt)

# 使用边界框坐标提取最终的全景图

stitched = stitched[y:y + h, x:x + w]

cv2.imshow("res",stitched)

#cv2.imwrite('final.jpg', stitched)

cv2.waitKey(0)

cv2.destroyAllWindows()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值