【问题标题】:Python / Contour OpenCV returns "too many values to unpack"Python / Contour OpenCV 返回“太多值无法解包”
【发布时间】:2018-07-15 16:04:31
【问题描述】:

我对本教程有疑问:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html 确实,当我尝试执行此代码时遇到此错误:

import numpy as np
import cv2

img = cv2.imread("onepoint.png", 0)

canny_img = cv2.Canny(img,150,200)
ret,thresh = cv2.threshold(canny_img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print M

这是错误:

  File "contours.py", line 13, in <module>
    contours,hierarchy = cv2.findContours(thresh, 1, 2)
ValueError: too many values to unpack

我不明白为什么会出现这个错误。我知道cv2.findContours (thresh, 1, 2) 会返回给我 3 个“数组”,但是为什么呢?如果有人好心向我解释我是一个接受者:(

我正在尝试构筑这个picture 的文字。

我是 OpenCV 的新手,我想你会明白的,

提前致谢

【问题讨论】:

标签: python opencv image-processing opencv-contour


【解决方案1】:

您需要将其更改为

im, contours,hierarchy = cv2.findContours(thresh, 1, 2)

或者,如果您只关心轮廓:

(_, contours, _) = cv2.findContours(thresh, 1, 2)

OpenCV 3中的功能发生了变化。

【讨论】:

  • 我明白了,谢谢 :) 但是为什么它返回了 3 个数组而不是教程中的 2 个?
  • @CorentinMar 因为本教程是为仅返回两个值的旧版本 OpenCV 编写的(正如 Jan 所提到的,该函数在 OpenCV 3 中已更改)。请注意在3.0 documentation 中,您可以看到有三个返回值而不是两个as there was in 2.4。
猜你喜欢
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 2014-10-19
  • 2019-03-28
  • 2013-06-06
  • 2017-07-17
相关资源
最近更新 更多