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()
相关推荐
wj3055853789 分钟前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李36 分钟前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154151 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君4 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG5 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户8356290780515 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL6 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆7 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
Miss_min7 小时前
128K长序列数据生成
开发语言·python·深度学习