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()
相关推荐
65岁退休Coder2 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
ikun_文4 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes5 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明5 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn5 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困5 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
欧特克_Glodon6 小时前
OpenCV计算机视觉开发入门与实践<二十七>:图像分割概述
c++·人工智能·opencv·计算机视觉
alphaTao6 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼6 小时前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君6 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python