Search code examples
pythonnumpymachine-learningkerasgrayscale

Channel width not displayed for data augmentation of Grayscale image


I have a grayscale image and want to perform augumentation methods using Keras. Problem: After importing the image, it is missing the channel width from it's dimension and thus facing a problem for 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


I get the following error

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

How do I add the extra channel axis to the dimension? Is there any other solution for Data Augumentation of the grayscale image?


Solution

  • Just add a dimension to the image with 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)
    

    Original image: enter image description here

    Augmented examples: enter image description here enter image description here enter image description here