简体   繁体   中英

Unable to reshape numpy array to RGB image

I have an numpy array which is of all images from CIFAR10 dataset: (50000, 3072)

I am taking a single array out of it, its shape is (3072,) then am trying to convert it to RGB image by doing .reshape(32, 32, 3)

But when I pass it to plt.show() it shows something like在此处输入图片说明

I am not sure why it is not showing the image correctly, I also tried .reshape(3, 32, 32) but it gives TypeError: Invalid shape (3, 32, 32) for image data while plotting

How do I reshape it correctly?

You should try np.swapaxes() to get valid shape for plt.imshow() to accept.

import numpy as np
dd = np.array([1]*3072).reshape(3, 32, 32)
dd1 = np.swapaxes(dd, 0, 2)

Try plt.imshow(dd1) to see what happens. (use your own data) :p

First of all, you should reshape using (3, 32, 32), this is determine by order of your data.

Then you have to display the reshape image, but plt.imshow() only accepts array with the shape (M, N) or (M, N, 3) or (M, N, 4), so you have to swap axes now as the reshaped data is (3, 32, 32). Details see plt.imshow and here

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