示出具有pylab.imshow的图像()(Showing an image with pylab.

2019-07-20 07:39发布

我是比较新的这一切,我开始在这里做图像分析教程: http://www.pythonvision.org/basic-tutorial我已经安装了所有的模块,但我打一个之前没有走得很远障碍。 尝试执行时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

相当肯定我按照教程,以信的所有指示,但我不能工作了是哪里出问题了

谢谢

Answer 1:

“这只是什么图像保存为DNA = mahotas.imread( 'dna.jpeg')型(DNA)给出numpy.ndarray和dna.shape给出(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)


文章来源: Showing an image with pylab.imshow()