我正在使用matplotlib imread函数读取来自文件系统的图像。但是,当显示这些图像时,它会更改JPG图像颜色。[Python 3.5,Anaconda3 4.3,matplotlib2.0]
# reading 5 color images of size 32x32
imgs_path = 'test_images'
test_imgs = np.empty((5,32,32,3), dtype=float)
img_names = os.listdir('test_images'+'/')
for i, img_name in enumerate(img_names):
#reading in an image
image = mpimg.imread(imgs_path+'/'+img_name)
test_imgs[i] = image
#Visualize new raw images
plt.figure(figsize=(12, 7.5))
for i in range(5):
plt.subplot(11, 4, i+1)
plt.imshow(test_imgs[i])
plt.title(i)
plt.axis('off')
plt.show()
它在所有图像中添加了蓝色/绿色色调。我在做什么错误?
matplotlib.image.imread
或 matplotlib.pyplot.imread
将图像读为无符号整数数组。
然后,您将其隐含地转换为float
。
matplotlib.pyplot.imshow
以不同的方式解释两种格式的数组。
- 浮点数组在
0.0
(无颜色)和1.0
(全彩色)之间解释。 - 整数数组在
0
和255
之间解释。
您拥有的两个选项是:
-
使用整数数组
test_imgs = np.empty((5,32,32,3), dtype=np.uint8)
-
将数组除以255。在绘图之前:
test_imgs = test_imgs/255.
matplotlib以RGB格式读取图像,而如果使用OpenCV,则以BGR格式读取图像。首先在RGB中转换您的.jpg图像,然后尝试显示它。它对我有用。