霍夫圆检测 AttributeError: 'NoneType'对象没有 'rint'属性。

5

我正在尝试使用OpenCV2中的Houghcircle检测此圆,但是出现了错误。

enter image description here

以下是我的代码。

1

chh = cv2.HoughCircles(crr, cv2.HOUGH_GRADIENT, 1,minDist = 50, param1 =200, 
param2 = 18, minRadius = 20, maxRadius =60)

[2]

ch = np.uint16(np.around(ch)) #error appears to come from here

我假设1会找到圆,而[2]将其转换为数组,我怀疑是np.around的问题。
非常感谢您的解释。
完整错误:
AttributeError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 55 try: ---> 56 return getattr(obj, method)(*args, **kwds) 57 AttributeError: 'NoneType' object has no attribute 'round'
在处理上述异常期间,发生了另一个异常:
AttributeError Traceback (most recent call last) in ----> 1 ch = np.uint16(np.around(ch)) # 错误似乎来自这里
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in around(a, decimals, out) 3005 3006 """ -> 3007 return _wrapfunc(a, 'round', decimals=decimals, out=out) 3008 3009
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 64 # a downstream library like 'pandas'. 65 except (AttributeError, TypeError): ---> 66 return _wrapit(obj, method, *args, **kwds) 67 68
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapit(obj, method, *args, **kwds) 44 except AttributeError: 45 wrap = None ---> 46 result = getattr(asarray(obj), method)(*args, **kwds) 47 if wrap: 48 if not isinstance(result, mu.ndarray):
AttributeError: 'NoneType' object has no attribute 'rint'

[1]中正在创建chh,而[2]中未定义ch - Dschoni
抱歉,如果我在第二段代码中插入“chh”,我仍然会得到相同的错误。 - quadhd
1个回答

3

这里有一个使用cv2.HoughCircles进行圆形检测的简单示例。

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find circles with HoughCircles
circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 1, minDist=150, param1=200, param2=18, minRadius=20)

# Draw circles
if circles is not None:
    circles = np.round(circles[0, :]).astype("int")
    for (x,y,r) in circles:
        cv2.circle(image, (x,y), r, (36,255,12), 3)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接