Python 使用 matplotlib 将图像显示为灰度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3823752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:55:04  来源:igfitidea点击:

Display image as grayscale using matplotlib

pythonmatplotlibgrayscale

提问by Ryan

I'm trying to display a grayscale image using matplotlib.pyplot.imshow(). My problem is that the grayscale image is displayed as a colormap. I need the grayscale because I want to draw on top of the image with color.

我正在尝试使用matplotlib.pyplot.imshow()显示灰度图像。我的问题是灰度图像显示为颜色图。我需要灰度,因为我想用颜色在图像上绘制。

I read in the image and convert to grayscale using PIL's Image.open().convert("L")

我读入图像并使用PIL 的 Image.open().convert("L")转换为灰度

image = Image.open(file).convert("L")

Then I convert the image to a matrix so that I can easily do some image processing using

然后我将图像转换为矩阵,以便我可以轻松地使用

matrix = scipy.misc.fromimage(image, 0)

However, when I do

但是,当我这样做时

figure()  
matplotlib.pyplot.imshow(matrix)  
show()

it displays the image using a colormap (i.e. it's not grayscale).

它使用颜色图显示图像(即它不是灰度)。

What am I doing wrong here?

我在这里做错了什么?

采纳答案by unutbu

The following code will load an image from a file image.pngand will display it as grayscale.

以下代码将从文件加载图像image.png并将其显示为灰度。

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

fname = 'image.png'
image = Image.open(fname).convert("L")
arr = np.asarray(image)
plt.imshow(arr, cmap='gray', vmin=0, vmax=255)
plt.show()

If you want to display the inverse grayscale, switch the cmap to cmap='gray_r'.

如果要显示反灰度,请将cmap切换为cmap='gray_r'

回答by janneb

Try to use a grayscale colormap?

尝试使用灰度颜色图?

E.g. something like

例如类似的东西

imshow(..., cmap=pyplot.cm.binary)

For a list of colormaps, see http://scipy-cookbook.readthedocs.org/items/Matplotlib_Show_colormaps.html

有关颜色图列表,请参阅http://scipy-cookbook.readthedocs.org/items/Matplotlib_Show_colormaps.html

回答by baco

try this:

尝试这个:

import pylab
from scipy import misc

pylab.imshow(misc.lena(),cmap=pylab.gray())
pylab.show()

回答by Eliel Van Hojman

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

You can also run once in your code

您也可以在代码中运行一次

plt.gray()

This will show the images in grayscale as default

这将默认以灰度显示图像

im = array(Image.open('I_am_batman.jpg').convert('L'))
plt.imshow(im)
plt.show()

回答by Al Conrad

I would use the get_cmap method. Ex.:

我会使用 get_cmap 方法。前任。:

import matplotlib.pyplot as plt

plt.imshow(matrix, cmap=plt.get_cmap('gray'))

回答by Stanley

@unutbu's answer is quite close to the right answer.

@unutbu 的答案非常接近正确答案。

By default, plt.imshow() will try to scale your (MxN) array data to 0.0~1.0. And then map to 0~255. For most natural taken images, this is fine, you won't see a different. But if you have narrow range of pixel value image, say the min pixel is 156 and the max pixel is 234. The gray image will looks totally wrong. The right way to show an image in gray is

默认情况下,plt.imshow() 会尝试将您的 (MxN) 数组数据缩放到 0.0~1.0。然后映射到0~255。对于大多数自然拍摄的图像,这很好,您不会看到任何不同。但是如果你的像素值范围很窄,比如最小像素为 156,最大像素为 234。灰色图像看起来完全错误。以灰色显示图像的正确方法是

from matplotlib.colors import NoNorm
...
plt.imshow(img,cmap='gray',norm=NoNorm())
...

Let's see an example:

让我们看一个例子:

this is the origianl image: original

这是原始图像:原始图像

this is using defaul norm setting,which is None: wrong pic

这是使用默认规范设置,即无: 错误图片

this is using NoNorm setting,which is NoNorm(): right pic

这是使用 NoNorm 设置,即 NoNorm(): 右图

回答by user3452134

Use no interpolation and set to gray.

不使用插值并设置为灰色。

import matplotlib.pyplot as plt
plt.imshow(img[:,:,1], cmap='gray',interpolation='none')