【问题标题】:skimage: Why does rgb2gray from skimage.color result in a colored image?skimage:为什么 skimage.color 中的 rgb2gray 会产生彩色图像?
【发布时间】:2017-02-09 20:51:03
【问题描述】:

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

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

非灰度的彩色图像。

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

【问题讨论】:

  • 灰度转换工作正常,但 matplotlib 默认使用另一个颜色图,不是灰色阴影,而是选择蓝色和红色之间的颜色。试试imshow(cmap='Greys')。您可以检查输出,并会看到图像中最亮的部分当前是红色的,最暗的部分是蓝色的。
  • 这很有帮助。我的图像现在是灰度的。

标签: python image-processing scikit-image


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 2019-02-17
    • 2012-07-26
    • 2010-09-22
    相关资源
    最近更新 更多