Skip to content
Advertisement

Returning the count of circles in the document

I have written code defining circles in documents. Can I return to the variable the count of circles found in the document? Thank you.

img = cv2.imread(r"some parth to doc")

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

minDist = 100
param1 = 30 #500
param2 = 100 #200 #smaller value-> more false circles
minRadius = 90
maxRadius = 200 #10

# docstring of HoughCircles: HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)

if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)

# Show result for testing:
imS = cv2.resize(img, (960, 540))
cv2.imshow('img', imS)
cv2.waitKey(0)

Advertisement

Answer

Well, you’re already doing

for i in circles[0, :]:

to draw the circles, so circles[0, :] is presumably an iterable of circles.

Wouldn’t it then make sense that

n_circles = len(circles[0, :])

would give you the number of circles to draw?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement