使用pylab.imshow()显示图片
我对这些东西还比较陌生,最近开始学习图像分析的教程,链接在这里。在尝试执行 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
我很确定我按照教程的每一步都做了,但就是搞不清楚哪里出了问题。
3 个回答
0
import matplotlib.pyplot as plt
plt.imshow(img.reshape(48, 48)) # for example: 48
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
0
从 v3.3 版本开始,这个错误不再出现了,因为现在大小为 MxNx1 的三维数组会自动转换成 MxN 的格式来显示。
43
这段话的意思是,图像在保存时的格式是这样的:用代码
dna = mahotas.imread('dna.jpeg')
读取后,type(dna)
会显示为numpy.ndarray
,而dna.shape
会显示为(1024, 1344, 1)
,表示这个图像的尺寸和通道数。
这里的问题是,如果你传入一个三维的 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)