问题
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