Ask Your Question

Hough Circles way too Sensitive

asked 2018-05-31 07:44:14 -0500

abeltan13 gravatar image

updated 2018-06-01 07:37:39 -0500

berak gravatar image


I just downloaded OpenCV today. I decided to give it a test on a really simple example.

So I did Hough Circles on this simple image: image description

And I implemented Hough Circle using python :

import cv2
import numpy as np

img = cv2.imread('a_circle.jpg',0)
#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
edges = cv2.Canny(img,50,150,apertureSize = 3)
circles = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=100,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)

However, I got a very messy circle detection with too many circles!

image description

I must be doing something wrongly! Could you tell me what it is?

Thanks very much!


I changed minDistance from 20 to 50 and I got:

image description

Update 2:

I changed minDistance from 50 to 120 and I got:

The problem has been solved. Thanks sjhalayka!!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-05-31 10:31:35 -0500

sjhalayka gravatar image

updated 2018-05-31 14:49:41 -0500

Can you please delete your code screenshot and replace it with a text version of your code? What happens if you fill in the circles? What happens when you change HoughCircle's fourth parameter minDist from 20 to 50?

image description

The following is the code that I tried. It's based off of your code:

import cv2
import numpy as np

img = cv2.imread('circles.jpg', 0)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50,
                           param1=50, param2=30,
                           minRadius = 100, maxRadius = 0)

for i in circles[0,:]:, (i[0],i[1]), i[2], (0,255,0), 2), (i[0],i[1]), 2, (0,0,255), 3)

cv2.imshow('detected circles', cimg)

edit flag offensive delete link more



I changed the minDist from 20 to 100 and it worked 100%!

Thanks a ton!

Btw, what does the minDist do? Sorry for noob question, I just got the software!

abeltan13 gravatar imageabeltan13 ( 2018-06-01 07:20:03 -0500 )edit

@abeltan13 -- minDist is the minimum distance between circles.

sjhalayka gravatar imagesjhalayka ( 2018-06-01 09:17:08 -0500 )edit

@abeltan13 -- I found this old doc:

Thanks for selecting my answer as correct. Would you also please upvote my answer? :)

sjhalayka gravatar imagesjhalayka ( 2018-06-01 10:09:07 -0500 )edit

Question Tools

1 follower


Asked: 2018-05-31 07:44:14 -0500

Seen: 1,577 times

Last updated: Jun 01 '18