Matplotlib 更改 jpg 图像颜色

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()

它给所有图像添加了一种蓝绿色的色调。我做错了什么吗?
2个回答

2
matplotlib.image.imreadmatplotlib.pyplot.imread会将图像读取为无符号整数数组。
您随后将其隐式转换为floatmatplotlib.pyplot.imshow对这两种格式的数组进行不同的解释。
  • float数组被解释为介于0.0(没有颜色)和1.0(完整颜色)之间。
  • integer数组被解释为介于0255之间。
因此,您有两个选项:
  1. Use an integer array

    test_imgs = np.empty((5,32,32,3), dtype=np.uint8)
    
  2. divide the array by 255. prior to plotting:

    test_imgs = test_imgs/255.
    

我使用了以下代码: test_imgs[i] = (mpimg.imread(imgs_path+'/'+img_name)*255).astype('uint8') - Shah-G
我觉得这没有意义。imread函数会给你一个整数类型的图像,其值介于0和255之间。如果你再将其乘以255,你将得到非常大的值。 - ImportanceOfBeingErnest

0

Matplotlib 读取的图像格式为 RGB,而如果使用 OpenCV,则读取的图像格式为 BGR。 首先将您的 .jpg 图像转换为 RGB 格式,然后尝试显示它。 这对我起作用了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接