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 find the solidity and equivalent diameter of an object in an image using OpenCV Python?
Solidity is the ratio of contour area to its convex hull area, measuring how "solid" or compact an object is. Equivalent diameter is the diameter of a circle with the same area as the contour. Both properties help analyze object shape characteristics in computer vision.
Understanding the Concepts
To compute solidity and equivalent diameter, we need ?
- Contour area ? area enclosed by the object boundary
- Convex hull ? smallest convex polygon containing all contour points
- Hull area ? area of the convex hull
Syntax
area = cv2.contourArea(cnt) hull = cv2.convexHull(cnt) hull_area = cv2.contourArea(hull) solidity = float(area)/hull_area equi_diameter = np.sqrt(4*area/np.pi)
Where cnt is a numpy array of contour points for an object in the image.
Complete Example ? Single Object
This example computes solidity and equivalent diameter for one object ?
import cv2
import numpy as np
# Create a sample image with a rectangle
img = np.zeros((200, 300, 3), dtype=np.uint8)
cv2.rectangle(img, (50, 50), (150, 120), (255, 255, 255), -1)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply thresholding
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Select the first contour
cnt = contours[0]
# Calculate solidity
area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
solidity = float(area) / hull_area
# Calculate equivalent diameter
equi_diameter = np.sqrt(4 * area / np.pi)
# Draw convex hull
cv2.drawContours(img, [hull], -1, (0, 255, 255), 2)
# Add text
cv2.putText(img, f'Solidity: {solidity:.2f}', (50, 180),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
cv2.putText(img, f'Eq. Diameter: {equi_diameter:.1f}', (50, 195),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
print(f"Solidity: {solidity:.3f}")
print(f"Equivalent Diameter: {equi_diameter:.2f}")
print(f"Area: {area}")
print(f"Hull Area: {hull_area}")
# Display result
cv2.imshow("Solidity & Equivalent Diameter", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Multiple Objects Example
For analyzing multiple objects in an image ?
import cv2
import numpy as np
# Create image with multiple shapes
img = np.zeros((300, 400, 3), dtype=np.uint8)
# Draw different shapes
cv2.rectangle(img, (50, 50), (120, 100), (255, 255, 255), -1)
cv2.circle(img, (200, 75), 30, (255, 255, 255), -1)
pts = np.array([[300, 50], [350, 50], [375, 90], [325, 110], [275, 90]], np.int32)
cv2.fillPoly(img, [pts], (255, 255, 255))
# Convert to grayscale and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(f"Number of objects detected: {len(contours)}")
for i, cnt in enumerate(contours):
# Calculate properties
area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
solidity = float(area) / hull_area
equi_diameter = np.sqrt(4 * area / np.pi)
# Get bounding rectangle for text positioning
x, y, w, h = cv2.boundingRect(cnt)
# Draw convex hull
cv2.drawContours(img, [hull], -1, (0, 255, 255), 2)
# Add text
cv2.putText(img, f'S:{solidity:.2f}', (x, y-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
cv2.putText(img, f'D:{equi_diameter:.1f}', (x, y+h+15),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1)
print(f"Object {i+1}: Solidity = {solidity:.3f}, Eq. Diameter = {equi_diameter:.2f}")
cv2.imshow("Multiple Objects Analysis", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Key Points
- Solidity = 1.0 indicates a perfectly convex shape
- Lower solidity means the shape has more concave areas
- Equivalent diameter helps compare object sizes regardless of shape
- Both measures are useful for object classification and shape analysis
Comparison
| Shape Type | Typical Solidity | Description |
|---|---|---|
| Circle/Square | ~1.0 | Highly convex |
| Star | 0.6-0.8 | Moderately concave |
| Crescent | 0.3-0.6 | Highly concave |
Conclusion
Solidity and equivalent diameter are valuable shape descriptors in computer vision. Use solidity to measure object compactness and equivalent diameter to compare sizes across different shapes.
