Home > Software design >  Tensorflow green-shifts image
Tensorflow green-shifts image

Time:10-27

I have the following code:

import tensorflow as tf
from matplotlib import pyplot as plt

def load(im1, im2):
    ima1 = tf.io.read_file(im1)
    ima1 = tf.image.decode_image(ima1)
    ima1 = tf.cast(ima1, tf.float32)

    ima2 = tf.io.read_file(im2)
    ima2 = tf.image.decode_image(ima2)
    ima2 = tf.cast(ima2, tf.float32)

    return ima1, ima2

inp, re = load(r"RAWs/1313 (1).jpg", r"Clean/1313 (1).png")

plt.figure()
plt.imshow(inp)
plt.figure()
plt.imshow(re)
plt.show()

Everything works fine, no errors, except that the second image re is greenshifted (right-most image below):

enter image description here

I have the latest version of tensorflow-cpu as I don't have a GPU due to current shortage, Python 3.9 64 Bit.

Does someone know why this is happening and how to resolve it?

Raw images: one, two, three.

CodePudding user response:

It's not a colour image, it's a single-channel greyscale image. You are looking at false colour, sometimes called pseudocolour. The imshow() function is mapping the numbers in the array to the colours in the viridis colourmap.

Colour images have 3 channels (or 4, if they have opacity too). So a NumPy array of a colour image will have shape like (h, w, 3) or (h, w, 4). This one has a single channel (shape like (h, w)). To put it another way, it's a greyscale image.

If you plot this array with plt.imshow(img, cmap='gray') it will display how you're expecting. This scheme maps zeros (or whatever the lowest number is) to black and ones (or the highest value) to white. (See all maplotlib's colourmaps here.)

  • Related