为什么matplotlib会更改显示图像中的颜色强度?

2024-04-25 05:01:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一些图像处理的代码,但我面临一个奇怪的问题。你知道吗

我要显示以下已转换为0到255灰度的图像:

https://ibb.co/tqTPcpq

为此,我编写了以下代码

img_low_cont = plt.imread("hamster.png") 
plt.imshow(img_low_cont, cmap='gray')

但它展示了我的另一个形象:

https://ibb.co/Xzw3WYB

我想弄清楚为什么会这样,但我不确定。有人知道吗?你知道吗

我很抱歉没有提供更多的细节,这正是我所有的信息。我花了几个小时的时间研究女装,但没有发现任何有用的东西。你知道吗


Tags: 代码https图像imgpngplt图像处理灰度
1条回答
网友
1楼 · 发布于 2024-04-25 05:01:41

matplotlib的^{}函数的vminvmax参数的文档提供了发生了什么的重要线索:

vmin, vmax : scalar, optional

When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. vmin, vmax are ignored if the norm parameter is used.

换句话说:默认情况下,单色图像的强度会重新缩放。要防止这种情况发生,请使用输入参数v=0vmax=255,如下所示:

import matplotlib.pyplot as plt
img_low_cont = plt.imread("hamster.png") 
plt.imshow(img_low_cont, cmap='gray', vmin=0, vmax=255)

相关问题 更多 >