问题:使用matplotlib将图像显示为灰度

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

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

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

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

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

但是,当我这样做

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

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

我在这里做错了什么?

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.

I read in the image and convert to grayscale using PIL’s 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?


回答 0

以下代码将从文件中加载图像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()

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

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

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'.


回答 1

尝试使用灰度色图?

例如类似

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

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

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


回答 2

import matplotlib.pyplot as plt

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

plt.gray()

默认情况下,它将以灰度显示图像

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

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

回答 3

我会使用get_cmap方法。例如:

import matplotlib.pyplot as plt

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

I would use the get_cmap method. Ex.:

import matplotlib.pyplot as plt

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

回答 4

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

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

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

让我们来看一个例子:

这是原始图片: 原始

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

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

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

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

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


回答 5

试试这个:

import pylab
from scipy import misc

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

try this:

import pylab
from scipy import misc

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

回答 6

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

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

Use no interpolation and set to gray.

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。