【问题标题】:I am getting error with im.show()我收到 im.show() 错误
【发布时间】:2017-08-13 15:24:30
【问题描述】:

我正在尝试保存灰度图像 (​​256,256,1) 并将其显示在输出中。

im = data.astype(np.uint8)
print im.shape
im = np.transpose(im, (2,1,0)) 
print im.shape
im.show()

但是,我收到以下错误:

(256, 256, 1)
Traceback (most recent call last):
  File "lmdb_reader.py", line 37, in <module>
    plt.imshow(im)
  File "/home/se/anaconda2/envs/caffeenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3029, in imshow
    **kwargs)
  File "/home/se/anaconda2/envs/caffeenv/lib/python2.7/site-packages/matplotlib/__init__.py", line 1819, in inner
    return func(ax, *args, **kwargs)
  File "/home/se/anaconda2/envs/caffeenv/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 4922, in imshow
    im.set_data(X)
  File "/home/se/anaconda2/envs/caffeenv/lib/python2.7/site-packages/matplotlib/image.py", line 453, in set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data

【问题讨论】:

标签: python python-2.7 python-3.x numpy matplotlib


【解决方案1】:

您需要在调用 matplotlib.pyplot.show() 之前指定颜色图。 默认情况下,当您传递 3D 数组时,该函数需要 RGB 图像。

例子:

im = np.squeeze(im)
plt.imshow(im,cmap='gray')
plt.show()

【讨论】:

    【解决方案2】:

    请注意,im.show() 不存在,但它可能只是问题中的拼写错误。 真正的问题是:

    Matplotlib 的pyplot.imshow 可以绘制尺寸为(N,M)(灰度)或(N,M,3)(RGB 颜色)的图像。你的头像是(N,M,1);因此,我们需要摆脱最后一个维度

    import matplotlib.pyplot as plt
    import numpy as np
    #create data of shape (256,256,1)
    data = np.random.rand(256,256,1)*255
    im = data.astype(np.uint8)
    print im.shape # prints (256L, 256L, 1L)
    # (256,256,1) cannot be plotted, therefore 
    # we need to get rid of the last dimension:
    im = im[:,:,0]
    print im.shape # (256L, 256L)
    # now the image can be plotted
    plt.imshow(im, cmap="gray")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 2018-12-21
      • 2015-09-13
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2018-11-24
      相关资源
      最近更新 更多