Keras ImageDataGenerator messing up with grey images, how to correctly augment greyscale image?

Viewed 1658

I am analysing 16bit grey scale images. After uploading greyscale image, everything looks fine.

import matplotlib.pyplot as plt
import numpy as np
img = plt.imread('example_image.tif')
plt.imshow(img)
plt.show()

Or one can also do

import cv2
img = cv2.imread('example_image.tif', -cv2.IMREAD_ANYDEPTH)
img1 = np.expand_dims(img, axis=2)
img2 = np.reshape(img1,[1,696,520,1])
plt.imshow(img)
plt.show()

When I checked my augmented images, they looks distorted. What am I doing wrong?

img1 = np.expand_dims(img, axis=2)
img2 = np.reshape(img1,[1,696,520,1])
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
    rescale=1. / 4095,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

i=0
for batch in datagen.flow(img2, batch_size=1,
                          save_to_dir='C:/image_analysis', save_prefix='grey_check_', save_format='tif'):
    i += 1
    if i > 20:
        break

enter image description here enter image description here

1 Answers

replace this line:

img2 = np.reshape(img1,[1,696,520,1])

on this:

img2 = np.reshape(img1,[1,520,696,1])
Related