python - 如何在二维数组中表示图像集?

标签 python python-3.x image image-processing pca

我正在尝试从本地计算机上的路径读取图像文件,并且我想生成一个数组来表示这些图像。如何将它们全部表示在维度为 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 postthis 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/

相关文章:

python - 如何测试文件是否包含完整路径或仅包含文件名,Python?

python:分离出 Pandas 数据框中有重复项的行

image - 如何在 MATLAB 中将图像变形为梯形

Android 应用程序 - 允许用户为按钮选择图像

python - 如何从相关矩阵和波动率向量创建协方差矩阵?

python - 精度和准确度

python - Keras log_loss 错误是一样的

python - 将日期时间字符串快速转换为秒 (Python3)

python - 我将如何解析结果并将其插入变量?

image - 在 JFreeChart 的条形图上的 X 轴标签旁边添加图像