如何使用keras将列表中的每个灰度图像转换为2d阵列



我有一个由几个排序的灰度图像组成的列表,称为imageList。我想将每个图像转换为2d数组,并将每个图像按转换后的顺序存储在一个名为arrayList的列表中。像这样:

imageList = ['1.png', '2.png', '3.png', '4.png',....,'1000.png']

arrayList = [[1th_2dArray] , [2th_2dArray], ......, [1000th_2dArray]]

请注意,我调整了图像的大小,并将它们从RGB转换为灰度,然后将它们存储在我的imageList中。

p.S:我是python的新手,我知道要将图像转换为数组,我可以使用其他方法,如Pillow或OpenCV Library,任何建议都将不胜感激。

IIUC,您有一个要转换为数组的图像的路径列表,所以您可以尝试这样的方法:

import tensorflow as tf
import numpy as np
image_list = ['/content/1.png', '/content/2.png', '/content/3.png']
array_list = [np.squeeze(tf.keras.preprocessing.image.img_to_array(tf.keras.preprocessing.image.load_img(path, color_mode='grayscale')), axis=-1) for path in image_list]
print(array_list[0].shape)
(100, 100)

因此,每个图像都被加载,然后转换为一个数组。之后,通道尺寸被省略,从而产生2D阵列。

最新更新