>>> cv2.HoughCircles??
HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles
. @brief Finds circles in a grayscale image using the Hough transform.
. @brief 在灰度图中使用Hough变换找到圆圈
.
. The function finds circles in a grayscale image using a modification of the Hough transform.
. 该函数使用梯度版本的Hough变换找到圆圈
.
. Example: :
. @include snippets/imgproc_HoughLinesCircles.cpp
.
. @note Usually the function detects the centers of circles well. However, it may fail to find correct
. radii. You can assist to the function by specifying the radius range ( minRadius and maxRadius ) if
. you know it. Or, you may set maxRadius to a negative number to return centers only without radius
. search, and find the correct radius using an additional procedure.
. @note 通常该函数能够很好的检测圆的中心. 但是其可能会找到错误的半径. 所以如果可能的话, 推荐你使用该
. 函数时指定半径的范围( minRadius 和 maxRadius ). 或者, 你也可以设置 `maxRadius` 为一个负值, 以使该函
. 数只返回圆心, 而通过其他的办法找到正确的半径
.
. @param image 8-bit, single-channel, grayscale input image.
. @param image 8位, 单通道, 灰度图.
. @param circles Output vector of found circles. Each vector is encoded as 3 or 4 element
. floating-point vector $(x, y, radius)$ or $(x, y, radius, votes)$ .
. @param circles 所找到圆的输出向量. 每一个向量被表示为3或4个元素的浮点数向量$(x, y, radius)$ or $(x, y, radius, votes)$.
. @param method Detection method, see #HoughModes. Currently, the only implemented method is #HOUGH_GRADIENT
. @param method 检测方法, 可以去查看 #HoughModes. 目前, 唯一被实现的方法是#HOUGH_GRADIENT
. @param dp Inverse ratio of the accumulator resolution to the image resolution. For example, if
. dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has
. half as big width and height.
. @param dp 图像分辨率与累加器分辨率之比. 举个例子, 若`dp=1`, 则累加器与输入图片有相同的分辨率,
. 若`dp=2`,累加器的宽度和高度只有其一半.
. @param minDist Minimum distance between the centers of the detected circles. If the parameter is
. too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is
. too large, some circles may be missed.
. @param minDist 被检测到的圆心的最小距离. 如果该参数很小, 除了一个正确的圆之外, 该圆的邻居也可能被
. 错误地检测出来. 如果该参数很大, 一些圆将可能被错过
. @param param1 First method-specific parameter. In case of #HOUGH_GRADIENT , it is the higher
. threshold of the two passed to the Canny edge detector (the lower one is twice smaller).
. @param param1 第一个指定方法参数的参数. 在#HOUGH_GRADIENT的情况下,其为传递给Canny边缘检测的两个阈值中
. 较高的阈值(较小的阈值为1/2)
. @param param2 Second method-specific parameter. In case of #HOUGH_GRADIENT , it is the
. accumulator threshold for the circle centers at the detection stage. The smaller it is, the more
. false circles may be detected. Circles, corresponding to the larger accumulator values, will be
. returned first.
. @param param2 第二个指定方法参数的参数. 在#HOUGH_GRADIENT的情况下, 它是检测阶段圆心的累加器阈值.
该值越小, 越多错误的圆将被检测出来. 在投票中获得高票的圆将被先返回.
. @param minRadius Minimum circle radius.
. @param minRadius 最小圆圈半径.
. @param maxRadius Maximum circle radius. If <= 0, uses the maximum image dimension. If < 0, returns
. centers without finding the radius.
. @param maxRadius 最大的圆圈半径. 如果该值等于0, 则使用图像的最大尺寸. 如果小于0, 则返回圆心位置,
. 而不去返回找到的半径
. @sa fitEllipse, minEnclosingCircle
. @sa 相似函数: fitEllipse, minEnclosingCircle
import cv2
import numpy as np
coins = './images/coins.png'
twoCoins = './images/twoCoins.png'
def circleDetec(img_path):
coins_img_gray = cv2.imread(img_path, 0)
coins_img = cv2.imread(img_path)
coins_circle = cv2.HoughCircles(coins_img_gray,
cv2.HOUGH_GRADIENT,
2,
100,
param1=100,
param2=250,
minRadius=100,
maxRadius=200)
circles = coins_circle.reshape(-1, 3)
circles = np.uint16(np.around(circles))
for i in circles:
cv2.circle(coins_img, (i[0], i[1]), i[2], (0, 0, 255), 5) # 画圆
cv2.circle(coins_img, (i[0], i[1]), 2, (0, 255, 0), 10) # 画圆心
cv2.imshow('', coins_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
circleDetec(coins)
circleDetec(twoCoins)
可以看到,下面哪个图,有一些不需要的圆,我们可以改一下参数
coins_circle = cv2.HoughCircles(coins_img_gray,
cv2.HOUGH_GRADIENT,
2,
100,
param1=100,
param2=350, # <---- 将这里的阈值调大一些
minRadius=100,
maxRadius=200)
圆有些不准确,我们可以改一下分辨率,稍微准确了一些
coins_circle = cv2.HoughCircles(coins_img_gray,
cv2.HOUGH_GRADIENT,
1, #<---- 修改分辨率,2->1
100,
param1=100,
param2=100, #<---- 要相应的改变阈值
minRadius=100,
maxRadius=200)