def data_augmentation(image): # 随机旋转 angle = np.random.randint(-10, 10) image = ndimage.rotate(image, angle, reshape=False)

# 随机平移
t_x, t_y = np.random.randint(-10, 10, size=(2,))
rows, cols = image.shape
M = np.float32([[1, 0, t_x], [0, 1, t_y]])
image = cv2.warpAffine(image, M, (cols, rows))

# 随机裁剪
crop_size = np.random.randint(280, 300)
if crop_size < rows and crop_size < cols:
    x = np.random.randint(0, cols - crop_size)
    y = np.random.randint(0, rows - crop_size)
    image = image[y:y+crop_size, x:x+crop_size]

# 随机翻转
flip = np.random.randint(0, 2)
if flip:
    image = cv2.flip(image, 1)

# 随机色彩变化
hsv_image = cv2.cvtColor(image, cv2.COLOR_GRAY2HSV)
h, s, v = cv2.split(hsv_image)
h = h.astype(np.float32)
s = s.astype(np.float32)
v = v.astype(np.float32)
h += np.random.randint(-10, 10)
s += np.random.randint(-10, 10)
v += np.random.randint(-10, 10)
h = np.clip(h, 0, 255).astype(np.uint8)
s = np.clip(s, 0, 255).astype(np.uint8)
v = np.clip(v, 0, 255).astype(np.uint8)
hsv_image = cv2.merge([h, s, v])
image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2GRAY)

return image
# 数据增强函数def data_augmentationimage # 随机旋转 angle = nprandomrandint-10 10 image = ndimagerotateimage angle reshape=False # 随机平移 t_x t_y = nprandomrandint-10 10 size=2 rows cols = image

原文地址: https://www.cveoy.top/t/topic/bnyA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录