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
Coloured image that is not in greyscale.
Why doesn't the function convert the image to greyscale?
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.