In this Python OpenCV article iam going to talk about Circle Detection with HoughCircles. so first of all let’s talk about HoughCircles.
Python OpenCV Tutorial : Circle Detection With HoughCircles
OpenCV also has a function for detecting circles, called HoughCircles. It works in a very similar fashion to HoughLines, but where minLineLength and maxLineGap were the parameters to discard or retain lines, HoughCircles has a minimum distance between circles’ centers, minimum, and maximum radius of the circles. Here’s the obligatory.
This is our complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import cv2 import numpy as np # Read image of planets planets = cv2.imread('planet.jpg') # Convert image to grayscale gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY) # Apply median blur to the grayscale image img = cv2.medianBlur(gray_img, 5) # Convert blurred grayscale image back to BGR cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # Detect circles in the image using HoughCircles # Parameters: # - img: the input image # - cv2.HOUGH_GRADIENT: the detection method # - 1: the inverse ratio of the accumulator resolution to the image resolution # - 120: minimum distance between the centers of detected circles # - param1: higher threshold for the Canny edge detector # - param2: threshold for center detection # - minRadius: minimum circle radius # - maxRadius: maximum circle radius circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 120, param1=100, param2=30, minRadius=0, maxRadius=0) # Round circle parameters and convert to unsigned 16-bit integers circles = np.uint16(np.around(circles)) # Loop through each detected circle for i in circles[0, :]: # Draw the outer circle # Parameters: # - planets: the image to draw on # - (i[0], i[1]): the center coordinates of the circle # - i[2]: the radius of the circle # - (0, 255, 0): the color of the circle (green) # - 6: the thickness of the circle cv2.circle(planets, (i[0], i[1]), i[2], (0, 255, 0), 6) # Draw the center of the circle # Parameters: # - planets: the image to draw on # - (i[0], i[1]): the center coordinates of the circle # - 2: the radius of the center point # - (0, 0, 255): the color of the center point (red) # - 3: the thickness of the center point cv2.circle(planets, (i[0], i[1]), 2, (0, 0, 255), 3) # Display image with detected circles cv2.imshow("HoughCircles", planets) # Wait for key press indefinitely cv2.waitKey() # Close all OpenCV windows cv2.destroyAllWindows() |
Also you need to have an image in your working directory, iam using this image.
OK at the top code first we have loaded our image and have done some conversion to the image.
1 2 3 4 |
planets = cv2.imread('planet.jpg') gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY) img = cv2.medianBlur(gray_img, 5) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) |
So this is our HoughCircle algorithm for circle detection, it needs some parameters like input image, dp, minDistance, minRadious and maxRadious.
1 |
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,param1=100,param2=30,minRadius=0,maxRadius=0) |
OK now this line of code is for drawing two circles, an outer big circle and a inner small circle.
1 2 3 4 5 |
for i in circles[0,:]: # draw the outer circle cv2.circle(planets,(i[0],i[1]),i[2],(0,255,0),6) # draw the center of the circle cv2.circle(planets,(i[0],i[1]),2,(0,0,255),3) |
And this is for showing of our image.
1 2 3 |
cv2.imshow("HoughCirlces", planets) cv2.waitKey() cv2.destroyAllWindows() |
So now run the complete code and this will be the result
FAQs:
How to detect circles in OpenCV Python?
For detecting circles in an image using OpenCV Python, you can use HoughCircles function. First, we need to convert the image to grayscale, after that we can apply any necessary preprocessing steps like blurring. and lastly we call the HoughCircles function with appropriate parameters such as the method for circle detection, minimum and maximum radius of circles to be detected, and other parameters related to edge detection and circle detection thresholds.
What is the Hough transform for circle detection?
Hough transform for circle detection is a feature detection technique used to identify circles in images. It works by representing each pixel in the image space as a point in a parameter space, where each point corresponds to a possible circle in the original image. By detecting peaks in this parameter space, the algorithm can identify the center and radius of circles present in the image.
What are houghcircles in OpenCV?
HoughCircles is a function provided by OpenCV library for detecting circles in images using the Hough transform. It takes as input a grayscale image and different parameters related to circle detection, such as the method used for detection, minimum and maximum circle radii, and thresholds for edge detection and circle detection.
Subscribe and Get Free Video Courses & Articles in your Email