Ask Your Question
1

Hough Circles way too Sensitive

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

abeltan13 gravatar image

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

berak gravatar image

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: 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)
#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)
circles = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=100,maxRadius=0)
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!

image description

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:

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
0

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

sjhalayka gravatar image

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

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,:]:
    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()
edit flag offensive delete link more

Comments

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!

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

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

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

@abeltan13 -- I found this old doc:

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

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

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

Question Tools

1 follower

Stats

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

Seen: 2,735 times

Last updated: Jun 01 '18