python matplotlib,应用颜色图后获取像素值

python matplotlib, get pixel value after colormap applied

我使用 imshow() 使用 matplotlib 显示图像。 imshow 应用 LUT,我想在应用 LUT 后检索像素值(在 x、y 处)。

例如

get_pixel(x, y) -> 黄色

有没有办法对函数 get_pixel 进行编码?

因此,要了解像素的颜色,您必须了解 matplotlib 如何将像素的标量值映射到颜色:

这是一个两步过程。首先,normalization is applied to map the values to the interval [0,1]. Then, the colormap 从 [0,1] 映射到颜色。对于这两个步骤,matplotlib 提供了各种选项。

如果您只调用 imshow,它将使用数据的最小值和最大值应用基本线性归一化。然后将规范化 class 实例保存为艺术家的属性。同样的事情也适用于颜色图。

因此,要计算特定像素的颜色,您必须手动应用以下两个步骤:

import matplotlib.pyplot as plt
import numpy as np

# set a seed to ensure reproducability
np.random.seed(100)

# build a random image
img = np.random.rand(10,10)

# create the image and save the artist 
img_artist = plt.imshow(img, interpolation='nearest')

# plot a red cross to "mark" the pixel in question
plt.plot(5,5,'rx', markeredgewidth=3, markersize=10)

# get the color at pixel 5,5 (use normalization and colormap)
print img_artist.cmap(img_artist.norm(img[5,5]))

plt.axis('image')
plt.show()

结果:

以及颜色:

(0.0, 0.84901960784313724, 1.0, 1.0)