简体   繁体   English

我无法绘制多灰度图像

[英]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])

I got this ERROR:我收到了这个错误:

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.

相关问题 如何将图像转换为灰度? - How can i convert images into grayscale? 我无法使用 CV 在 python 中将我的 RGB 颜色更改为灰度图像 - I can't change my RGB colour into grayscale images in python using CV 如何使用 NumPy 处理彩色和灰度图像? - How can I use NumPy to work with both colored and grayscale images? 无法将视频转换为灰度 - Can't convert video to grayscale 如何创建多个 plot 案例? - How I can create a multi plot cases? 神经网络不接受灰度图像 - Neural network doesn't accept grayscale images 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? 如何变换灰度图像的直方图以强制特定比例的高光/中间调/阴影? - How can I transform the histograms of grayscale images to enforce a particular ratio of highlights/midtones/shadows? 如何使用带有灰度图像的预训练神经网络? - How can I use a pre-trained neural network with grayscale images? 如何在张量流中反转黑白图像(或灰度图像)? (黑色&lt;-&gt;白色变化) - How can I invert BLACK and WHITE (or grayscale) images in tensorflow? (black <->white change)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM