Python把多张图拼接在一起

用到的函数是concatenate,具体代码如下:

import os

import numpy as np

import cv2

img_fold_A = './original'

img_fold_B = './gt'

img_fold_C = './prediction'

img_fold_ABC = './combination'

img_list1 = os.listdir('./original')

img_list2 = os.listdir('./gt')

img_list3 = os.listdir('./prediction')

num_imgs = len(img_list1)

for n in range(num_imgs):

    name_A = img_list1[n]

    name_B = img_list2[n]

    name_C = img_list3[n]

    path_A = os.path.join(img_fold_A, name_A)

    path_B = os.path.join(img_fold_B, name_B)

    path_C = os.path.join(img_fold_C, name_C)

    path_AB = os.path.join(img_fold_ABC, name_A)

    im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR

    im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR

    im_C = cv2.imread(path_C, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR

    im_AB = np.concatenate([im_A, im_B], 1)

    im_ABC = np.concatenate([im_AB, im_C], 1)

    cv2.imwrite(path_AB, im_ABC)

这里实现了把3张图片无缝拼接在一起。

如何在这三张图中间接入分割线呢?代码如下:

在im_C = cv2.imread(path_C, 1) 这一句后插入这一段代码即可:

    cut1 = np.ones((1024,20,3),dtype = 'u8')*255

    im_A1 = np.concatenate([im_A, cut1], 1)   

    im_AB = np.concatenate([im_A1, im_B], 1)

    im_AB1 = np.concatenate([im_AB, cut1], 1)

    im_ABC = np.concatenate([im_AB1, im_C], 1)

即可。

你可能感兴趣的:(Python把多张图拼接在一起)