1

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.

enter image description here

2
  • 1
    Don't post images of text Commented Jun 28, 2022 at 22:33
  • What dimension of the mask corresponds to the rows of the image. For example, if your images were 10x20 instead of 512x512, would the mask be (20, 10, 288) or (10, 20, 588)? Commented Jun 28, 2022 at 22:41

2 Answers 2

1

Reshape just keeps the bytes in the same order. You actually need to move pixels around.

mask = np.transpose(mask,(2,0,1))
Sign up to request clarification or add additional context in comments.

1 Comment

That changes strides, but does not necessarily move anything around. Also, because of the square shape, it's unclear what the order of the 0 and 1 should be
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.