我有以下的代码,使用opencv读取并显示一张图片:
import cv2, matplotlib.pyplot as plt
img = cv2.imread('imgs_soccer/soccer_10.jpg',cv2.IMREAD_COLOR)
img = cv2.resize(img, (128, 128))
plt.imshow(img)
plt.show()
我希望使用 Keras 生成一些随机图片,因此我定义了以下生成器:
image_gen = ImageDataGenerator(rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.01,
zoom_range=[0.9, 1.25],
horizontal_flip=True,
vertical_flip=False,
fill_mode='reflect',
data_format='channels_last',
brightness_range=[0.5, 1.5])
但是,当我以这种方式使用它:
image_gen.flow(img)
I get this error:
'Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (128, 128, 3))
在我看来很明显:RGB图像当然是三维的!那么我错在哪里了?文档说明它需要一个四维数组,但没有指定第四维应该放什么!
还有,这个四维数组应该如何创建?目前我拥有的是(width, height, channel)的数组,这第四维应该放在开始还是结尾?
我对numpy也不是很熟悉: 如何修改现有的img数组以添加第四个维度?