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

Epi*_*lle 1 python matplotlib imshow

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

举个例子

  • 我有一张全黑图像
  • 我用imshow显示
  • 图像变黄(LUT原因)

get_pixel(x, y)-> 黄色

有没有办法编写函数 get_pixel ?

hit*_*tzg 5

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

这是一个两步过程。首先,应用归一化将值映射到区间 [0,1]。然后,颜色图从 [0,1] 映射到颜色。对于这两个步骤,matplotlib 提供了各种选项。

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

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

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()
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

和颜色:

(0.0, 0.84901960784313724, 1.0, 1.0)