3

我正在使用 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()

它为所有图像添加了蓝色/绿色色调。我做错了什么吗?

4

2 回答 2

2

matplotlib.image.imreadmatplotlib.pyplot.imread将图像读取为无符号整数数组。
然后,您将其隐式转换为float.

matplotlib.pyplot.imshow以不同的方式解释两种格式的数组。

  • 浮点数组在0.0(无颜色)和1.0(全彩)之间解释。
  • 0整数数组在和之间解释255

因此,您有两个选择:

  1. 使用整数数组

    test_imgs = np.empty((5,32,32,3), dtype=np.uint8)
    
  2. 将数组除以 255。在绘图之前:

    test_imgs = test_imgs/255.
    
于 2017-02-25T12:53:35.167 回答
0

Matplotlib 以 RGB 格式读取图像,而如果您使用 opencv,它会以 BGR 格式读取图像。首先将您的 .jpg 图像转换为 RGB,然后尝试显示它。它对我有用。

于 2018-11-14T06:55:09.223 回答