python - 使用 pylab.imshow() 显示图像

标签 python matplotlib imshow

我对这一切都比较陌生,我开始学习图像分析教程 here .当试图执行 pylab.imshow(dna)步骤它返回以下错误:

In [10]: pylab.imshow(dna)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-fc86cadb4e46> in <module>()
----> 1 pylab.imshow(dna)

 /usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect,    interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
   2375         ax.hold(hold)
   2376     try:
-> 2377         ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   2378         draw_if_interactive()
   2379     finally:

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   6794                        filterrad=filterrad, resample=resample, **kwargs)
   6795 
-> 6796         im.set_data(X)
   6797         im.set_alpha(alpha)
   6798         self._set_artist_props(im)

/usr/lib/pymodules/python2.7/matplotlib/image.pyc in set_data(self, A)
    409         if (self._A.ndim not in (2, 3) or
    410             (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 411             raise TypeError("Invalid dimensions for image data")
    412 
    413         self._imcache =None

TypeError: Invalid dimensions for image data
可以肯定的是,我已按照教程中的所有说明进行操作,但我无法确定是出了什么问题。

最佳答案

it's just what the image is saved as in dna = mahotas.imread('dna.jpeg') type(dna) gives numpy.ndarray and dna.shape gives (1024, 1344, 1)


这就是问题所在,如果您通过 3D ndarray ,它预计您将拥有 3 或 4 个平面(RGB 或 RGBA)(阅读堆栈跟踪最后一帧中第 410 行的代码)。
你只需要摆脱额外的维度使用
dna = dna.squeeze()
或者
imshow(dna.squeeze())
看看squeeze正在做,看下面的例子:
a = np.arange(25).reshape(5, 5, 1)
print a.shape # (5, 5, 1)
b = a.squeeze()
print b.shape # (5, 5)

关于python - 使用 pylab.imshow() 显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008045/

相关文章:

python - 使用简单绘图脚本时出现属性错误

python - 为什么这些图像 gridspec 子图的高度略有不同?

python - 在 imshow 上绘制 RGB 点

python - python "slow"是如何进行游戏开发的?

Python迭代循环

python - 使用 Sphinx 创建 PDF 时如何避免 "too deeply nested"错误?

python - 格式化numpy记录数组中的 "Kilo"、 "Mega"、 "Gig"数据

python - 当绘制在强度之上时,颤抖图会发生变化

python - 图像标题未循环显示

Python:防止 Pandas 系列中的值四舍五入为整数