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

て烟熏妆下的殇ゞ 提交于 2020-01-10 11:53:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39805697/skimage-why-does-rgb2gray-from-skimage-color-result-in-a-colored-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!