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()
相关推荐
软萌萌的13 分钟前
Java Spring Boot 修改yml配置&加载顺序规则
java·spring boot·python
Dxy12393102161 小时前
Linux 编译安装 Python 3.12.10(多版本共存,不破坏系统Python)
linux·运维·python
持敬chijing2 小时前
Python概述
开发语言·python
赤羽尾风4 小时前
NumPy快速入门
python·numpy
倒流时光三十年4 小时前
第三阶段 26 · highlight 高亮(返回命中片段)
后端·python·django
久久学姐6 小时前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm
Zane19947 小时前
闭包到底"闭"住了什么?一文讲透 LEGB 规则与循环里的闭包陷阱
后端·python
Lumi_Peak7 小时前
Claude思考了42秒,我的代码质量直接提升了一个档次
python·claude
m沐沐7 小时前
【机器学习】DBSCAN聚类算法——原理、参数调优与实战
人工智能·python·深度学习·算法·机器学习·聚类·dbscan