灰度图像的数据增强不显示通道宽度

Channel width not displayed for data augmentation of Grayscale image

我有一张灰度图像,想使用 Keras 执行增强方法。 问题:导入图像后,它的尺寸缺少通道宽度,因此面临 ImageDataGenerator 的问题。

#importing libraries 

import keras 
from keras import backend as K
import imageio
from keras.preprocessing.image import ImageDataGenerator
from skimage import io
from skimage import color
import numpy as np
from scipy import misc, ndimage


# Reading image

img = io.imread('img1.png')
img = img.reshape((1, ) + img.shape )  #reshaping the existing (height, width) dimension to (1, height, width)


# ImageDataGenerator class for augumentation

datagen = ImageDataGenerator(
        rotation_range=45, 
        width_shift_range=0.2, 
        height_shift_range=0.2, 
        shear_range=0.2, 
        zoom_range=0.2, 
        horizontal_flip=True,
        fill_mode='constant', cval=255)


# Creating an iterator for datagen.flow (we use this since currently working only on 1 image)

i = 0
for batch in datagen.flow(img, batch_size=5, save_to_dir="augumented", save_prefix="aug", save_format="png"):
    i += 1
    if i>20:
        break


我收到以下错误

Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (1, 2054, 2456)

如何将额外的通道轴添加到维度?灰度图的Data Augumentation还有其他解决方案吗?

只需使用 tf.expand_dims 为图像添加尺寸:

img = io.imread('/content/result_image.png')
img = img.reshape((1, ) + img.shape )  #reshaping the existing (height, width) dimension to (1, height, width)
img = tf.expand_dims(img, axis=-1)

原图:

增强示例: