Skip to content
AI360Xpert
Beta

Convex Hull

Shrink-wrap a contour in the smallest convex polygon that holds it. The gaps between wrap and shape reveal fingers, notches, and every concave secret.

The hull is the smallest convex polygon holding every point: six points give four hull vertices and two interior points
The hull is the smallest convex polygon holding every point: six points give four hull vertices and two interior points

Why Does This Exist?

Area and moments weigh a shape but cannot see dents: a starfish and its shrink-wrapped blob have similar area yet differ exactly where the arms indent. Gesture interfaces need those indentations — fingertips are hull vertices, the webbing between fingers is gaps — and part inspection needs concavity as a measurable feature. The convex hull supplies both by separating what bulges out from what caves in.

This page covers the hull definition, the linearithmic construction, and convexity defects. Contour detection finds the outlines the hull then shrink-wraps.

The one idea you need first

A set is convex when the straight segment between any two of its points stays inside the set. A circle is convex; a crescent is not, because a segment across its bay leaves the shape. The hull is the smallest convex set that still contains every point you started with.

Think of It Like This

A rubber band around pegs

Hammer pegs into a board along the shape's outline and snap a rubber band around all of them. The band touches only the outermost pegs and spans straight across every bay between headlands. Touched pegs are hull vertices, spanned bays are concavities, and the deepest water under each span measures the defect depth.

It stops holding for interiors. The band never learns about holes or inner walls, the same way the hull of a washer equals the hull of a solid disk. Interior detail needs contour hierarchy, never the hull.

How It Actually Works

The smallest convex container

The convex hull of a point set is the smallest convex polygon containing every point: any segment between two hull points stays inside. Construction sorts points by xx (then yy) and builds upper and lower chains, keeping only left turns for a counterclockwise hull. Sorting dominates, so Andrew's monotone chain runs in O(nlog⁡n)O(n \log n) time.

Worked example

Trace six points: (0,0)(0,0), (2,1)(2,1), (4,0)(4,0), (1,3)(1,3), (3,3)(3,3), (2,0.5)(2,0.5). Sorted by xx they run (0,0)(0,0), (1,3)(1,3), (2,0.5)(2,0.5), (2,1)(2,1), (3,3)(3,3), (4,0)(4,0). Walking the lower chain, each right turn or straight step discards the middle point: (1,3)(1,3) is discarded when (2,0.5)(2,0.5) arrives, since the turn at (1,3)(1,3) heads back down. The band ends up touching (0,0)(0,0), (4,0)(4,0), (3,3)(3,3), (1,3)(1,3) and spanning over (2,1)(2,1) and (2,0.5)(2,0.5), which sit strictly inside. Four hull vertices, two interior points, and the signed area 99 (positive) confirms counterclockwise order.

Defects, solidity, and fingers

Each hull edge plus the contour stretch beneath it defines one convexity defect: start point, end point, farthest contour point, and depth to the edge. OpenCV returns depths in fixed-point units, so scale before thresholding. Solidity — contour area over hull area — compresses the same story to one number: near 11 for blobs, well below for stars. A raised hand shows five hull vertices at the fingertips with four deep defects between them, which is the entire gesture feature in eight numbers.

Code

Monotone-chain hull on the fixture points:

pts = [(0, 0), (2, 1), (4, 0), (1, 3), (3, 3), (2, 0.5)]
def cross(o, a, b):    return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
pts = sorted(set(pts))lower, upper = [], []for p in pts:    while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:        lower.pop()    lower.append(p)for p in reversed(pts):    while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:        upper.pop()    upper.append(p)
hull = lower[:-1] + upper[:-1]print(f"hull {hull}")# -> hull [(0, 0), (4, 0), (3, 3), (1, 3)]

Four vertices, counterclockwise, with (2,1)(2,1) and (2,0.5)(2,0.5) correctly excluded as interior.

Watch Out For

Hull spikes from unfiltered speckle

Symptom: one stray bright pixel becomes a hull vertex and invents a huge defect across the real shape. The hull is maximally sensitive to outliers by construction — the farthest point always wins.

Approximate the contour and drop specks with a morphological open before hulling, and distrust any defect whose start or end sits on an isolated vertex.

Thresholding absolute defect depths

Symptom: a fixed depth cutoff counts fingers on adult hands but misses children's, because depth scales with hand size in pixels. Defect depth is an absolute pixel distance, not a proportion.

Normalize by the hull edge length or the hand's bounding size before comparing, and calibrate the cutoff per camera distance.

The Quick Version

  • The convex hull is the smallest convex polygon holding every contour point, built in O(nlog⁡n)O(n \log n).
  • Hull vertices mark protrusions; convexity defects measure each concavity's depth and span.
  • Solidity (contour area over hull area) compresses convexity into one sortable number.
  • Five fingertips plus four defects encode an open-hand gesture with no learning at all.
  • Hulls ignore holes and interiors, and one outlier pixel can invent a giant false defect.