skimage:为什么skimage.color中的rgb2gray导致彩色图像? [英] skimage: Why does rgb2gray from skimage.color result in a colored image?

查看:483
本文介绍了skimage:为什么skimage.color中的rgb2gray导致彩色图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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.

这篇关于skimage:为什么skimage.color中的rgb2gray导致彩色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆