Python 图像的等高线图给出:TypeError输入必须是2D数组错误

Python 图像的等高线图给出:TypeError输入必须是2D数组错误,python,python-2.7,numpy,matplotlib,scipy,Python,Python 2.7,Numpy,Matplotlib,Scipy,我正在使用Debian Linux和python 2.7。 我正在读一个图像,并试图处理它,但我被显示以下错误。有人能告诉我我做错了什么吗 import Image import scipy from scipy import ndimage import matplotlib.pyplot as plt import numpy as np from scipy import misc import scipy.misc img = scipy.misc.imread("/home/subh

我正在使用Debian Linux和python 2.7。
我正在读一个图像,并试图处理它,但我被显示以下错误。有人能告诉我我做错了什么吗

import Image
import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np
from scipy import misc
import scipy.misc

img = scipy.misc.imread("/home/subhradeep/Desktop/test.jpg")
array=np.asarray(img)
plt.figure(figsize=(10, 3.6))

plt.subplot(131)
plt.imshow(array, cmap=plt.cm.gray)

plt.subplot(132)
plt.imshow(array, cmap=plt.cm.gray, vmin=10, vmax=100)
plt.axis('off')

plt.subplot(133)
plt.imshow(array, cmap=plt.cm.gray)
plt.contour(array, [160, 211])
plt.axis('off')

plt.subplots_adjust(wspace=0, hspace=0., top=0.99, bottom=0.01, left=0.05,right=0.99)
plt.show()
我收到以下错误消息

Traceback (most recent call last):
  File "1saveimg.py", line 22, in <module>
    plt.contour(array, [160, 211])
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2632, in contour
    ret = ax.contour(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 7976, in contour
    return mcontour.QuadContourSet(self, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1414, in __init__
    ContourSet.__init__(self, ax, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 860, in __init__
    self._process_args(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1427, in _process_args
    x, y, z = self._contour_args(args, kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1488, in _contour_args
    x, y = self._initialize_x_y(z)
  File "/usr/lib/pymodules/python2.7/matplotlib/contour.py", line 1573, in _initialize_x_y
    raise TypeError("Input must be a 2D array.")
TypeError: Input must be a 2D array.
回溯(最近一次呼叫最后一次):
文件“1saveimg.py”,第22行,在
plt.等高线(阵列[160211])
文件“/usr/lib/pymodules/python2.7/matplotlib/pyplot.py”,等高线中的第2632行
ret=最大等高线(*args,**kwargs)
文件“/usr/lib/pymodules/python2.7/matplotlib/axes.py”,等高线中的第7976行
返回mcontour.QuadContourSet(self,*args,**kwargs)
文件“/usr/lib/pymodules/python2.7/matplotlib/contour.py”,第1414行,在__
轮廓线集.uuuu初始化(self,ax,*args,**kwargs)
文件“/usr/lib/pymodules/python2.7/matplotlib/contour.py”,第860行,在__
自身处理参数(*参数,**kwargs)
文件“/usr/lib/pymodules/python2.7/matplotlib/contour.py”,第1427行,在进程参数中
x、 y,z=自身轮廓参数(参数,kwargs)
文件“/usr/lib/pymodules/python2.7/matplotlib/contour.py”,第1488行,在参数中
x、 y=self.\u初始化\u x\u y(z)
文件“/usr/lib/pymodules/python2.7/matplotlib/contour.py”,第1573行,在
raise TypeError(“输入必须是2D数组”)
TypeError:输入必须是二维数组。

我也尝试了
Image.open()
,但这也引发了同样的错误。

这里的问题是
plt.contour
只打印二维数组,但大多数
jpg
文件都是三维的,因为有三种颜色。您已通过使用灰度彩色贴图(
cm.gray
)打印来隐藏这一事实,这只会改变它的外观(
imshow
仍在显示彩色图像,只是用灰色“墨水”打印)。你有几个选择,主要是:

1) 以某种方式将其转换为2d,最简单的方法是使图像黑白(灰度)。以下是一些方法:

gray = img.sum(-1)  # sums along the last (color) axis

2) 分别绘制每种颜色:

r, g, b = np.rollaxis(img, -1)
plt.contour(r, cmap=plt.cm.Reds)
plt.contour(g, cmap=plt.cm.Greens)
plt.contour(b, cmap=plt.cm.Blues)

而且

scipy.misc.imread
返回一个数组,因此您可以跳过行
arr=np.asarray(img)
,直接使用
img
。如果您决定使用
PIL.Image
,它将返回一个
PIL.Image
对象(因此可以使用
img.convert
方法),则情况并非如此

这样做是多余的,只需选择其中一个:

from scipy import misc
import scipy.misc

此处,如果转换为灰度:

并且,如果单独绘制每种颜色:

此代码有什么错误?始终将完整的错误回溯复制并粘贴到问题本身中(而不是仅将错误消息放在标题中)。更改图像文件名并修复复制/粘贴错误
le$
后,您的代码似乎适合我。@DSM我应该对图像文件名做哪些更改?你想用img替换所有的数组指令吗?不,我的意思是显然我在
/home/subradeep/Desktop/test.jpg中没有图片,所以我不得不把它改成我自己的。然而,事实证明,我的测试之所以有效,是因为我随机选择的
jpg
文件恰好是黑白的非常感谢你立刻消除了我所有的疑虑。没有比这更好的解释了。现在我把所有的概念都弄清楚了。我试着按照你的建议分别绘制每种颜色,但后来我得到了这个回溯(最后一次调用):文件“1saveimg.py”,第23行,在r,g,b=np.rollaxis(img,-1)ValueError:太多的值无法解包我现在做错了什么?嗯,你能告诉我img.shape
是什么吗?该行假设它类似于
(H,W,3)
,其中
H,W
是图像的高度和宽度,
3
是颜色的数量。
rollaxis
只是将形状更改为
(3,H,W)
,因此
r,g,b=…
相当于
r=np.rollaxis(img,-1)[0];g=np.rollaxis(img,-1)[1]
等等。也许忽略
rollaxis
并执行
plt.contour(img[…,0])更简单;plt.等高线(img[…,1])
等等
r, g, b = np.rollaxis(img, -1)
plt.contour(r, cmap=plt.cm.Reds)
plt.contour(g, cmap=plt.cm.Greens)
plt.contour(b, cmap=plt.cm.Blues)
from scipy import misc
import scipy.misc