将图像显示为灰度
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
以下代码将从文件
image.png
加载图像并将其显示为灰度。如果要显示反灰度,请将 cmap 切换为
cmap='gray_r'
。The following code will load an image from a file
image.png
and will display it as grayscale.If you want to display the inverse grayscale, switch the cmap to
cmap='gray_r'
.尝试使用灰度色彩图?
例如,
有关颜色图列表,请参阅 http://scipy-cookbook.readthedocs.org /items/Matplotlib_Show_colormaps.html
Try to use a grayscale colormap?
E.g. something like
For a list of colormaps, see http://scipy-cookbook.readthedocs.org/items/Matplotlib_Show_colormaps.html
import matplotlib.pyplot as plt
您也可以在代码中运行一次
这将默认以灰度显示图像
import matplotlib.pyplot as plt
You can also run once in your code
This will show the images in grayscale as default
我会使用 get_cmap 方法。前任。:
I would use the get_cmap method. Ex.:
@unutbu 的答案非常接近正确答案。
默认情况下,plt.imshow() 会尝试将 (MxN) 数组数据缩放到 0.0~1.0。然后映射到0~255。对于大多数自然拍摄的图像来说,这很好,您不会看到任何不同。但如果你的图像像素值范围很窄,比如最小像素是 156,最大像素是 234。灰度图像看起来会完全错误。
以灰色显示图像的正确方法是
让我们看一个例子:
这是原始图像:
原始
这是使用默认规范设置,即无:
错误图片
这是使用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
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
试试这个:
try this:
不使用插值并设置为灰色。
Use no interpolation and set to gray.
应该有效。但是,这种方法的问题在于它不是真正的灰色。它仅将 RGB 通道之一更改为灰色。
看下面。
[结果图像]
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.
[Result images]
当图像有紫色和紫色时黄色。
更改图像保存方式:
plt.imsave(...., cmap='gray')
When the image has purple & yellow color.
change way of saving image:
plt.imsave(...., cmap='gray')