【问题标题】:ValueError: too many values to unpack - OpenCV Python HoughLinesValueError:解压的值太多 - OpenCV Python HoughLines
【发布时间】:2019-07-06 03:44:44
【问题描述】:

由于来自 OpenCV 的 HoughLine,我正在尝试从图像中检测到的线中获取 rho 和 theta。

lines = cv.HoughLinesP(edges, 1, np.pi/180, hThreshold, maxLineGap=lineGap)
if lines is not None:
    for line in lines:
       rho, theta = line[0]

但我在最后一行收到此错误。

ValueError:解包的值太多

你知道如何解决这个问题吗?或者另一种获取 rho 和 theta 值的方法?

PS:我用 pip3 install opencv-python --user 安装了 opencv-python

【问题讨论】:

标签: python opencv hough-transform


【解决方案1】:

上述代码中的line[0] 是一个包含4 个值的列表。这就是为什么,你正在超越错误。您正在做的是尝试使用Probabilistic Hough lines 检测行,即,

lines = cv2.HoughLinesP(binarized image, ro accuracy, theta accurancy, threshold, minimum line length, max line gap)

正确代码:

lines = cv.HoughLinesP(edges, 1, np.pi/180, hThreshold, maxLineGap=lineGap)
for line in lines:
    x1, y1, x2, y2 = line[0]

但是,您要做的是使用Hough lines 进行线路检测。因此,将您的代码从 lines = cv.HoughLinesP(edges, 1, np.pi/180, hThreshold, maxLineGap=lineGap) 更改为

lines = cv2.HoughLines(edges, 1, np.pi / 180, 220)
for line in lines:
    rho, theta = line[0]
    print(rho, theta)

【讨论】:

  • 确实,它与 HoughLines 方法完美配合,感谢您的快速回答。
猜你喜欢
  • 2017-11-25
  • 2015-09-04
  • 2018-10-30
  • 2022-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2015-03-15
相关资源
最近更新 更多