我正在尝试使用 OpenCV 中的 Hough Circle Detection 和 Python 从笔记本电脑的默认摄像机中查找实时视频供稿中的圆圈,但它可以检测视频中的数百个圆圈。
这是我的代码:
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()
我也尝试通过播放文件中的视频来尝试它,但是没有用。
请帮助!
答案 0 :(得分:1)
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()