python opencv图像拼接和显示

功能

用python的opencv读入多个图片,组成图片列表,而后将图片列表按照设定的比例拼接成一张图像。如图所示。

demo代码如下:

python 复制代码
# --coding:utf-8--
# 读取图片,汇总结果
import cv2 as cv
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import math
def show(imgs_np):
    cv.namedWindow("image")
    cv.imshow('image', imgs_np)
    cv.waitKey(0)



def cat(imgs:list,row=2,colcumn=2,resize_shape=[1920,1080]):
    len_=imgs.__len__()
    img0=imgs[0]
    img_shape=img0.shape
    img_row_first=[]
    new_imgs=[]
    # 把列表里的图片统一尺寸
    for i in range(len_):
        new_img = cv.resize(imgs[i], [img_shape[1], img_shape[0]], interpolation=cv.INTER_LINEAR)
        new_imgs.append(new_img)
    del imgs

    # 找到每一行的图片作为合并初始图片
    for i in range(row):
        img_row_first.append(new_imgs[i*colcumn])

    # 每一行内的每一列合并
    # for i in range(len_):
    #     now_col=math.floor(i/row)
    #     img_cat=img_row_first[now_col]
    #         img_cat=np.concatenate((img_cat,new_imgs[i]),axis=1)
    #         img0=np.concatenate((img2,new_img),axis=1)
    img_cat=[]
    for r in range(row):
        img_cat_row=img_row_first[r]
        for c in range(colcumn):
            if c!=0:
                img_cat_row=np.concatenate((img_cat_row,new_imgs[c]),axis=1)
        img_cat.append(img_cat_row)

    # 将所有行得到的图片合并
    img_cat0=img_cat[0]
    for i in range(len(img_cat)):
        if i != 0:
            img_cat0 = np.concatenate((img_cat0, img_cat[i]), axis=0)

    # 将最终拼接后的图片resize成需要的尺寸
    img_cat0=cv.resize(img_cat0,resize_shape,interpolation=cv.INTER_LINEAR)

    return img_cat0






if __name__=="__main__":
    # imgs = Image.open("/media/lbw/E/lbw/data/scene/datasets/test/test/2.jpg",mode='r')
    imgs1=cv.imread("/media/lbw/E/lbw/data/scene/datasets/test/rural/13.png")
    imgs2=cv.imread("/media/lbw/E/lbw/data/scene/datasets/test/rural/16.png")
    imgs3=cv.imread("/media/lbw/E/lbw/data/scene/datasets/test/rural/18.jpg")
    imgs4=cv.imread("/media/lbw/E/lbw/data/scene/datasets/test/rural/23.png")
    imgs_list=[imgs1,imgs2,imgs3,imgs4]

    # show(imgs1)
    img0=cat(imgs_list)
    show(img0)
相关推荐
hhzz6 小时前
基于监控视频的水位尺自动识别技术方案与实现
python·opencv·yolo·图像识别·cv
yongche_shi7 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
weixin_408099678 小时前
OCR批量识别图片方案:从手动处理到自动化API系统(Python/Java/PHP实战)
图像处理·python·ocr·文字识别·api调用·批量识别·石榴智能
AI行业学习8 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程9 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
云烟成雨TD9 小时前
LangFlow 1.x 系列【5】可视化编辑页面功能说明
人工智能·python·agent
geovindu10 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
tryCbest11 小时前
Python 文件操作
服务器·python
涛声依旧-底层原理研究所11 小时前
Agent 长任务可靠性设计:实现暂停、恢复、续跑与崩溃重启的完整方案
人工智能·python·系统架构
AC赳赳老秦11 小时前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析
java·开发语言·人工智能·python·github·php·openclaw