OpenCV单窗口并排显示多张图片

OpenCV单窗口并排显示多张图片

  • 效果
  • 代码

PS:本例的代码适合图片的宽度和高度都相同。

效果

原始三张图片:


合并显示:

代码

python 复制代码
import cv2
import numpy as np


def opencv_multi_img():
    # 读取图片
    img1 = cv2.imread('saw_1.jpeg')
    img2 = cv2.imread('saw_2.jpeg')
    img3 = cv2.imread('saw_3.jpeg')

    # 检查图片是否成功加载
    if img1 is None or img2 is None or img3 is None:
        print("Error: Unable to load one or more images.")
        return

        # 获取图片的高度和宽度
    h1, w1, _ = img1.shape
    h2, w2, _ = img2.shape
    h3, w3, _ = img3.shape

    # 确保所有图片的高度相同
    if h1 != h2 or h1 != h3:
        print("Error: Images must have the same height.")
        return

        # 创建一个空白的大画布
    max_width = w1 + w2 + w3  # 三张图片的总宽度
    canvas = np.zeros((h1, max_width, 3), dtype=np.uint8)

    # 将图片放置到画布上
    canvas[:, :w1] = img1
    canvas[:, w1:w1 + w2] = img2
    canvas[:, w1 + w2:w1 + w2 + w3] = img3

    # 展示多个图片
    cv2.imshow("multi_img", canvas)

    # 等待用户按键关闭窗口
    cv2.waitKey(0)
    cv2.destroyAllWindows()


# 调用函数
opencv_multi_img()
相关推荐
小小测试开发6 小时前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想6 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波7 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
copyer_xyf7 小时前
Python 异常处理
前端·后端·python
麻雀飞吧8 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy8 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.8 小时前
【01】Python 机器学习
开发语言·python
为爱停留9 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python
l1t9 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python
曾阿伦9 小时前
Python 搭建简易HTTP服务
开发语言·python·http