count number of circles with opencv and python using the circular hough transform

In this tutorial we are going to count the number of circles of one image using opencv with python. To do that first we need to get the circles using the  circular hough transform.

1. We need one image, I have one segmenteed image:



2. Detect the circles using the circular hough transform


Code:

image = result #segmented image

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

circles = 0

circles = cv2.HoughCircles(gray,

                           cv2.HOUGH_GRADIENT,

                           15, #valor <=29 |

                           41,

                           param1=31,

                           param2=31,

                           minRadius=0,

                           maxRadius=33 # solo valors >=26. Valores entre 30 y mas comienza a detectar zonas afectadas

                          )


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

for i in circles[0,:]:

    # draw the outer circle

    cv2.circle(image,(i[0],i[1]),i[2],(0,255,0),2)

    # draw the center of the circle

    cv2.circle(image,(i[0],i[1]),2,(0,0,255),3)



print("Number of circles: "+ str(len(circles[0,:])))

plt.imshow(image, cmap='gray', vmin=0, vmax=255)

plt.show()





Comentarios