1

I'm currently working with fMRI data in the form of 3D numpy arrays. However, my arrays are of various shapes: from (60, 40, 20, 20) to (64, 64, 40, 20) to values like (96, 96, 60, 20). In order to use these fMRI images in a machine learning framework, I need to ensure that all of these numpy arrays are the same shape.

This is what I'm doing right now:

for f in dataset:
  img_data_arr.resize (64, 64, 40, 20)

I'm using the resize function to reshape all of the arrays into (64, 64, 40, 20) shape. However, I've heard that if the image is larger than the shape given, it'll just replace the empty boxes with repeated values. For example:

First array:
[[1 2 3]
 [4 5 6]]

The shape of first array:
(2, 3)

Second array:
[[1 2]
 [3 4]
 [5 6]]

The shape of second array:
(3, 2)

Resize the second array:
[[1 2 3]
 [4 5 6]
 [1 2 3]]

Is there any way to resize smaller arrays so that the empty spaces are filled with 0s? Thank you!

1
  • Don't depend on what you hear. Read the documentation of the relevant function or method. I doubt if the numpy resize (function or method) is appropriate for your purpose. And test anything with small enough arrays so that you can actually what's going on. Image processing packages (cv2 etc) have better resizing functions. Commented Aug 7, 2020 at 19:20

1 Answer 1

1

If you try to resize an array to a different shape that does not have the same exact number of elements, it will throw an error. Here, is an example:

a = np.array([[1,2,3],[4,5,6]])
#[[1 2 3]
#[4 5 6]]

print(a.shape)
#(2, 3)

print(a.reshape(3,2))
#[[1 2]
# [3 4]
# [5 6]]

print(a.reshape(3,3))
#ValueError: cannot reshape array of size 6 into shape (3,3)

However, for fMRI and machine learning purposes, I would caution you about resizing. Since, resizing is changing the neighborhood of pixels, it might have a different interpretation of what you see it an image. I would suggest padding smaller images to the largest image size using np.pad with 0s (or any value you deem fit) or having a layer at input to upsample/downsample pixels of images to same size images. I assume this is a common issue and has other standard solutions as well.

Sign up to request clarification or add additional context in comments.

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.