OpenCV-Python - Multi-image stitching and saving

Function function: It is suitable for multi-image stitching of images of the same size or different sizes, and save them

def show_multi_images():
    """
    imgname_laser: image
    imgname_lidar:image
    """
    # 读取图片
    img1 = cv2.imread("16296400010513_2022-09-20_1663677464_1663678190_laser.png")
    img2 = cv2.imread('16296400010513_2022-09-20_1663677464_1663678190_laser1.png')

    # 开始图像拼接流程
    h1, w1, _ = img1.shape
    h2, w2, _ = img2.shape
    print(img1.shape)
    print(img2.shape)
    if h1 != h2 or w1 != w2:
        # 如果图片的至少一个维度大小不一致,则根据图的
        # 最大宽、高重新生成空图,然后将各自的图复制到
        # 指定区域
        max_h = max(h1, h2)
        max_w = max(w1, w2)

        # 初始化空的大图,可以容纳所有要拼接的图
        new_img = np.zeros((max_h, 2*max_w, 3))  # 横向并排显示多列
        # new_img = np.zeros((2*max_h, max_w, 3))  # 纵向并排显示多行
        print(new_img.shape)

        # 横向并排拼接:根据各自图的大小,分别放置到指定区域
        new_img[0:h1, 0:w1, :] = img1
        new_img[0:h2, w1:(w1+w2), :] = img2
    else:
        # 图片大小一致,则可以直接拼接,axis=1 横向拼接
        new_img = np.concatenate((img1, img2), axis=1)

    cv2.imwrite("new.png", new_img)
       

Guess you like

Origin blog.csdn.net/kxh123456/article/details/130976354