首页 > 解决方案 > opencv lib 错误:AttributeError: 'NoneType' 对象没有属性 'shape'

问题描述

我将图像的引用作为参数传递给函数。应该可以将引用列表传递给图像,因此使用列表。代码看起来像这样

def cleanOCR(borders):
    detectedOCR = []
    print(f'Borders = {borders}')
    for i, image in enumerate(borders):
        image = cv2.imread(image)
        assert not isinstance(image,type(None)), 'image not found'
        edges = cv2.Canny(image, 50, 150, apertureSize=3)
        lines = cv2.HoughLinesP(image=edges, rho=1, theta=np.pi / 180, threshold=100, lines=np.array([]), minLineLength=100, maxLineGap=80)    
        a, b, c = lines.shape

但我得到一个错误

  File "platesOCR.py", line 66, in cleanOCR
    a, b, c = lines.shape
AttributeError: 'NoneType' object has no attribute 'shape'

我使用 cv2 版本 '4.1.1' 代码不是我的,我刚刚开始处理 OpenCV。

print(f'Borders = {borders}') give me right file path

标签: pythonopencv

解决方案


这本质上意味着在图像中没有检测到线条。

要检查这一点,只需打印行。我猜它会是空的。

要不得到一个空的 np 数组,请尝试减少 minLineLength 值。图像中短于该值的线段将被拒绝。所以我建议减少这个值。

还有maxLineGap。连接它们的同一条线上的点之间允许的最大间隙。所以你的价值似乎更高。


推荐阅读