使用matplotlib以灰度显示图像

时间:2023-01-15 22:55:52

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()显示灰度图像。我的问题是,灰度图像显示为colormap。我需要灰度因为我想在图像的顶部用颜色画。

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?

我在这里做错了什么?

6 个解决方案

#1


234  

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')
plt.show()

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

如果要显示逆灰度,请将cmap切换到cmap='gray_r'。

#2


23  

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

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

#3


12  

import matplotlib.pyplot as plt

进口matplotlib。pyplot作为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()

#4


8  

try this:

试试这个:

import pylab
from scipy import misc

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

#5


8  

I would use the get_cmap method. Ex.:

我将使用get_cmap方法。例:

import matplotlib.pyplot as plt

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

#6


2  

@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

在默认情况下,pl .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(): right pic

#1


234  

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')
plt.show()

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

如果要显示逆灰度,请将cmap切换到cmap='gray_r'。

#2


23  

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

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

#3


12  

import matplotlib.pyplot as plt

进口matplotlib。pyplot作为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()

#4


8  

try this:

试试这个:

import pylab
from scipy import misc

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

#5


8  

I would use the get_cmap method. Ex.:

我将使用get_cmap方法。例:

import matplotlib.pyplot as plt

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

#6


2  

@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

在默认情况下,pl .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(): right pic