【问题标题】:Python numpy ValueError when I load image data and transfer it to an arrayPython numpy ValueError 当我加载图像数据并将其传输到数组时
【发布时间】:2016-03-24 18:20:12
【问题描述】:

我找不到解决办法。我的图像形状是128*128*3,它有三个通道,但也会导致错误

文件“E:/ML/keras_test/vgg.py”,第 30 行,在 load_data data[i,:,:,:] = arr

ValueError: 无法将输入数组从形状 (128,128) 广播到 形状 (128,128,3)

我的代码如下:

def load_data(path):
data = np.empty((12755,128,128,3),dtype="uint8")
label = np.empty((12755,),dtype="uint8")


imgs = []
imgs_name = []
for each_person in os.listdir(path):
    temp = os.path.join(path,each_person)
    for each_image in os.listdir(temp):
        imgs.append(temp + "\\" + each_image)
        imgs_name.append(each_image)  

num = len(imgs)
for i in range(num):
    img = Image.open(imgs[i])
    arr = np.asarray(img,dtype="uint8")
    print arr.shape
    data[i,:,:,:] = arr
    label[i] = int(imgs_name[i].split('.')[0])

print 'load_data is ok!' + str(data.shape[0])
return data,label

【问题讨论】:

    标签: python numpy python-imaging-library array-broadcasting


    【解决方案1】:

    您试图将原始数据放入一个小包中,这是不可能的。我认为您正在尝试将具有 RGB 通道的图像转换为具有一个通道的灰度图像。试试

    datum = (imgs.sum(axis=2) / 3).reshape((12755, -1))

    生成的datum 是一个12755 x 16384 数组。

    【讨论】:

    • 但我的图像确实有 RGB 通道。如果我使用 arr.shape 来检查它。我确定每个图像都有三个通道
    • 我的图像有三个通道,那么它应该是 16384*3 数组。所以我创建了一个具有 shpe(12755,128,128,3) 的数组。但是不知道为什么会出现这个错误?
    • 所以你想保留 3 个频道...我误解了你。试着解释一下目标数组的形状。
    猜你喜欢
    • 2017-09-21
    • 2020-06-05
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多