The np.stack document explains:
Join a sequence of arrays along a new axis.
The
axis
parameter specifies the index of the new axis in the dimensions of the result. For example, ifaxis=0
it will be the first dimension and ifaxis=-1
it will be the last dimension.
Generate a new axis for a given axis, and stack arrays along that axis
Here is the code test:
a = np.arange(8).reshape((2,2,2))
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
b = np.arange(8).reshape((2,2,2))+1
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
The first 2 is 2 samples, and the next 2*2 is the pixel matrix. To turn a and b into 2*2*2*2, the fourth 2 represents the color channel:
c = np.stack((a,b),axis=3)
array([[[[0, 1],
[1, 2]],
[[2, 3],
[3, 4]]],
[[[4, 5],
[5, 6]],
[[6, 7],
[7, 8]]]])
Verify, see c[:,:,:,0]:
c[:,:,:,0]
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
It can also become the second 2 is the color channel:
c = np.stack((a,b),axis=1)
array([[[[0, 1],
[2, 3]],
[[1, 2],
[3, 4]]],
[[[4, 5],
[6, 7]],
[[5, 6],
[7, 8]]]])
c[:,1,:,:]
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
c[:,0,:,:]
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])