I have a 3d array in shape (288,512,512) that 288 is the number of images and 512*512 is the width and height of the image. Also, there is a mask array of this image in shape (512,512,288). How can I convert the shape of the mask array to the shape of this image array? I reshaped the mask array in shape (288,512,512), but I plot any mask from this array, not found a correlation between this mask and the its corresponding image.
CodePudding user response:
Reshape just keeps the bytes in the same order. You actually need to move pixels around.
mask = np.transpose(mask,(2,0,1))
CodePudding user response:
You don't want to reshape the array, but rather swap the interpretation of the dimensions. Depending on which axis of the mask corresponds to rows, you will want either
mask.transpose()
OR
mask.transpose(2, 0, 1)
Here is a simple example that shows what reshape and transpose do to a small 2D array to help you understand the difference:
>>> x = np.arange(10).reshape(2, 5)
>>> x
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> x.reshape(5, 2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> x.transpose()
array([[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]])
As you can see, reshape changes the sizes of the axes, while transpose alters the strides. Usually, when you combine them, data ends up getting copied somewhere along the way.