Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

Unable to detect circles with HOUGH_GRADIENT_ALT (OpenCV Python)

asked 2020-10-19 20:08:43 -0500

thunr gravatar image

updated 2020-10-20 01:55:59 -0500

berak gravatar image

Hello, I'm trying to use the HOUGH_GRADIENT_ALT mode of the Hough Circles package and I'm having a lot of trouble. I am not even to detect a circle in a very simple test case (I attached a low resolution copy), let alone for the intended use. I did a basic parameter sweep for p1 and p2, which was a successful strategy for me using the HOUGH_GRADIENT mode. Based off my understanding of the documentation, calling the function with these parameters should work for a clearly defined circle.

cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT_ALT, 1.5, 1, 300, 0.9)

I've tried a combination of values for dp (0.5,1,1.5), parameter 1 (100-10000 in increments of 100), and parameter 2 (0.1 to 1.0 in increments of 0.1), none of which returned a single circle. I also confirmed that the original mode worked as expected, so I'm not sure what caused this change in behavior. I would appreciate any advice that someone could give because I'm really at a loss as to what's gone wrong. I included an example workflow below in case there's something I'm doing incorrectly beforehand.

import cv2
input_tif = '/path/to/tif'
image = cv2.imread(input_tif)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT_ALT, 1.5, 1, 300, 0.9)


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2020-11-15 17:54:42 -0500

This is a nice solution, can it detect multiple circles on an image?

edit flag offensive delete link more

answered 2020-10-20 07:46:45 -0500

supra56 gravatar image

updated 2020-10-22 23:07:56 -0500

Try this:

#!/usr/bin/env python
#OpenCV 4.4.0, RPI 3B/3B+, 4B/4/8gb, Buster v10
#Date: 21th October, 2020

import cv2
import numpy as np
image = cv2.imread('circle.png')
output = image.copy()
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find circles
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.5, 100)
# If some circle is found
if circles is not None:
   # Get the (x, y, r) as integers
   circles = np.round(circles[0, :]).astype("int")
   # loop over the circles
   for (x, y, r) in circles:, (x, y), r, (0, 255, 0), 2)
# show the output image
cv2.imwrite('circleA.png', output)


image description

If u using this cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT_ALT, 1.5, 1, 300, 0.9). U get this. Output:

image description

Apologised. I didn't see this cv2.HOUGH_GRADIENT_ALT. Here is code:

import cv2

img = cv2.imread('circle.png')
img2 = img.copy()
img_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT_ALT,
                            2, 30, param1=300,
                            param2=0.85, minRadius=20)

for circle in circles[0]:
    center_x, center_y, radius = circle, (center_x, center_y), int(radius),(0, 0, 255), 2)

cv2.imwrite('circle4.png', img2) 
cv2.imshow("img2", img2)


image description

edit flag offensive delete link more



Hi, thanks for your response and advice. I get an identical output to yours when I call HOUGH_GRADIENT, but I when I call HOUGH_GRADIENT_ALT with the parameters above, I don't get any outputs. May I ask how you installed OpenCV? I'm wondering if there is maybe something wrong with my pip install.

thunr gravatar imagethunr ( 2020-10-22 11:22:34 -0500 )edit

I used linux for raspberry pi. Opencv 4.5 and python 3.7.3.

supra56 gravatar imagesupra56 ( 2020-10-22 22:26:04 -0500 )edit

Don't used this w/out keyword 1.5, 1, 300, 0.9. U will have to play around values. Too lazy for me. Btw, it will work on pc too.

supra56 gravatar imagesupra56 ( 2020-10-22 23:05:15 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-10-19 20:08:43 -0500

Seen: 145 times

Last updated: Oct 22