You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV HoughCircles参数设置与报错问题求助

金币检测:OpenCV HoughCircles 参数调试问题与报错求助

我使用以下图片进行金币检测:
金币图片

我无法理解如何设置OpenCV中HoughCircles函数的minRadius、maxRadius等参数以检测出所有金币。另外,我已给dp滑动条的值加1避免其为0,但调整maxR滑动条时出现如下报错:

AttributeError: 'NoneType' object has no attribute 'rint'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/noemi/Escritorio/morphology/hougbarra.py", line 43, in <module>
    circles = np.uint16(np.around(circles))
  File "<__array_function__ internals>", line 200, in around
  File "/home/noemi/Escritorio/morphology/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 3337, in around
    return _wrapfunc(a, 'round', decimals=decimals, out=out)
  File "/home/noemi/Escritorio/morphology/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 54, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "/home/noemi/Escritorio/morphology/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 43, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: loop of ufunc does not support argument 0 of type NoneType which has no callable rint method

我使用的代码如下:

import cv2
import numpy as np

def nada(x):
    pass

image = cv2.imread("money.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
filas, columnas, _ = image.shape
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# 创建窗口和滑动条
cv2.namedWindow("Hough con barras")
cv2.createTrackbar("dp", "Hough con barras", 1, 100, nada)
cv2.createTrackbar("minDist", "Hough con barras", 1, 100, nada)
cv2.createTrackbar("minR", "Hough con barras", 50, 100, nada)
cv2.createTrackbar("maxR", "Hough con barras", 10, 100, nada)
cv2.createTrackbar("param1", "Hough con barras", 100, 100, nada)
cv2.createTrackbar("param2", "Hough con barras", 100, 100, nada)

while True:
    img2 = image.copy()
    dpd = cv2.getTrackbarPos("dp", "Hough con barras") + 1
    minDist = cv2.getTrackbarPos("minDist", "Hough con barras") + 1
    param1 = cv2.getTrackbarPos("param1", "Hough con barras") + 1
    param2 = cv2.getTrackbarPos("param2", "Hough con barras") + 1
    minR = cv2.getTrackbarPos("minR", "Hough con barras") +1
    maxR = cv2.getTrackbarPos("maxR", "Hough con barras") + 1

    # 使用HoughCircles检测圆
    circles = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, int(dpd), int(columnas / minDist), param1=int(param1), param2=int(param2),
                               minRadius=int(columnas / minR), maxRadius=int(columnas / maxR))
    # 找到并绘制轮廓
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        # 绘制外圆
        cv2.circle(img2, (i[0], i[1]), i[2], (0, 255, 0), 2)
        # 绘制圆心
        cv2.circle(img2, (i[0], i[1]), 2, (0, 0, 255), 3)

    cv2.imshow("Hough con barras", img2)
    if cv2.waitKey(1) == ord("s"):
        break

cv2.destroyAllWindows()

滑动条本应便于调试参数,但我不清楚各参数的正确设置方式,希望得到帮助。


内容的提问来源于stack exchange,提问作者Noemí

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 18:35:55