How to find the minimum enclosing circle of an object in OpenCV Python?


A minimum enclosing circle (circumcircle) of an object is a circle which completely covers the object with minimum area. We can find the minimum enclosing circle of an object using the function cv2.minEnclosingCircle().

Syntax

The syntax of this function is −

(x,y),radius = cv2.minEnclosingCircle(cnt)

Where, "cnt" are the contour points. It is represented as an array of contour points.

Output − It returns coordinate of center (x,y) and radius of minimum enclosing circle. (x,y) and radius are of float dtype. So, to draw a circle on the image, we convert them to integers.

To draw a minimum enclosing circle, we use the function same as the function to draw the circle on an image −

cv2.circle(img,center,radius,(0,255,0),2)

Steps

You can use the following steps to find the minimum enclosing circle of an object −

Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.

import cv2

Read the input image using cv2.imread() and convert it to grayscale. Here we are loading an image naled fourpoint-star.png.

img = cv2.imread('fourpoint-star.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Apply thresholding on the grayscale image to create a binary image. Adjust the second argument for better contour detection.

ret,thresh = cv2.threshold(gray,150,255,0)

Find the contours in the image using cv2.findContours() function.

contours, _ = cv2.findContours(thresh,  cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

Select a contour cnt or loop over the all contours. Find the minimum enclosing circle for the contour cnt using cv2.minEnclosingCircle(cnt) function.

cnt = contours[0]
(x,y),radius = cv2.minEnclosingCircle(cnt)

Draw the minimum enclosing circle on the input image passing center and radius to the below function. Third and fourth arguments are the color and thickness of the circle drawn.

center = (int(x),int(y))
radius = int(radius)
cv2.circle(img,center,radius,(0,255,0),2)

Display the image with the drawn Convex Hull.

cv2.imshow("Circle", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Let's have a look at some examples for more clear understanding.

Example 1

In the Python program below, we detect the contour of an object in the image and find the minimum enclosing circle of the object. We also draw the minimum enclosing circle on the input image.

# import required libraries import cv2 # load the input image img = cv2.imread('fourpoint-star.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding to convert grayscale to binary image ret,thresh = cv2.threshold(gray,150,255,0) # find the contours contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of contours detected:", len(contours)) # select the first contour cnt = contours[0] # find the minimum enclosing circle (x,y),radius = cv2.minEnclosingCircle(cnt) # convert the radius and center to integer number center = (int(x),int(y)) radius = int(radius) # Draw the enclosing circle on the image cv2.circle(img,center,radius,(0,255,0),2) cv2.imshow("Circle", img) cv2.waitKey(0) cv2.destroyAllWindows()

We will use this image as the Input File in this program −

Output

When you execute the above code, it will produce the following output −

Number of contours detected: 1

And, we get the following output window −

The minimum enclosing circle of the detected object is drawn in green color.

Example 2

In this example, we detect the contours of objects in the image and find the minimum enclosing circle of the objects. We also draw all minimum enclosing circles on the input image.

import cv2 import matplotlib.pyplot as plt img = cv2.imread('shape.png') img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(img1,150,255,0) contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of objects detected:", len(contours)) # find the enclosing circle for all the contours in the image for cnt in contours: (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) img = cv2.circle(img,center,radius,(0,0,255),3) # Display the image cv2.imshow("Circle", img) cv2.waitKey(0) cv2.destroyAllWindows()

We will use this image as the Input File in this program −

Output

When we execute the above code, it will produce the following output

Number of contours detected: 3

And we get the following output window −

The minimum enclosing circles of the detected objects are shown in red color.

Updated on: 28-Sep-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements