'why plotting a (112,112,2) in matplotlib using imshow() is not possible?
why is it that we can not plot a matrix that has the shape (112,112,2) but doing it for shapes such as (112,112,3)is ok ?
TypeError: Invalid shape (112, 112, 2) for image data
why doesn't this error come up when trying to plot a (112,112,3) matrix ?
010101010101010101010100101010101010101010011010101010010101010101010
UPDATE I get this image from the web(address of the image
and use plt.imread() to turn the image into matrix. and then use plt.matshow() to plot it.
this is the result:

so far so good, after doing 3dmaxpooling on the image:
def maxpooling3d(image_path,stride):
"""
image_path = the URL or the directory path of the image
stride = the strides for 3d data i.e (2,2,2)
"""
#MATRIXIFY THE DATA AND GET THEIR DIMENSIONS
image_matrix = plt.imread(image_path)
d1,d2,d3 = image_matrix.shape
#RESHAPE IT
image_matrix_reshaped = image_matrix.reshape(1,d1,d2,d3,1)
#MAXPOOL THE RESHAPED IMAGE MATRIX
max_pool = tf.keras.layers.MaxPooling3D(strides=stride)
model = tf.keras.Sequential([max_pool])
result = model.predict(image_matrix_reshaped)
return np.squeeze(result)
and then get the result :
result_matrix = maxpooling3d(image_path,(2,2,2))
now that I want to plot the maxpooled matrix, I get this error:
TypeError Traceback (most recent call last)
<ipython-input-249-941479181323> in <module>
----> 1 plt.matshow(result_matrix)
e:\Users\Keram\anaconda3\lib\site-packages\matplotlib\pyplot.py in matshow(A, fignum, **kwargs)
2299 fig = figure(fignum, figsize=figaspect(A))
2300 ax = fig.add_axes([0.15, 0.09, 0.775, 0.775])
-> 2301 im = ax.matshow(A, **kwargs)
2302 sci(im)
2303 return im
e:\Users\Keram\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in matshow(self, Z, **kwargs)
7751 'aspect': 'equal', # (already the imshow default)
7752 **kwargs}
-> 7753 im = self.imshow(Z, **kw)
7754 self.title.set_y(1.05)
7755 self.xaxis.tick_top()
e:\Users\Keram\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1445 def inner(ax, *args, data=None, **kwargs):
1446 if data is None:
-> 1447 return func(ax, *map(sanitize_sequence, args), **kwargs)
1448
1449 bound = new_sig.bind(ax, *args, **kwargs)
e:\Users\Keram\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, **kwargs)
5521 resample=resample, **kwargs)
5522
-> 5523 im.set_data(X)
5524 im.set_alpha(alpha)
5525 if im.get_clip_path() is None:
e:\Users\Keram\anaconda3\lib\site-packages\matplotlib\image.py in set_data(self, A)
709 if not (self._A.ndim == 2
710 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
--> 711 raise TypeError("Invalid shape {} for image data"
712 .format(self._A.shape))
713
TypeError: Invalid shape (256, 256, 2) for image data
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
