显示不同结果的Pyplot彩色地图

2024-10-02 22:24:46 发布

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

对一些scikit图像代码示例和pyplot进行了一番研究,我发现根据我如何应用彩色地图,我在图像上发现了不同的结果,我不知道为什么。在

当我应用一个颜色贴图作为一个插件显示一个图像时,一切正常,当我直接对图像做它,它变成了完全黑色。我很困惑,因为我想把彩色地图应用到我想保存(而不是显示)的图像上,但我不知道如何正确地进行。在

import matplotlib.pyplot as plt
from skimage import data
from skimage.color import rgb2hed


ihc_rgb = data.immunohistochemistry()
ihc_hed = rgb2hed(ihc_rgb)

fig, axes = plt.subplots(2, 2, figsize=(7, 6))
ax0, ax1, ax2, ax3 = axes.ravel()

ax0.imshow(ihc_hed[:, :, 0], cmap=plt.cm.gray)
ax0.set_title("plugin")

ax1.imshow(plt.cm.gray(ihc_hed[:, :, 0]))
ax1.set_title("direct")

[继续说下去]

enter image description here


Tags: from图像importdata地图pltrgb彩色
2条回答

根据您对Stefan van der Waltanswer的评论,我得出结论,您不想保存matplotlib图形,而是直接保存图像。 正如Stefan指出的,在使用colormap之前,您需要缩放这些值。Matplotlib为此提供了Normalize类:

import matplotlib.pyplot as plt
import matplotlib.colors
from skimage import data
from skimage.color import rgb2hed

ihc_rgb = data.immunohistochemistry()
ihc_hed = rgb2hed(ihc_rgb)

fig, axes = plt.subplots(1, 3, figsize=(8, 3))
ax0, ax1, ax2 = axes.ravel()

ax0.imshow(ihc_hed[:, :, 0], cmap=plt.cm.gray)
ax0.set_title("plugin")

ax1.imshow(plt.cm.gray(ihc_hed[:, :, 0]))
ax1.set_title("direct")

norm = matplotlib.colors.Normalize()
gray_image = plt.cm.gray(norm(ihc_hed[:,:,0])) 

ax2.imshow(gray_image)
ax2.set_title("direct with norm")

for a in axes:
    a.xaxis.set_visible(False)
    a.yaxis.set_visible(False)

plt.gcf().tight_layout()
plt.show()

enter image description here

ihc_hed[:, :, 0]内的值介于-1.37和-0.99之间。imshow的默认行为是显示整个值范围。^另一方面,{}对值介于0和1之间的图像进行操作。在

在第一种情况下,自动重新缩放可以节省您的时间,但第二种情况下不会。我建议始终使用固定范围的imshowimshow(image, vmin=0, vmax=1, cmap='gray')

相关问题 更多 >