Ask Your Question
1

Tuning parameters for cv2.houghcircle() method ?

asked 2017-05-04 05:47:56 -0600

MayankSoni gravatar image

I am unable to get the desired parameters after applying the cv2.houghcircle() method

MY CODE:

import cv2
import numpy as np

planets = cv2.imread('E:/Users/msoni3/Desktop/solar.jpg')
#planets = cv2.resize(planets, (1000,600))
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
#img = cv2.medianBlur(gray_img, 5)
img = cv2.GaussianBlur(gray_img, (3,3), 0)

cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,
                        param1=100,param2=35,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))

for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(planets,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(planets,(i[0],i[1]),2,(0,0,255),3)

cv2.imwrite("planets_circles.jpg", planets)
cv2.imshow("HoughCirlces", planets)
cv2.waitKey()
cv2.destroyAllWindows()

MY INPUT IMAGE: image description

and my output is My Output

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-05-04 13:57:34 -0600

electron gravatar image

updated 2017-05-04 14:17:21 -0600

Not sure that cv2.HoughCircles is the best option here. I recommend to try findContours also. Anyway question is about houghCircles, so example of code that detects all planets correctly:

import cv2
import numpy as np

planets = cv2.imread(path)
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
gray_img = cv2.medianBlur(gray_img, 3)
circles = cv2.HoughCircles(gray_img, cv2.HOUGH_GRADIENT, 1, 10,
                    param1=350, param2=10, minRadius=2, maxRadius=13)
if circles is not None:  # circles found
    circles = np.uint16(np.around(circles))

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

cv2.imshow("HoughCirlces", planets)
cv2.waitKey()
cv2.destroyAllWindows()

What I corrected in your code: set minimum distance between circles centers from 120 to 10, changed min and max radius for circle, changed param1 and param2. Also I adde check for case if no circles were found.

Docs that I used: link

Hope this helps!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-05-04 05:47:56 -0600

Seen: 1,828 times

Last updated: May 04 '17