问题
The following Python code creates list of numpy array. I want to load by data sets as a numpy array that has dimension K x M x N x 3
, where K
is the index of the image and M x N x 3
is the dimension of individual image. How can I modify the existing code to do so ?
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)
回答1:
You could initialize an output array of that shape and once inside the loop, index into the first axis to assign image arrays iteratively -
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
If you don't know K
beforehand, we could get it with len(glob.glob(path+"/*.ppm"))
.
来源:https://stackoverflow.com/questions/46046459/how-to-load-mutiple-ppm-files-present-in-a-folder-as-single-numpy-ndarray