如何将文件夹中存在的多个 POM 文件作为单个 Numpy ndarray 加载?
How to load mutiple PPM files present in a folder as single Numpy ndarray?
以下 Python 代码创建 numpy 数组列表。我想按数据集加载为具有维度 K x M x N x 3
的 numpy 数组,其中 K
是图像的索引, M x N x 3
是单个图像的维度。我该如何修改现有代码才能做到这一点?
image_list=[]
for filename in glob.glob(path+"/*.ppm"):
img = imread(filename,mode='RGB')
temp_img = img.reshape(img.shape[0]*img.shape[1]*img.shape[2],1)
image_list.append(temp_img)
您可以初始化该形状的输出数组,一旦进入循环,索引到第一个轴以迭代分配图像数组 -
out = np.empty((K,M,N,3), dtype=np.uint8) # change dtype if needed
for i,filename in enumerate(glob.glob(path+"/*.ppm")):
# Get img of shape (M,N,3)
out[i] = img
如果你事先不知道K
,我们可以用len(glob.glob(path+"/*.ppm"))
得到它。
以下 Python 代码创建 numpy 数组列表。我想按数据集加载为具有维度 K x M x N x 3
的 numpy 数组,其中 K
是图像的索引, M x N x 3
是单个图像的维度。我该如何修改现有代码才能做到这一点?
image_list=[]
for filename in glob.glob(path+"/*.ppm"):
img = imread(filename,mode='RGB')
temp_img = img.reshape(img.shape[0]*img.shape[1]*img.shape[2],1)
image_list.append(temp_img)
您可以初始化该形状的输出数组,一旦进入循环,索引到第一个轴以迭代分配图像数组 -
out = np.empty((K,M,N,3), dtype=np.uint8) # change dtype if needed
for i,filename in enumerate(glob.glob(path+"/*.ppm")):
# Get img of shape (M,N,3)
out[i] = img
如果你事先不知道K
,我们可以用len(glob.glob(path+"/*.ppm"))
得到它。