如何在 numpy 数组中附加图像?

How to append images in numpy array?

我正在尝试用这种形状制作一个 numpy(number_of_images,32,32) 所有图像都具有相同的尺寸:32,32 这是我的代码:

data=np.array([])
for filename in glob.glob(path+'*.tif'): 
    im = np.array([np.array(cv2.imread(filename, cv2.IMREAD_GRAYSCALE))])
    #im = preprocess(im)
    im = NormalizeData(im)
    data=np.append(data,im)

我的形状是(1,32,32)。但是数据形状不是我想要的 (number_of_images,32,32)。我目前拥有的是 (113664,)

尝试使用列表并在最后转换为 numpy - 减少混淆

def main():
    cv_img_fake = np.zeros(shape=(1, 32, 32), dtype=np.uint8)
    print('image shape {}'.format(cv_img_fake.shape))

    images = []
    for filename_i in range(10):  # imagine 10 images
        print('filename {}:'.format(filename_i))
        im = cv_img_fake.copy()  # shape 1,32,32
        print('\timage shape {}'.format(im.shape))
        im = im.reshape(-1, im.shape[-1])  # shape 32,32
        print('\timage shape {}'.format(im.shape))

        # do to im whatever you want except changing the dims
        # check after every function by printing dims didn't change - e.g. still 32,32
        # im = NormalizeData(im)
        # print('\timage shape {}'.format(im.shape))
        # im = preprocess(im)
        # print('\timage shape {}'.format(im.shape))
        images.append(im)
    images = np.uint8(images)  # 10 images of 32, 32
    print('images shape {}'.format(images.shape))  # 10,32,32
    return

输出:

image shape (1, 32, 32)
filename 0:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 1:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 2:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 3:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 4:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 5:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 6:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 7:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 8:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 9:
    image shape (1, 32, 32)
    image shape (32, 32)
images shape (10, 32, 32)