[英]I can't plot multi grayscale images
I have a training set with 31367 examples this data is RGB images, I want to convert them from RGB to grayscale and plot it in jupyter notebook.我有一个包含 31367 个示例的训练集,这些数据是 RGB 图像,我想将它们从 RGB 转换为灰度并将其绘制在 jupyter notebook 中。
# Convert from RBG to grayscale
X_train_gray = np.expand_dims(np.asarray([cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for img in X_train]), 3)
X_train_gray = np.reshape(X_train_gray, (len(X_train_gray), 32, 32))
X_train_gray = np.asarray(X_train_gray)/255
To plot 3 image I do this:要绘制 3 个图像,我这样做:
figg, axx = plt.subplots(1,3)
axx[1,1].imshow(X_train_gray[13])
axx[1,2].imshow(X_train_gray[14])
axx[1,3].imshow(X_train_gray[15])
IndexError Traceback (most recent call last) in () IndexError Traceback (最近一次调用最后一次) in ()
---> 17 axx[1,1].imshow(X_train_gray[14])
IndexError: too many indices for array IndexError:数组的索引太多
Note: there's no error if i use plt.imshow(X_train_gray[14]), and it plots the gray image.
注意:如果我使用 plt.imshow(X_train_gray[14]) 没有错误,它会绘制灰色图像。
The issue is with the indexing of the axes.问题在于轴的索引。 The indexing starts at 0. Moreover, when doing:
索引从 0 开始。此外,在执行以下操作时:
f, ax = plt.subplots(1,3)
ax will looks like: ax 看起来像:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x0000024A6F452320>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0000024A6F4A3358>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0000024A6F4C99E8>],
dtype=object)
Thus, you need to use only 1 indices and not 2.因此,您只需要使用 1 个索引而不是 2 个。
Solution:解决方案:
figg, axx = plt.subplots(1,3)
axx[0].imshow(X_train_gray[13])
axx[1].imshow(X_train_gray[14])
axx[2].imshow(X_train_gray[15])
Add the plt.gray()
method before subplots:在子图之前添加
plt.gray()
方法:
figg, axx = plt.subplots(1,3)
plt.gray()
axx[1,1].imshow(X_train_gray[13])
axx[1,2].imshow(X_train_gray[14])
axx[1,3].imshow(X_train_gray[15])
It works for me.这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.