简体   繁体   中英

How to convert 4D image array to 3D image array

image_arr.shape is (501, 128, 128, 1)

My code:

n_samples, h, w = images_arr.shape

Returns the following error:

ValueError: too many values to unpack (expected 3)

How do I convert above shape to 3D?

Given that the array shape is (501, 128, 128, 1) , the last dimension is not needed, so we can squeeze it out, ie. all the relevant data is in the first 3 dimensions in this case:

images_arr = np.empty((501, 128, 128, 1))

squeezed = np.squeeze(images_arr)

squeezed.shape
>>> (501, 128, 128)

n_samples, h, w = squeezed.shape

The (501, 128, 128, 1) is a 4-tuple - has four values inside. However, n_samples, h, w expects three values.

Note now that in the image_arr.shape the last value is number of colour channels that you don't need for n_samples, h, w .

What you should do then is:

n_samples, h, w = image_arr.shape[:3]

That is, take first three values (and skip the colour).

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