Search code examples
pythonpython-3.ximagenumpyscikit-image

Python skimage image with one value for color definition


I just started working with skimage and I am using it in python.3.6 with the skimage-version: 0.17.2
And I started using the example form: https://scikit-image.org/docs/stable/user_guide/numpy_images.html
There I found something that I did not understand. The color of this image is only defined by one singe value in the numpy-array. How can that be? Is it nit using RBG or something like this? My code looks like this:

from skimage import data

camera = data.camera()
print(camera.shape)
print(camera[0,0])

And the output is:

(10, 100)
0.0

What is driving me crazy is the 0.0 shouldn't it be something like [0,0,0] for white in this example ?

When I show the image I get following result :image i get
Can anybody please help me ?


Solution

  • The color is defined by a single value because it's not RGB, it's greyscale. So the image shape is (512, 512), and not (512, 512, 3). As a result, if you pick a single white point it will be [255] and not [255, 255, 255].

    If you're confused because the picture isn't black and white, it's just because the default colormap of matplotlib is viridis, so green and yellow. This doesn't change the pixel values, it's essentially just a "theme" or camera filter. If you changed the colormap to grays, you will get:

    import matplotlib.pyplot as plt
    
    plt.imshow(255 - camera, cmap='Greys')
    plt.show()
    

    enter image description here

    If you don't specify the colormap, even a random array of pixels will get the yellowish tint:

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots(figsize=(4, 4))
    plt.imshow(np.random.randint(0, 256, (512, 512)))
    plt.show()
    

    enter image description here

    There is one thing I don't understand, though. The pic seems inverted. I had to subtract the pixel values from 255 to get the normal camera pic.