Given a list with 6 2D arrays with the same dimension (200, 200). Every three consecutive arrays can be stacked up to a 3D array.
Desired output:
array = (200, 200, 3, 2)
I am familiar with np.dstack:
n = 6
array = []
for i in range(n):
x = np.random.randn(n1, n2)
array.append(x)
array = np.dstack(array)
Speed should be considered as I am working with large data sets.
Edit: I created images for testing: https://wetransfer.com/downloads/afb0a2fbfbd8b6047a68dad41b56a0d520200509184625/761355
Adapted Paddy's answer:
import numpy as np
from skimage import io
import glob
from natsort import natsorted
DIR = (Set folder path with the test images)
list_path = glob.glob(DIR + "/*.png")
list_path_sorted = natsorted(list_path)
array = []
for i in range(len(list_path_sorted)):
image = np.array( io.imread(list_path_sorted[i]) )
array.append(image)
a = np.dstack(array).reshape(200, 200, 3, 2)
a.shape
>>> (200, 200, 3, 2)
Issue: The order in the third dimension doesn't match. To test this:
plt.imshow(a[:,:,0,0]) -> should show picture with A1
plt.imshow(a[:,:,2,0]) -> should show picture with A3
plt.imshow(a[:,:,0,1]) -> should show picture with B1
plt.imshow(a[:,:,2,1]) -> should show picture with B3





