masaic数据增强代码:
四张图片随机拼接
输出结果:随机拼接
'''
数据增强方法测试
1. 像素级:HSV增强、旋转、平移、缩放、剪切、透视、翻转等
2. 图片级:MixUp、Cutout、CutMix、Mosaic、Copy-Paste等
'''
import math
import os
import random
import cv2
import numpy as np
# 将图像的最长边缩放到640,短边填充到640
def fix_shape(imgs, new_shape=(640, 640), color=(114, 114, 114)):
new_imgs = []
for img in imgs:
shape = img.shape[:2] # current shape [height, width]
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
new_imgs.append(img)
return new_imgs
# 将xywh形式的标签信息转化为xyxy形式
def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
# Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
y = np.copy(x)
y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x
y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y
y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x
y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y
return y
# 绘制带有GT框图像
def plot_box(img, label, line_thickness=3):
colors = [[np.random.randint(0, 255) for _ in range(3)] for _ in range(len(label))]
for i, l in enumerate(label):
color = colors[i % len(colors)]
# tl = 框框的线宽 要么等于line_thickness要么根据原图im长宽信息自适应生成一个
tl