为什么 HoughCircles 检测到虚假圆圈?

Why HoughCircles is detecting false circles?

我正在尝试使用 OpenCV 中的 Hough Circle DetectionPython,但它在视频中检测到数百个圆圈。

这是我的代码:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.medianBlur(gray, 5)

    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

    detected_circles = np.uint16(np.around(circles))

    for (x, y, r) in detected_circles[0, :]:
        cv2.circle(gray, (x, y), r, (0, 255, 255), 2)

    cv2.imshow('Live', gray)
    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()

我也尝试过从文件中播放视频,但没有用。

请帮忙!!

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

尝试将 minRadius 参数更改为更大的值,例如 5 或更多。

代码如下:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    frame = cv2.medianBlur(frame, 5)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=10, maxRadius=20)
    if circles is not None:
        detected_circles = np.uint16(np.around(circles))
        for (x, y, r) in detected_circles[0, :]:
            cv2.circle(frame, (x, y), r, (0, 255, 255), 2)

    cv2.imshow('Live', frame)
    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()