【发布时间】: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