user1388142
user1388142

Reputation: 603

detect circles from the image using Hough Circle transform

I am trying to detect circles from the following image using Hough Circles function of OpenCV

enter image description here

My code (OpenCV with Python)

myImage = cv2.imread("C:\\sample.jpg") 
img = cv2.resize(myImage,(640,480))        
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray,cv2.cv.CV_HOUGH_GRADIENT,1,10, param1=50,param2=35,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(myImage,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',myImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

But due to some reason, I am not able to get the correct outputs. I am getting the following output

enter image description here

UPDATE

Thanks it's working now. By setting the param2 high I am able to detect 2 circles. I was displaying them wrongly and now everything is fine

Upvotes: 2

Views: 1524

Answers (3)

Charles Martin
Charles Martin

Reputation: 63

You are showing the original image cv2.imshow('detected circles',myImage) but the circles are computed over the gray reescales image. Change

cv2.imshow('detected circles',myImage)

for

cv2.imshow('detected circles',img)

and you are done.

Upvotes: 0

Lamar Latrell
Lamar Latrell

Reputation: 1670

Well, one thing is that max radius is set at 0...

i.e. your range is 0 < radius < 0.

Unless I'm mistaken (?) that's a wee bit restrictive yeah?

Upvotes: 0

Aswin P J
Aswin P J

Reputation: 561

You seem to have given the coordinates wrong.

    # draw the outer circle
    cv2.circle(myImage,(i[1],i[0]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[1],i[0]),2,(0,0,255),3)

Change that to

    # draw the outer circle
    cv2.circle(myImage,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[0],i[1]),2,(0,0,255),3)

Upvotes: 1

Related Questions