Sub topic 13
Convex Hulls and Convexity Defects
Assume you know basic Python, NumPy array indexing, image loading with OpenCV, and how contours are extracted. This chapter focuses on describing whether a contour is concave or close to convex.
The convex hull
A convex hull is the smallest convex shape that contains every point in a contour. A shape is convex when a straight line between any two points stays inside the shape. A crescent, hand silhouette, or star is not convex because it has inward dents.
OpenCV returns the hull as contour points by default:
hull = cv2.convexHull(contour)To calculate convexity defects, request point indexes instead:
hull_indexes = cv2.convexHull(contour, returnPoints=False)These indexes refer back to the original contour. OpenCV needs them to locate the gaps between the contour and its hull.
Convexity defects
A convexity defect is an inward region between a contour and its convex hull. For each defect, OpenCV reports four values:
| Value | Meaning |
|---|---|
| Start index | Hull boundary point where the gap begins |
| End index | Hull boundary point where the gap ends |
| Far index | Deepest contour point in the gap |
| Depth | Distance from the far point to the hull edge |
The depth returned by OpenCV is stored in fixed-point form. Divide it by 256.0 to get the distance in pixels.
import cv2
import numpy as np
points = [
(40, 40), (120, 40), (120, 70), (85, 70),
(85, 110), (120, 110), (120, 140), (40, 140),
(40, 110), (75, 110), (75, 70), (40, 70)
]
contour = np.array(points, dtype=np.int32).reshape((-1, 1, 2))
hull_points = cv2.convexHull(contour)
hull_indexes = cv2.convexHull(contour, returnPoints=False)
defects = cv2.convexityDefects(contour, hull_indexes)
canvas = np.zeros((180, 160, 3), dtype=np.uint8)
cv2.drawContours(canvas, [contour], -1, (255, 0, 0), 2)
cv2.polylines(canvas, [hull_points], True, (0, 0, 255), 2)
if defects is not None:
for start, end, far, depth in defects[:, 0]:
point = tuple(contour[far][0])
print("deepest point:", point, "depth:", depth / 256.0)
cv2.circle(canvas, point, 4, (0, 255, 0), -1)
cv2.imwrite("hull_and_defects.png", canvas)The blue line shows the original contour. The red line shows the hull. Green points mark detected defect locations.
Solidity and filtering defects
Solidity compares the contour area with its hull area:
area = cv2.contourArea(contour)
hull_area = cv2.contourArea(hull_points)
solidity = area / hull_area if hull_area else 0.0
print(f"solidity: {solidity:.3f}")A convex contour has solidity near 1.0. A contour with deep dents has a lower value. Solidity is useful for separating compact objects from highly indented objects, but it does not identify the location of each dent.
Defect detection is sensitive to noise. Small bumps can create many shallow defects. Filter defects by depth or angle before treating them as meaningful features.
Common failure modes
defectscan beNonewhen the contour is already convex.returnPoints=Falseis required for the hull passed tocv2.convexityDefects.- A noisy contour produces false defects. Smooth the mask before finding contours.
- Defect depth depends on image scale. Compare normalized values when objects have different sizes.
- A very small contour can produce unstable measurements.
Exercise
Why does convexityDefects require hull indexes instead of the hull points themselves?
Show answer
It must connect each hull boundary point to the corresponding position in the original contour so it can locate the deepest point and calculate each defect's depth.