Python-openCv可检测出给出奇怪结果的圆

时间:2018-10-03 21:15:21

标签: python opencv object-detection

我正在使用https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/#comment-480634中解释的代码,并试图基本上检测出该示例instagram页面下半部分(附加)中显示的较小的圆形轮廓图像(准确地说是5个)。我不知道为什么: 1.代码捕获了5个小圆形轮廓圆圈中的仅一个 2.为什么页面上显示一个大圆圈,这对我来说似乎很荒唐。 这是我正在使用的代码:

# we create a copy of the original image so we can draw our detected circles 
# without destroying the original image.
image = cv2.imread("instagram_page.png")

# the cv2.HoughCircles function requires an 8-bit, single channel image, 
# so we’ll convert from the RGB color space to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# detect circles in the image. We pass in the image we want to detect circles as the first argument, 
# the circle detection method as the second argument (currently, the cv2.cv.HOUGH_GRADIENT method 
# is the only circle detection method supported by OpenCV and will likely be the only method for some time),
# an accumulator value of 1.5 as the third argument, and finally a minDist of 100 pixels.
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.7, minDist= 1, param1 = 300, param2 = 100, minRadius=3, maxRadius=150)

print("Circles len -> {}".format(len(circles)))


# ensure at least some circles were found
if circles is not None:    
    # convert the (x, y) coordinates and radius of the circles to integers
    # converting our circles from floating point (x, y) coordinates to integers, 
    # allowing us to draw them on our output image.
    circles = np.round(circles[0, :]).astype("int")

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        orange = (39, 127, 255)
        cv2.circle(output, (x, y), r, orange, 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)


img_name = "Output"
cv2.namedWindow(img_name,cv2.WINDOW_NORMAL)
cv2.resizeWindow(img_name, 800,800)
cv2.imshow(img_name, output)
cv2.waitKey(0)    
cv2.destroyAllWindows()

我使用minDist = 1来确保可能捕获到那些小圆圈。有人看到我的参数完全错误吗?enter image description here

1 个答案:

答案 0 :(得分:2)

我使用这些参数并设法检测到所有圆圈(Ubuntu 16.04 LTS x64,Python 3.7,numpy==1.15.1python-opencv==3.4.3):

circles = cv2.HoughCircles(
    gray,
    cv2.HOUGH_GRADIENT,
    1.7,
    minDist=100,
    param1=48,
    param2=100,
    minRadius=2,
    maxRadius=100
)

enter image description here