There were some points that I was a little worried about when displaying images with matplotlib, so I have summarized them. I'm sure there are people who are interested (especially those who are starting to use it), so I hope I can save the trouble of searching even a little. I use jupyter notebook a lot, so I assume that it will be used there.
seeing is believing. Let's display the image for the time being.
python
#The usual various imports
import cv2
import numpy as np
from matplotlib import pyplot as plt
im = np.array(cv2.imread('lena.png',0)) #Load the image and put it in numpy format
cv2.imread ('lena.png', 0)
means to read "lena.png " to 0, that is, in grayscale.
Now let's display the image with matplotlib.
python
#Display with matplotlib
plt.imshow(im)
plt.show()
Then it will be displayed like this.
Somehow it is colored.
Since matplotlib has a color map set by default, it looks like this.
To release this, just insert plt.gray ()
.
python
plt.gray() #Color map"gray"Set to
plt.imshow(im)
plt.show()
It was as expected. Actually, there is information in various places so far, but I have thought this way.
I couldn't find it unexpectedly, and I thought, "I wonder if anyone wants to get it back ...". I didn't know what the default colormap was, so I gave up trying to restore it, but it was properly written in Official.
This * viridis * seems to be the default colormap. Let's set it immediately.
python
plt.viridis()
plt.imshow(im)
plt.show()
Now it's back. Congratulations.
I think it was a miracle that I noticed this, but I thought that the color was a little strange. So, I tried to load such an image in the test. It's Makuro. … But when it is displayed by matplotlib
It seems that the color gradation is normalized. In order to solve this, it seems that vmin and vmax must be specified as shown below.
python
plt.imshow(im, vmin = 0, vmax = 255)
If you squint, you can see it slightly.
I'm sure there are other default settings that I'm not aware of. that's all. Please let me know if you have any mistakes.
Recommended Posts