简体   繁体   中英

Convert a 3d array to a 4d array

I have a matrix and I am obtaining a 2 channel matrix with images having size 256x120. Now, I need to store several images so I need to reshape my matrix to (No.ofimages,256,120,2).

I tried to use reshape and then append:

But I am getting a TypeError: 'builtin_function_or_method' object is not subscriptable when using reshape

Any ideas on how I can solve it?

Based on my current understanding of your issue:

import numpy as np

img = np.random.random((112,112,2))
print(img.shape)

result = np.empty((0, 112, 112, 2))  # first axis is zero, for adding images along it

for i in range(100):        # replace this loop with something that reads in the images
    result = np.append(result, img[np.newaxis, ...], axis=0)    # add a new axis to each image and append them to result

print(result.shape)

Will produce:

(112, 112, 2)
(100, 112, 112, 2)

To access the images stored in the result variable, simply use indexing:

print(result[1].shape)   # e.g., access the second image

Will produce:

(112, 112, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM