Issue
Can a numpy image (W, H) with intensity be converted to rgb format (W, H, 3) and same imshow result?
Eg, a numpy image img1 = (128,128)
with intensity (not greyscale) => convert it to img2 = (128,128,3)
. Running pyplot.imshow(img1)
and pyplot.imshow(img2)
can get the same display result.
Solution
matplotlib.imshow
uses colormaps
behind the scenes, and so can you.
- Get the appropriate color map from
matplotlib.colormaps
(use "viridis" if you aren't sure) - Apply that to your input array
Here is the Colormap documentation in case you want to learn more.
Here is some sample code that shows how to accomplish it:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
rng = np.random.default_rng(seed=42)
img1 = rng.random(size=(128, 128))
plt.imshow(img1)
plt.show()
img2 = mpl.colormaps['viridis'](img1)
plt.imshow(img2)
plt.show()
Answered By - BitsAreNumbersToo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.