我需要将数据集中的图像加载到numpy矩阵数组中,该数据集包含800张每张为64×64像素的图像。我需要将每张64×64的图像转换为矩阵的一行,该行有4096列。下面我展示了我的代码实现方式。我收到一个ValueError: cannot reshape array of size 4096 into shape (64,)。请帮帮我,谢谢。
array = np.zeros((800, 64))for i in range(800): path = “some path” img = mpimg.imread(path) array[i] = img.reshape(64)
回答:
您的原始数组应为800, 4096
的形状,因为每个子数组代表一个(64, 64)
的图像,需要4096个元素。
因此,我认为您应该这样做:
array = np.zeros((800, 4096))paths = [...] # 在此设置路径for i, path in enumerate(paths): array[i] = mpimg.imread(path).reshape(4096)