我有一个包含不同尺寸图像的文件夹。我尝试将ImageDataGenerator与flow_from_directory一起用于批量加载/数据扩充。
有没有办法保持我的图像的宽高比?似乎图像被拉伸到target_size:我想"垫"我的图像没有变形(用一个恒定的值填充间隙)
这是我的代码:
datagen = ImageDataGenerator(
rescale = 1./255,
fill_mode='constant')
generator = datagen.flow_from_directory(
'data/images',
target_size=(256,256),
color_mode = 'grayscale',
batch_size=99,
class_mode=None,
shuffle=False)
图像被拉伸到(256,256)。
答案 0 :(得分:6)
我找到了问题的答案。
当使用ImageDataGenerator / flow_from_directory时,目前无法保持宽高比,但在Github上打开pull request以添加此功能。
答案 1 :(得分:0)
根据this post,您可以使用以下代码在图像中添加填充而无需拉伸:
使用PIL
:
from PIL import Image, ImageOps
desired_size = 224
im_pth = "/home/jdhao/test.jpg"
im = Image.open(im_pth)
old_size = im.size # old_size[0] is in (width, height) format
ratio = float(desired_size) / max(old_size)
new_size = tuple([int(x * ratio) for x in old_size])
# use thumbnail() or resize() method to resize the input image
# thumbnail is a in-place operation
# im.thumbnail(new_size, Image.ANTIALIAS)
im = im.resize(new_size, Image.ANTIALIAS)
# create a new image and paste the resized on it
new_im = Image.new("RGB", (desired_size, desired_size))
new_im.paste(im, ((desired_size - new_size[0]) // 2,
(desired_size - new_size[1]) // 2))
new_im.show()
使用opencv
:
import cv2
desired_size = 224
im_pth = "data_cropped/ges_cropped_0/001019180113990401104712_frame_150.jpg"
im = cv2.imread(im_pth)
old_size = im.shape[:2] # old_size is in (height, width) format
ratio = float(desired_size) / max(old_size)
new_size = tuple([int(x * ratio) for x in old_size])
# new_size should be in (width, height) format
im = cv2.resize(im, (new_size[1], new_size[0]))
delta_w = desired_size - new_size[1]
delta_h = desired_size - new_size[0]
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
left, right = delta_w // 2, delta_w - (delta_w // 2)
color = [0, 0, 0]
new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color)
print(new_im.shape)
cv2.imshow("image", new_im)
cv2.waitKey(0)
cv2.destroyAllWindows()
在ImageDataGenerator
中,您可以使用preprocessing_function
参数在生成数据之前进行上述预处理。我知道答案来晚了,但我希望其他开发人员可以使用此代码。