python matplotlib, get pixel value after colormap applied

喜夏-厌秋 提交于 2019-12-11 12:07:52

问题


I display an image with matplotlib using imshow(). imshow apply a LUT and I would like to retrieve a pixel value (at x, y) after LUT applied.

For exemple

  • I have a full black image
  • I display with imshow
  • The image becomes yellow (cause of LUT)

get_pixel(x, y) -> yellow

Is there a way to code the function get_pixel ?


回答1:


So, to get to the color of pixels, you have to look at how matplotlib maps the scalar values of the pixels to colors:

It is a two step process. First, a normalization is applied to map the values to the interval [0,1]. Then, the colormap maps from [0,1] to colors. For both steps matplotlib provides various options.

If you just call imshow it will apply the a basic linear normalization using the minimum and maximum of your data. The normalization class instance is then saved as an attribute of the artist. The same thing holds for the colormap.

So to compute the color of a specific pixel you have to apply these two steps manually:

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()

Result:

And the color:

(0.0, 0.84901960784313724, 1.0, 1.0)



来源:https://stackoverflow.com/questions/30645476/python-matplotlib-get-pixel-value-after-colormap-applied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!