我正在尝试使用 ImageDataGenerator 对我的数据进行简单的数据增强。但是,由于缩放参数,我的生成器给出了失真的图像。我希望随机缩放仅放大我的数据,并在放大时对宽度和高度应用相同的缩放(以避免图像输出失真)。
gen = ImageDataGenerator(featurewise_center=False, featurewise_std_normalization=False,
samplewise_center = True, samplewise_std_normalization=True,
rotation_range=180, width_shift_range=0, height_shift_range=0,
shear_range=0, zoom_range=0.4, fill_mode='reflect',
horizontal_flip=True, rescale=1./255)
目前,生成的图像如下。
答案 0 :(得分:1)
您正在寻找的是随机裁剪:
def random_crop(image):
height, width = image.shape[:2]
random_array = numpy.random.random(size=4);
w = int((width*0.5) * (1+random_array[0]*0.5))
h = int((height*0.5) * (1+random_array[1]*0.5))
x = int(random_array[2] * (width-w))
y = int(random_array[3] * (height-h))
image_crop = image[y:h+y, x:w+x, 0:3]
image_crop = resize(image_crop, image.shape)
return image_crop
# Data generator
datagen = ImageDataGenerator(rotation_range=0.2,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=random_crop)
来源 here。