Change language

Finding Circles Using OpenCV | python

| | | | |

Basics of Circle Detection

A circle can be described by the following equation:

To detect circles, we can fix point (x, y). Now we need to find 3 parameters: a, b and r. So the problem is in the 3D search space. To find possible circles, the algorithm uses a three-dimensional matrix called the Accumulator Matrix to store the potential values ​​of a, b, and r. The value of a (center x coordinate) can range from 1 to rows, b (center y coordinate) can range from 1 to columns, and r can range from 1 to maxRadius = ,

Below are the steps of the algorithm.

  • Accumulator matrix initialization: Dimension row matrix initialization * cols * maxRadius with zeros.
  • Image preprocessing: Apply to the image blur, grayscale and edge detector. This is done to make the circles look like darkened edges of the image.
  • Loop through points: select point in the image.
  • Fixing r and looping through a and b: use a double nested loop to find the value of r by varying a and b in the given ranges.

    for a in range (rows):

    for b in range (cols):

      r = ma th. sqrt ((xi - a) * * 2 + (yi - b) * * 2 )

    accum_matrix [a] [b] [r] + = 1

  • Voting: Select the points in the battery matrix with the maximum value. These are strengths that indicate the existence of a circle with parameters a, b and r. This gives us the space of the circles.
  • Finding circles: Finally, using the above circles as candidates, vote according to the image. The maximum circle of voted in the accumulator matrix gives us a circle.

The HoughCircles function in OpenCV has the following parameters, which can be changed according to the image.

Detection Method: OpenCV has an advanced implementation, HOUGH_GRADIENT, which uses gradient of the edges instead of filling up the entire 3D accumulator matrix, thereby speeding up the process.
dp: This is the ratio of the resolution of original image to the accumulator matrix.
minDist: This parameter controls the minimum distance between detected circles.
Param1: Canny edge detection requires two parameters - minVal and maxVal. Param1 is the higher threshold of the two. The second one is set as Param1 / 2.
Param2: This is the accumulator threshold for the candidate detected circles. By increasing this threshold value, we can ensure that only the best circles, corresponding to larger accumulator values, are returned.
minRadius: Minimum circle radius.
maxRadius: Maximum circle radius.

Below is the code for finding circles using OpenCV in the image above.

import cv2

import numpy as np

# Read the image.

img = cv2.imread ( ’eyes.jpg’ , cv2.IMREAD_COLOR)

# Convert to from shades of gray.

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

# Blur using core 3 * 3.

gray_blurred = cv2.blur (gray, ( 3 , 3 ))

# Apply Hough transform to blurry image .

detected_circles = cv2.HoughCircles ( gray_blurred, 

cv2.HOUGH_GRADIENT, 1 , 20 , param1 = 50 ,

  param2 = 30 , minRadius = 1 , maxRadius = 40 )

# Draw the circles that are found.

if detected_circles is not None :


  # Convert circle parameters a, b and r to integers.

  detected_circles = np.uint16 (np.around (detected_circles))


for pt in detected_circles [ 0 ,:]:

a, b, r = pt [ 0 ], pt [ 1 ], pt [ 2 ]


# Draw a circle. (img, (a, b), r, ( 0 , 255 , 0 ), 2 )


  # Draw a small circle (radius 1) to show the center. (img, (a, b), 1 , ( 0 , 0 , 255 ), 3 )

cv2.imshow ( "Detected Circle" , img)

  cv2.waitKey ( 0 )