我需要在以下方面获得帮助…
这段代码:
show_picture(x_train[0])print(x_train.shape)plt.imshow(x_train,cmap=cm.Greys_r,aspect='equal')
返回以下内容:
(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
正确的操作步骤是什么
回答:
首先,看起来你在这里处理的是一个包含267张100×100像素的RGB图像的数组。我假设你使用的是NumPy数组。为了将这些图像转换为灰度,你可以使用这个答案中提出的方法:
def rgb2gray(rgb): return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])x_train_gray = rgb2gray(x_train)
请注意,这可以一次性处理所有图像,结果的形状应该是(267, 100, 100)
。然而,np.imshow
一次只能处理一张图像,所以要以灰度显示图像,你可以这样做:
plt.imshow(x_train_gray[0], cmap=plt.get_cmap('gray'), vmin=0, vmax=1)plt.show()