将多副图片拼接为一幅大图片
加入白边,分割图像
白边的增加使用了遮罩层
import numpy as np
a = np.ones((10, 10))
print(a)
mask = np.ones(a.shape[:2],dtype=np.bool)
mask[2:-2, 2:-2] = 0
print(mask)
a[mask] = 0
print(a)
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
[[ True True True True True True True True True True]
[ True True True True True True True True True True]
[ True True False False False False False False True True]
[ True True False False False False False False True True]
[ True True False False False False False False True True]
[ True True False False False False False False True True]
[ True True False False False False False False True True]
[ True True False False False False False False True True]
[ True True True True True True True True True True]
[ True True True True True True True True True True]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
交换轴有点懵逼。。。。
import numpy as np
import cv2 as cv
import os
img_dir = 'D:/tmp/phone'
img_width_num = 6
img_height_num = 3
img_width_size = 150
img_height_size = 200
imgs = []
for name in os.listdir(img_dir)[:img_height_num * img_width_num]:
path = os.path.join(img_dir, name)
img = cv.imread(path)
img = cv.resize(img, (img_width_size, img_height_size))
# print(img.shape) # (200, 150, 3)
# 加上白边
mask = np.ones(img.shape[:2], dtype=np.bool)
mask[2:-2, 2:-2] = 0
img[mask] = 255
imgs.append(img)
img = np.concatenate(imgs, 0)
print(img.shape) # (3600, 150, 3)
img = img.reshape(img_height_num, img_width_num, img_height_size, img_width_size, 3)
print(img.shape, img[0, 0].shape) # (3, 6, 200, 150, 3) (200, 150, 3)
img = img.swapaxes(1, 2).reshape(img_height_size * img_height_num, img_width_size * img_width_num, 3)
print(img.shape) # (600, 900, 3)
cv.imshow('img', img)
cv.waitKey(0)
转载至链接:https://my.oschina.net/ahaoboy/blog/1932892