# Hough Circles way too Sensitive

Hi,

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:

And I implemented Hough Circle using python :

import cv2
import numpy as np

#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
#bin=cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
#invbin=cv2.bitwise_not(bin)
edges = cv2.Canny(img,50,150,apertureSize = 3)
cv2.imshow('edge',edges)
cv2.waitKey(0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()


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

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

Thanks very much!

Update:

I changed minDistance from 20 to 50 and I got:

Update 2:

I changed minDistance from 50 to 120 and I got:

The problem has been solved. Thanks sjhalayka!!

edit retag close merge delete

Sort by ยป oldest newest most voted

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?

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

import cv2
import numpy as np

cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50,
param1=50, param2=30,

for i in circles[0,:]:
cv2.circle(cimg, (i[0],i[1]), i[2], (0,255,0), 2)
cv2.circle(cimg, (i[0],i[1]), 2, (0,0,255), 3)

cv2.imshow('detected circles', cimg)
cv2.waitKey(0)

cv2.destroyAllWindows()

more

Hi,sjhalayka

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!

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

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

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

@abeltan13 -- I found this old doc:

https://docs.opencv.org/2.4/modules/i...

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

Official site

GitHub

Wiki

Documentation