如何使用 python 方法 "ImageDataGenerator" 并将增强图像保存在变量中?
How to use the python method "ImageDataGenerator" and save the augmented images in a variable?
我正在构建一个增强数据库来改进我的 CNN。方案是:
- 我发送一张图片,每次一张,生成另外 40 张图片。
- 引用的方法将增强图像保存在一个目录中,但我想将它们保存在一个变量中,而不是先将它们保存在我的电脑中。也就是我想直接保存在一个变量中
上面的代码显示了我在说什么。看一下参数“save_to_dir”...如果我忽略它,则会进行处理,但数据不会保存在任何地方。
谁能帮帮我?
import numpy as np
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import cv2
IMAGE_PATH = "---"
OUTPUT_PATH = "---"
image = cv2.imread(IMAGE_PATH)
plt.imshow(image)
image = np.expand_dims(image, axis=0)
imgAug = ImageDataGenerator(rotation_range=360, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.20, fill_mode='wrap', horizontal_flip=True, vertical_flip=True)
imgGen = imgAug.flow(image, save_to_dir=OUTPUT_PATH,
save_format='png', save_prefix='dentezudo_')
counter = 0
for (i, newImage) in enumerate(imgGen):
counter += 1
if counter == 10:
break
函数 .flow()
returns 一个生成器,您可以迭代它(就像您在代码中所做的那样)以获取图像。在您的代码中,增强图像将分配给 newImage
.
根据the docs,flow()
也可以将图像保存到磁盘:
save_to_dir: None or str (default: None). This allows you to
optionally specify a directory to which to save the augmented pictures
being generated (useful for visualizing what you are doing).
我正在构建一个增强数据库来改进我的 CNN。方案是:
- 我发送一张图片,每次一张,生成另外 40 张图片。
- 引用的方法将增强图像保存在一个目录中,但我想将它们保存在一个变量中,而不是先将它们保存在我的电脑中。也就是我想直接保存在一个变量中
上面的代码显示了我在说什么。看一下参数“save_to_dir”...如果我忽略它,则会进行处理,但数据不会保存在任何地方。 谁能帮帮我?
import numpy as np
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import cv2
IMAGE_PATH = "---"
OUTPUT_PATH = "---"
image = cv2.imread(IMAGE_PATH)
plt.imshow(image)
image = np.expand_dims(image, axis=0)
imgAug = ImageDataGenerator(rotation_range=360, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.20, fill_mode='wrap', horizontal_flip=True, vertical_flip=True)
imgGen = imgAug.flow(image, save_to_dir=OUTPUT_PATH,
save_format='png', save_prefix='dentezudo_')
counter = 0
for (i, newImage) in enumerate(imgGen):
counter += 1
if counter == 10:
break
函数 .flow()
returns 一个生成器,您可以迭代它(就像您在代码中所做的那样)以获取图像。在您的代码中,增强图像将分配给 newImage
.
根据the docs,flow()
也可以将图像保存到磁盘:
save_to_dir: None or str (default: None). This allows you to optionally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).