将图像显示为灰度

发布于 2024-09-25 14:13:50 字数 464 浏览 9 评论 0原文

我正在尝试使用 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 it to be 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

﹎☆浅夏丿初晴 2024-10-02 14:13:50

以下代码将从文件 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'.

不甘平庸 2024-10-02 14:13:50

尝试使用灰度色彩图?

例如,

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

奢望 2024-10-02 14:13:50

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()
无尽的现实 2024-10-02 14:13:50

我会使用 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'))
傲娇萝莉攻 2024-10-02 14:13:50

@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

莫相离 2024-10-02 14:13:50

试试这个:

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()
旧人哭 2024-10-02 14:13:50

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

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')
人生戏 2024-10-02 14:13:50
plt.imshow(img[:,:,0], cmap='gray')

plt.imshow(img[:,:,1], cmap='gray')

plt.imshow(img[:,:,2], cmap='gray')

应该有效。但是,这种方法的问题在于它不是真正的灰色。它仅将 RGB 通道之一更改为灰色。

看下面。

from sklearn.datasets import load_sample_image
flower = load_sample_image("flower.jpg")

plt.subplot(1,4,1)
plt.imshow(flower)
plt.axis("off")
plt.title("Original")

# R level to gray
plt.subplot(1,4,2)
plt.imshow(flower[:,:,0], cmap='gray')
plt.axis("off")
plt.title("R to gray")


# G leval to gray
plt.subplot(1,4,3)
plt.imshow(flower[:,:,1], cmap='gray')
plt.axis("off")
plt.title("R to gray")

# B leval to gray
plt.subplot(1,4,4)
plt.imshow(flower[:,:,2], cmap='gray')
plt.axis("off")
plt.title("R to gray")

plt.show()

[结果图像]

plt.imshow(img[:,:,0], cmap='gray')

plt.imshow(img[:,:,1], cmap='gray')

plt.imshow(img[:,:,2], cmap='gray')

should work. But, the issue with this approach is that it is not true gray. It only changes one of RGB channel to gray.

look below.

from sklearn.datasets import load_sample_image
flower = load_sample_image("flower.jpg")

plt.subplot(1,4,1)
plt.imshow(flower)
plt.axis("off")
plt.title("Original")

# R level to gray
plt.subplot(1,4,2)
plt.imshow(flower[:,:,0], cmap='gray')
plt.axis("off")
plt.title("R to gray")


# G leval to gray
plt.subplot(1,4,3)
plt.imshow(flower[:,:,1], cmap='gray')
plt.axis("off")
plt.title("R to gray")

# B leval to gray
plt.subplot(1,4,4)
plt.imshow(flower[:,:,2], cmap='gray')
plt.axis("off")
plt.title("R to gray")

plt.show()

[Result images]

转身泪倾城 2024-10-02 14:13:50

当图像有紫色和紫色时黄色。

更改图像保存方式:
plt.imsave(...., cmap='gray')

When the image has purple & yellow color.

change way of saving image:
plt.imsave(...., cmap='gray')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文