w,h = template.shape [:: - 1]导致值错误太多值无法解压缩

时间:2016-06-30 12:20:55

标签: python opencv

我在Python 2.7 (x86) ,Open CV 3.1.0, Windows 7 (x64)

在Open CV上尝试一些简单的模板匹配

图片

enter image description here

代码

dark_elixir_sample = cv2.imread('dark_elixir_sample.png')

w, h = dark_elixir_sample.shape[::-1]

我正在尝试让w,h在我的脚本中进一步使用,但是脚本会返回以下错误。

错误

ValueError: too many values to unpack

3 个答案:

答案 0 :(得分:3)

您的图片形状返回3维

im.shape
>>> (24, 28, 3)

如果你只想要前两个:

w, h = im.shape[:-1]
>>> (24, 28)

w, h, _ = im.shape
# w is 24, h is 28

因为否则,您试图将3个值解压缩为仅2个变量,这在Python中无效,因此您的错误。 _就像a convention in Python for variables you don't want to use,或“一次性”。

答案 1 :(得分:0)

dark_elixir_sample = cv2.imread('dark_elixir_sample.png',0)
w, h = dark_elixir_sample.shape[::-1]

答案 2 :(得分:0)

您正在做-w,h = im.shape
   发生了什么-w,h = imageHeight,imageWidth,number(rgb为3,灰色为2)

解决方案   w,h,_ = in.shape [::-1]或转换为灰度图像