kwotsin
kwotsin

Reputation: 2923

skimage: Why does rgb2gray from skimage.color result in a colored image?

When I tried to convert the image to gray scale using:

from skimage.io import imread
from skimage.color import rgb2gray
mountain_r = rgb2gray(imread(os.getcwd() + '/mountain.jpg'))

#Plot
import matplotlib.pyplot as plt
plt.figure(0)
plt.imshow(mountain_r)
plt.show()

I got a weird colored image instead of a gray scale.

Manually implementing the function also gives me the same result. The custom function is:

def rgb2grey(rgb):
    if len(rgb.shape) is 3:
        return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

    else:
        print 'Current image is already in grayscale.'
        return rgb

Original

Coloured image that is not in greyscale. gray

Why doesn't the function convert the image to greyscale?

Upvotes: 25

Views: 26245

Answers (1)

Tony Power
Tony Power

Reputation: 1178

The resulting image is in grayscale. However, imshow, by default, uses a kind of heatmap (called viridis) to display the image intensities. Just specify the grayscale colormap as shown below:

plt.imshow(mountain_r, cmap="gray")

For all the possible colormaps, have a look at the colormap reference.

Upvotes: 39

Related Questions