【问题标题】:Python opencv probabilistic Hough line transform - TypeError: object of type 'NoneType' has no len()Python opencv概率霍夫线变换-TypeError:'NoneType'类型的对象没有len()
【发布时间】:2019-12-10 11:26:23
【问题描述】:

我想对棋盘图像执行概率霍夫线变换,但遇到了错误。这个错误的一个奇怪之处在于 它只在我为 OpenCV 函数 HoughLinesP() 的参数指定关键字时发生

首先,查看我的代码行和收到的错误消息:

#-*- coding:utf-8-*-
import matplotlib.pyplot as plt
import cv2
from skimage import io
import numpy as np

# Load image file
fpath = 'D:/Users/'
image = io.imread(fpath + 'checkerboard.JPG')
image_original = image.copy()

edges = cv2.Canny(image, 50, 200, apertureSize=3)
gray = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)

minLineLength = 100
maxLineGap = 0
# Perform the probabilistic Hough transform
lines = cv2.HoughLinesP(image=edges, rho=1, theta=np.pi/180, threshold=100, minLineLength=minLineLength, maxLineGap=maxLineGap)

for i in range(len(lines)):
    for x1, y1, x2, y2 in lines[i]:
        cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 3)

plt.figure(figsize=(10, 5), dpi=150)
plt.subplot(1, 2, 1)
plt.imshow(image_original, cmap='gray', vmin=0, vmax=255)

plt.subplot(1, 2, 2)
plt.imshow(image, cmap='gray', vmin=0, vmax=255)

plt.show()
Traceback (most recent call last):
  File "D:\Users\sihohan\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-c2c4eb0ac4e8>", line 1, in <module>
    runfile('D:/Users/2_prob_hough.py', wdir='D:/Users')
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/Users/2_prob_hough.py", line 20, in <module>
    for i in range(len(lines)):
TypeError: object of type 'NoneType' has no len()

如上所述,如果我不为 HoughLinesP() 的参数指定关键字,我的代码可以正常工作:

# Perform the probabilistic Hough transform
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap)

这可能是什么原因? 谢谢。

(以下是 maxLineGap = 0、10 和 20 的输出图像)

【问题讨论】:

    标签: python python-3.x opencv hough-transform


    【解决方案1】:

    OpenCV houghLinesP parameters 声明cv2.HoughLinesP 包含冗余参数lines。所以你的 minLineLength 在

    # Perform the probabilistic Hough transform
    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap)
    

    用作lines 参数。因此,maxLineGap 被用作 minLineLength,因为它被设置为零,所以会产生更多的结果。

    【讨论】:

    • 是的,所以我认为如果我指定所有参数(image=、rho=、theta=、threshold=、minLineLength=、maxLineGap=),它会起作用,但我注意到我需要指定除了 link 中建议的 image= 之外的所有其他人,这也很奇怪......所以如果我写 lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi/180, threshold=100, minLineLength=minLineLength, maxLineGap=maxLineGap),我的代码可以工作,但是这一次,问题是它只在 maxLineGap 不为 0 时才有效. 你知道这是什么原因吗?
    • 我的第一个猜测是,您的图像中没有满足所有参数的线条,但我不能肯定地说。你能上传你的图片吗?
    • 我在帖子中添加了指向生成的输出图像的链接。所以要明确一点,对于 maxLineGap=0,当我没有指定任何参数时,代码工作并生成了输出,但是对于 maxLineGap=10 和 20,我必须指定除第一个参数之外的所有参数 (image=) .
    • 我不确定你的问题是否正确。如果您查看HoughTranformP 的定义,您会看到def HoughLinesP(image, rho, theta, threshold, lines=None, minLineLength=None, maxLineGap=None):。如果您不指定参数并使用上述代码,则使用lines 参数,maxLineGap 默认为 0。您是否尝试仅指定maxLineGapminLineLength?这也应该有效。
    • 我尝试按照您的建议仅指定 maxLineGapminLineLength,这仅适用于 10 和 20 的 maxLineGap,但不适用于 0。如果我不指定参数总而言之,它适用于所有maxLineGap 值(0、10 和 20),但我认为我得到的输出是错误的,因为我会将minLineLength 用作lines,并将maxLineGap 用作minLineLength正如你所指出的。但是,如果您在这里查看link,他们使用 HoughLinesP() 而不指定任何参数...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多