简体   繁体   中英

How to convert 4d numpy array of images to 3d?

I have 4d numpy array of shape (80,8,8,3). It's 80 images 8x8, 3 channels. I want to make one large image out of them with 8 rows and 10 columns. How can I convert 4d array to 3d with reshaping to a needed shape? I tried np.concatenate(images[i:i + 10,:,:,:] but it doesn't work in some reason.

If you just want to see them, you could make a plot without fiddling with your data:

data = np.random.random((80, 8, 8, 3))

import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=8, ncols=10, figsize=(10, 8))
for ax, img in zip(axs.ravel(), data):
    ax.imshow(img)
    ax.axis('off')

This results in:

matplotlib 输出

If you really want to reshape it, you can do this:

n, h, w, c = data.shape
data_new = (data.reshape(8, 10, h, w, c)
                .swapaxes(1,2)
                .reshape(h*8, w*10, c))

To plot this result:

plt.figure(figsize=(10, 8))
plt.imshow(data_new)
plt.axis('off')

matplotlib 输出

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