简体   繁体   中英

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.

# 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:

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 ()

---> 17 axx[1,1].imshow(X_train_gray[14])

IndexError: too many indices for array

Note: there's no error if i use plt.imshow(X_train_gray[14]), and it plots the gray image.

The issue is with the indexing of the axes. The indexing starts at 0. Moreover, when doing:

f, ax = plt.subplots(1,3)

ax will looks like:

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.

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:

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.

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