Reputation: 43
I need help in the following regard...
this code:
show_picture(x_train[0])
print(x_train.shape)
plt.imshow(x_train,cmap=cm.Greys_r,aspect='equal')
returns the following:
(267, 100, 100, 3)
TypeError Traceback (most recent call last)
<ipython-input-86-649cf879cecf> in <module>()
2 show_picture(x_train[0])
3 print(x_train.shape)
----> 4 plt.imshow(x_train,cmap=cm.Greys_r,aspect='equal')
5
5 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in set_data(self, A)
697 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
698 raise TypeError("Invalid shape {} for image data"
--> 699 .format(self._A.shape))
700
701 if self._A.ndim == 3:
TypeError: Invalid shape (267, 100, 100, 3) for image data
whats the correct procedure to do this
Upvotes: 2
Views: 108
Reputation: 671
First of all, it seems like you are working with an array of 267 of 100x100 RGB images here. I am assuming you are using a NumPy array. In order to convert the images to grayscale you can use the method proposed in this answer:
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
x_train_gray = rgb2gray(x_train)
Note that this works for all images in one pass and the resulting shape should be (267, 100, 100)
. However, np.imshow
only works for one image at a time so to plot an image in grayscale you can do the following:
plt.imshow(x_train_gray[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
Upvotes: 1