我正在尝试从本地计算机上的路径读取图像文件,并且我想生成一个数组来表示这些图像。如何将它们全部表示在维度为 2 的数组中。
images = [imageio.imread(path) for path in glob.glob([pathtoimages])]
images = np.asarray(images)
print(images.shape)
scaler = StandardScaler()
# Fit on training set only.
scaler.fit(images)
#
## Apply transform to both the training set and the test set.
#train_img = scaler.transform(images)
我正在关注this guide对一组全为 257x257 的图像进行 PCA。当我执行 print(images.shape) 时,我得到 (130, 257, 257, 3),因为有 130 个 257x257 的图像,有 3 个 channel 。当我尝试执行 StandardScaler 时,出现以下错误。
ValueError: Found array with dim 4. StandardScaler expected <= 2.
我的主要问题是如何将大小为 4 的数组压缩为只有 2 维的数组?我已经有this post和 this one ,但我仍然不确定。
此外,请确保在运行代码时替换 glob.glob() 函数中的 [pathtoimages]。
最佳答案
您需要先拼合图像,然后再将其添加到列表中。所以你的尺寸为 (257,257,3) 的图像将变成尺寸为 257*257*3 的一维数组
images = [np.array(imageio.imread(path)).flatten() for path in glob.glob(pathtoimages)]
images = np.array(images)
print(images.shape)
关于python - 如何在二维数组中表示图像集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636536/