【问题标题】:Combine more than 1 openCV images and show them in CV2.Imshow() in OpenCV Python组合超过 1 张 openCV 图像并在 OpenCV Python 中的 CV2.Imshow() 中显示它们
【发布时间】:2017-10-05 10:07:56
【问题描述】:

我有 3 到 4 张图像,我试图将它们全部组合成一张图像(放入一个窗口中),然后通过 CV2.imshow() 函数显示它们。但问题是这个问题的每个解决方案都是针对完全相同尺寸的图像,这不是我的情况。我的图像都是不同尺寸的。请帮我解决这个问题?我有四个不同尺寸的图像,想要这样的输出

||||||||||||||||||||||||||||||||

||图片1 ||图2 ||

|||||||||||||||||||||||||||||||||

||图片1 ||图2 ||

|||||||||||||||||||||||||||||||||

目前,我有这样的代码,用于两个仅适用于大小相同的图像的图像

im = cv2.imread('1.png')
img = cv2.imread('2.jpg')
both = np.hstack((im,im))
cv2.imshow('imgc',both)
cv2.waitKey(10000)

【问题讨论】:

    标签: python opencv cv2


    【解决方案1】:

    使用 opencv 的 im.resize() 函数调整图像大小,然后进行合并任务。 始终使用参考尺寸,例如 1000 x 800(您可以更改)

    import cv2
    
    import numpy as np
    
    list_of_img_paths = [path2,path3,path4]
    
    im = cv2.imread(path1)
    
    imstack = cv2.resize(im,(1000,800))
    
    for path in list_of_img_paths:
        im = cv2.imread(path)
        im = cv2.resize(im,(1000,800))
        # hstack to join image horizontally  
        imstack = np.hstack((imstack,im))
    
    cv2.imshow('stack',imstack)
    cv2.waitKey(0)
    

    【讨论】:

    • 我得到了异常 TypeError: _vhstack_dispatcher() 需要 1 个位置参数,但给出了 2 个。当我用 imstack = np.hstack((imstack,im)) 替换 imstack = np.hstack(imstack,im) 时,它起作用了。
    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多