python实现将一幅图拼接到另一幅图上

# Standard imports
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
# Read images
#准备拼接的图
input_dir='./cai_jian'#拼接图的文件夹
dst = cv2.imread("./yuan_tu/0.jpg")#被拼接的图
# Create a rough mask around the airplane.
#src_mask = np.zeros(src.shape, src.dtype)
# 当然我们比较懒得话,就不需要下面两行,只是效果差一点。
for filename in os.listdir(input_dir):
    path=input_dir+'/'+filename
    src = cv2.imread(path)
    sp= src.shape
    h=sp[0]#高
    w=sp[1]#宽
    print(filename,w, h)
    if h>w:
        max=h
        min=w
    else:
        max=w
        min=h
    print(max)
    if max<180:
        src_mask = 255 * np.ones(src.shape, src.dtype) #<-- 全白
        #poly = np.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ], np.int32)
        #cv2.fillPoly(src_mask, [poly], (255, 255, 255))
        # 这是CENTER 所在的地方
        center = (90,90)
        # Clone seamlessly.
        output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
        # 保存结果
        cv2.imwrite("./pin_jie/"+filename, output)
    else:
        scale=180/max
        src1=cv2.resize(src,(int(w * scale), int(h * scale)))
        #plt.subplot(2, 2, 1),plt.imshow(src)
        #plt.subplot(2, 2, 2),plt.imshow(src1)
        plt.show()
        src_mask = 255 * np.ones(src1.shape, src1.dtype)  # <-- 全白
        # poly = np.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ], np.int32)
        # cv2.fillPoly(src_mask, [poly], (255, 255, 255))
        # 这是准备拼接图所在的中心位置
        center = (90, 90)
        # Clone seamlessly.
        output = cv2.seamlessClone(src1, dst, src_mask, center, cv2.NORMAL_CLONE)
        # 保存结果
        cv2.imwrite("./pin_jie/" + filename, output)

效果:
原图:
在这里插入图片描述
拼接图:
在这里插入图片描述
拼接后:
在这里插入图片描述
这里被拼接的尺寸是180*180的,如果拼接图尺寸比这个大,需要根据相应程序中的代码去挑合适的比例进行缩放。
参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值