Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compute the area and perimeter of an image contour using OpenCV Python?
OpenCV provides powerful functions to compute the area and perimeter of image contours. A contour is a curve joining all continuous points along a boundary with the same color or intensity, making them essential for shape analysis and object detection.
To compute area and perimeter, we first detect contours using cv2.findContours(), then apply cv2.contourArea() and cv2.arcLength() functions respectively.
Syntax
The functions use the following syntax:
area = cv2.contourArea(cnt) perimeter = cv2.arcLength(cnt, True)
Where cnt is a NumPy array containing the contour points of an object.
Steps to Compute Area and Perimeter
Follow these steps to compute contour measurements:
Step 1: Import Libraries and Read Image
import cv2
import numpy as np
# Read image and convert to grayscale
img = cv2.imread('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Step 2: Apply Thresholding
# Create binary image using thresholding ret, thresh = cv2.threshold(gray, 150, 255, 0)
Step 3: Find Contours
# Detect contours in the binary image contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Step 4: Calculate Area and Perimeter
# For each contour, compute measurements
for cnt in contours:
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt, True)
print(f'Area: {area}, Perimeter: {perimeter}')
Example 1: Single Pentagon Shape
This example computes the area and perimeter of a pentagon shape:
import cv2
import numpy as np
# Create a simple pentagon shape for demonstration
img = np.zeros((300, 300, 3), dtype=np.uint8)
points = np.array([[150, 50], [200, 100], [180, 170], [120, 170], [100, 100]], np.int32)
cv2.fillPoly(img, [points], (255, 255, 255))
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply thresholding
ret, thresh = cv2.threshold(gray, 150, 255, 0)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours found:", len(contours))
# Calculate area and perimeter for the first contour
if contours:
cnt = contours[0]
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt, True)
print(f'Area: {area}')
print(f'Perimeter: {round(perimeter, 2)}')
# Draw contour on original image
cv2.drawContours(img, [cnt], -1, (0, 255, 0), 2)
Number of contours found: 1 Area: 7800.0 Perimeter: 314.64
Example 2: Multiple Shapes
This example demonstrates computing measurements for multiple contours:
import cv2
import numpy as np
# Create image with multiple shapes
img = np.zeros((400, 500, 3), dtype=np.uint8)
# Draw different shapes
cv2.rectangle(img, (50, 50), (150, 150), (255, 255, 255), -1)
cv2.circle(img, (300, 100), 50, (255, 255, 255), -1)
cv2.ellipse(img, (400, 250), (60, 40), 0, 0, 360, (255, 255, 255), -1)
# Convert to grayscale and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, 0)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(f"Number of contours found: {len(contours)}")
# Calculate area and perimeter for each contour
for i, cnt in enumerate(contours):
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt, True)
print(f'Contour {i+1} - Area: {area}, Perimeter: {round(perimeter, 2)}')
# Draw contour
cv2.drawContours(img, [cnt], -1, (0, 255, 0), 2)
Number of contours found: 3 Contour 1 - Area: 10000.0, Perimeter: 400.0 Contour 2 - Area: 7854.0, Perimeter: 314.16 Contour 3 - Area: 7540.0, Perimeter: 384.88
Key Parameters
Understanding the function parameters:
- cv2.contourArea(cnt): Returns the area enclosed by the contour
- cv2.arcLength(cnt, closed): Returns the perimeter length
-
closed parameter: Set to
Truefor closed contours,Falsefor open curves
Practical Applications
These measurements are useful for:
- Object classification based on size
- Quality control in manufacturing
- Medical image analysis
- Computer vision shape recognition
Conclusion
OpenCV's cv2.contourArea() and cv2.arcLength() functions provide accurate measurements of contour dimensions. These tools are essential for shape analysis and object detection applications in computer vision projects.
