OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Detecting circles in images using OpenCV can be achieved with the Hough Circle Transform. This method identifies circles of a particular radius in a source image. The OpenCV library provides a built-in function to find circles in an image using this method.
Let's go through a step-by-step tutorial to detect circles in an image using OpenCV in Python:
Setup: Install OpenCV if you haven't already:
pip install opencv-python
Read the Image: Begin by reading the image in which you want to detect circles.
import cv2 import numpy as np img = cv2.imread("path_to_image.jpg")
Pre-processing: Convert the image to grayscale and use the Hough gradient method, which requires edge detection. Use the Canny edge detector for this purpose.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3)
Detect Circles:
Use the HoughCircles
function from OpenCV.
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=30, param1=50, param2=30, minRadius=0, maxRadius=0)
Parameters:
image
: 8-bit, single-channel binary source image.cv2.HOUGH_GRADIENT
: The method to detect circles, and it is the only method currently in OpenCV for circle detection.dp
: The inverse ratio of resolution.minDist
: Minimum distance between centers of detected circles.param1
: The higher threshold of the two passed to the Canny edge detector (the lower one is twice smaller).param2
: It is the accumulator threshold for the circle centers. Smaller values will mean more false circle detections.minRadius
: Minimum circle radius.maxRadius
: Maximum circle radius.If circles are detected, the output will be a vector of detected circles. The circles are represented as (x_center, y_center, radius)
.
Draw the Circles: If circles are detected, you can draw them:
if circles is not None: circles = np.uint16(np.around(circles)) for i in circles[0, :]: center = (i[0], i[1]) # center of the circle cv2.circle(img, center, 1, (0, 100, 100), 3) radius = i[2] # radius of the circle cv2.circle(img, center, radius, (255, 0, 255), 2)
Display the Results:
cv2.imshow("Detected Circles", img) cv2.waitKey(0) cv2.destroyAllWindows()
When you run this code, you should see the original image with the detected circles outlined.
Feel free to experiment with the parameters in the HoughCircles
function to adjust circle detection based on your specific image and requirements.
The HoughCircles
function in OpenCV is used for circle detection. It takes the input image, the method for circle detection, and parameters like minimum and maximum circle radius.
import cv2 import numpy as np def detect_circles(image): # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply GaussianBlur to reduce noise blurred = cv2.GaussianBlur(gray, (9, 9), 2) # Use HoughCircles for circle detection circles = cv2.HoughCircles( blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=50, param1=100, param2=30, minRadius=10, maxRadius=100 ) if circles is not None: # Convert circle coordinates to integers circles = np.uint16(np.around(circles)) # Draw circles on the original image for i in circles[0, :]: cv2.circle(image, (i[0], i[1]), i[2], (0, 255, 0), 2) return image
Load an image and apply the circle detection function.
# Load an image image = cv2.imread('circles_image.jpg') # Detect circles in the image result_image = detect_circles(image) # Display the original and circle-detected images cv2.imshow('Original Image', image) cv2.imshow('Circle Detection', result_image) cv2.waitKey(0) cv2.destroyAllWindows()