step1. 假设模型输入尺寸为s,生成一幅尺寸为2s * 2s的灰色图
step2. 从点A(s/2, s/2)和点B(3s/2, 3s/2)限定的矩形内随机选择一点作为拼接点
step3. 随机选择四张图,取其部分拼入该图,如下图所示,四种颜色代表四张样本图,超出的部分将被舍弃
import numpy as np
import random
import cv2
def load_image(f, img_size):
im = cv2.imread(f) # BGR
h0, w0 = im.shape[:2] # orig hw
r = img_size / max(h0, w0) # ratio
if r != 1: # if sizes are not equal
interp = cv2.INTER_LINEAR if r > 1 else cv2.INTER_AREA
im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp)
return im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized
s = 640
yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in [-s // 2, -s // 2]) # mosaic center x, y
img4 = np.full((s * 2, s * 2, 3), 114, dtype=np.uint8) # base image with 4 tiles
img, _, (h, w) = load_image("1.jpg",s) # top left
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
img, _, (h, w) = load_image("2.jpg",s) # top right
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
img, _, (h, w) = load_image("3.jpg",s) # bottom left
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
img, _, (h, w) = load_image("4.jpg",s) # bottom right
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
cv2.imwrite("mosaic.jpg",img4)