【发布时间】:2018-06-05 14:18:14
【问题描述】:
我想将多个 RGB 图像读取到一个 numpy 数组。我所有的图像都是分辨率(32,32,3)。我的文件夹中有 10 张图像,我希望我的最终“图像”numpy 数组为 (10, 32, 32, 3)。我试过下面的代码。
import matplotlib.image as mpimg
import os
folder = 'test_images'
images = np.array([(mpimg.imread(os.path.join(folder, filename))) for filename in os.listdir('test_images')], dtype='uint8')
我得到以下错误
ValueError Traceback (most recent call last)
<ipython-input-109-0c5d51212e48> in <module>()
3
4 folder = 'test_images'
----> 5 images = np.array([(mpimg.imread(os.path.join(folder, filename))) for filename in os.listdir('test_images')], dtype='uint8')
6
7 print(len(images))
ValueError: could not broadcast input array from shape (32,32,3) into shape (32,32)
【问题讨论】:
-
您的意思是生成的形状应该是 (10, 32, 32, 3) 还是真的是 (10, 32, 32, 5)?如果是第二个,那为什么是 5 个频道?
-
@BloodyD 我的错误应该是 (10, 32, 32, 3)。我已经改正了
-
一件事可能是,一些图像被加载为灰度图像。尝试使用 PIL 加载图像并将其转换为 RGB:np.asarray(Image.open(imname).convert("RGB"))
-
@BloodyD 我按照您的建议将图像阅读库更改为 cv2 和 PIL。它工作正常,可能是这个库返回的结构有问题
-
是的,我认为 matplotlib 会读取图像的一些元数据来确定是将图像加载为灰度还是 RGB。如果您确实知道您的图像都是 RGB 有时会有所帮助,但在这种情况下,如您所见,需要进行一些手动读取和转换
标签: numpy matplotlib