Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv2.HoughCircles is NOT the best option here because it can detect only circles, but some planets look like ellipse, not correct circle. I recommend to try findContours here. Anyway question is about houghCircles, so example of code that detects 3 most circle-like planets:

import cv2
import numpy as np

planets = cv2.imread(path)
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,1,10,
                        param1=150,param2=30,minRadius=3,maxRadius=100)
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()

Not sure if you can achive better results here with hough circles. What I corrected in your code: set minimum distance between circles centers from 120 to 10, changed min and max radius for circle. Hope this helps!

not sure that cv2.HoughCircles is NOT the best option here because it can detect only circles, but some planets look like ellipse, not correct circle. I recommend to try findContours here. Anyway question is about houghCircles, so example of code that detects 3 most circle-like some planets:

import cv2
import numpy as np

planets = cv2.imread(path)
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,1,10,
                        param1=150,param2=30,minRadius=3,maxRadius=100)
gray_img = cv2.GaussianBlur(gray_img, (5,5), 0)
circles = cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,0.01,5,
                    param1=350,param2=10,minRadius=4,maxRadius=10000)
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()

Not sure if you can achive better results here with hough circles. I recommend to play around param1 and param2

What I corrected in your code: set minimum distance between circles centers from 120 to 10, changed min and max radius for circle. circle, changed param1 and param2

Hope this helps!

not sure that cv2.HoughCircles is the best option here because it can detect only circles, but some planets look like ellipse, not correct circle. I recommend to try findContours here. Anyway question is about houghCircles, so example of code that detects some planets:

import cv2
import numpy as np

planets = cv2.imread(path)
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
gray_img = cv2.GaussianBlur(gray_img, (5,5), 0)
circles = cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,0.01,5,
                    param1=350,param2=10,minRadius=4,maxRadius=10000)
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()

Not sure if you can achive better results here with hough circles. I recommend to play around param1 and param2param2 and try to change blur params also

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

Hope this helps!

not Not sure that cv2.HoughCircles is the best option here because it can detect only circles, but some planets look like ellipse, not correct circle. here. I recommend to try totry findContours here. also. Anyway question is about houghCircles, so example of code that detects some planets:all planets correctly:

import cv2
import numpy as np

planets = cv2.imread(path)
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
gray_img = cv2.GaussianBlur(gray_img, (5,5), 0)
cv2.medianBlur(gray_img, 3)
circles = cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,0.01,5,
cv2.HoughCircles(gray_img,cv2.HOUGH_GRADIENT,0.01,10,
 param1=350,param2=10,minRadius=4,maxRadius=10000)
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()

Not sure if you can achive better results here with hough circles. I recommend to play around param1 and param2 and try to change blur params also

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

Docs that I used: link

Hope this helps!

Not sure that cv2.HoughCircles is the best option here. I recommend totry 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,0.01,10,
cv2.HoughCircles(gray_img, cv2.HOUGH_GRADIENT, 1, 10,
 param1=350,param2=10,minRadius=2,maxRadius=13)
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 param2param2. Also I adde check for case if no circles were found.

Docs that I used: link

Hope this helps!