首页 > 解决方案 > TypeError:“NoneType”对象不可迭代崩溃

问题描述

代码是:

lines = cv2.HoughLinesP(masked_image, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)

    # Create an empty black image
    line_image = np.zeros((masked_image.shape[0], masked_image.shape[1], 3), dtype=np.uint8)

    for line in lines: #where the error is happing
        for x1,y1,x2,y2 in line:
            cv2.line(line_image, (x1, y1), (x2, y2), [0, 0, 255], 20)

出现此错误:

Traceback (most recent call last):
  File "linefindinginvideo.py", line 48, in <module>
    for line in lines:
TypeError: 'NoneType' object is not iterable

有关完整代码,请访问我的 GitHub https://github.com/jimhoggey/SelfdrivingcarForza/blob/main/lanemarkinginvideo.py

标签: pythonopencvnonetype

解决方案


这意味着这一行:

cv2.HoughLinesP(masked_image, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)

返回None,其原因是它无法检测到任何行。您将需要调整一些值以使检测正常工作,或者可能是图像中确实没有线条的情况。


推荐阅读