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()
相关推荐
qq_2837200512 小时前
2026 最新 Python+AI 零基础入门全教程 :从零搭建人工智能完整项目
开发语言·人工智能·python
时尚IT男12 小时前
Python发票识别实战:从PDF中精准提取发票号与(小写)¥金额
开发语言·python·pdf
许彰午12 小时前
12_ArrayList与LinkedList深度对比
java·前端·python
CTA终结者13 小时前
期货量化环境装不上怎么办:天勤 TqSdk 安装与 Python 版本排查
开发语言·python
SilentSamsara13 小时前
Python 与 Docker:多阶段构建、最小镜像与健康检查
运维·开发语言·python·docker·中间件·容器
哈泽尔都13 小时前
运动控制教学——5分钟学会力控算法(阻抗/导纳/力位混合)
c++·python·算法·决策树·贪心算法·机器人·gpu算力
月疯13 小时前
PyTorch 中定义了一个 LeakyReLU 激活函数层
人工智能·pytorch·python
小白学大数据13 小时前
AI 智能爬虫实战:Selenium+Python 自动绕反爬、一键提取数据
爬虫·python·selenium·数据分析
DreamLife☼13 小时前
OpenBCI-实战二:脑波控制小游戏开发
python·pygame·openbci·cyton·ganglion