skimage:为什么 skimage.color 中的 rgb2gray 会产生彩色图像?

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

当我尝试使用以下方法将图像转换为灰度时:

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()

我得到了一张奇怪的彩色图像,而不是灰度图像。

手动实现该功能也给了我相同的结果。自定义函数为:

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

非灰度的彩色图像。

为什么函数不将图像转换为灰度?

生成的图像为灰度图像。但是,imshow 默认情况下使用一种热图(称为 viridis)来显示图像强度。只需指定灰度颜色图,如下所示:

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

对于所有可能的颜色图,请查看 colormap reference