JacobM
JacobM

Reputation: 15

Python: Retrieve number of circles found with HoughCircles

I have been looking to use HoughCirlces to count the number of circles that appear in a given image, the image is only black or white and the circles are quite circular as they are from a particle detector and alpha particles come up as circles. The following snippet of code is what I am using currently but once the circles have been detected I don't know how to find out how many were found. Any help would be greatly appreciated.

As a side not if anybody has a good way of finding accurate parameter values for HoughCircles for a given type of image that would also be helpful,

image = cv2.imread(imPath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100)

Upvotes: 0

Views: 4370

Answers (2)

Eternal-student
Eternal-student

Reputation: 21

This question came up on a Google search and didn't work for me (the len(circles) always displays 1). So I used len(circles[0,:]) instead.

Upvotes: 2

P.R.
P.R.

Reputation: 3917

circles is a list with one element per found circle. The documentation states [0]:

"circles – Output vector of found circles. Each vector is encoded as a 3-element floating-point vector (x, y, radius) ."

So len(circles) gives you the number of circles.

[0] http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=houghcircles#houghcircles

Upvotes: 0

Related Questions