Contour Properties
Turn an outline into numbers: area from one moment, center from two ratios, and pointing direction from the spread of pixels around that center.
Why Does This Exist?
Detection hands you loops, but a robot gripper needs the center in millimeters and a sorter needs the area in square pixels. Contour properties are the integrals that convert outlines into those measurements: area, centroid, orientation, and the shape ratios that tell rods from disks. They cost one pass over the loop and need no training, which keeps them inside every real-time inspection pipeline.
This page covers raw and central moments, the centroid derivation, and the ratio descriptors. Hull-based concavity analysis continues in convex hulls, and fitted geometry in bounding shapes.
Think of It Like This
Balancing a cardboard cutout
Cut a wrench shape from cardboard and balance it on one fingertip: the balance point is the centroid, the only point where weight spreads evenly in all directions. Weigh the cutout for area, then compare its weight against the shoebox it ships in: that ratio is extent, telling flat plates from spindly frames without a single measurement of length.
It stops holding for bent abstractions. Balance and weight cannot tell a boomerang from a banana of equal mass, the same way area and centroid miss concavities. Notches and holes need hull defects and hierarchy, not moments.
How It Actually Works
Moments, centroid, and orientation
The raw moment of order sums over the shape: . Area is the zeroth moment , pure pixel count. The centroid divides first orders by area: , . Central moments subtract the centroid first, making them translation-proof, and the orientation angle comes from the second orders: .
Verify on a right triangle with vertices , , . Area is , so . The centroid of a triangle averages its vertices: , . Hence and , and both ratios return the centroid exactly.
Perimeter and the ratio descriptors
Perimeter integrates step lengths along the ordered loop, which is why ordering matters. Three ratios then compress shape: aspect ratio (width over height of the upright box), extent (contour area over box area, near for plates), and solidity (contour area over hull area, near for blobs, low for starfish). Filters like "extent above and solidity above " isolate rectangular chips on a belt with two comparisons.
Code
Shoelace area plus vertex-averaged centroid on the fixture triangle:
verts = [(0, 0), (4, 0), (0, 3)]
area2 = sum( verts[i][0] * verts[(i + 1) % 3][1] - verts[(i + 1) % 3][0] * verts[i][1] for i in range(3))area = abs(area2) / 2cx = sum(v[0] for v in verts) / 3cy = sum(v[1] for v in verts) / 3print(f"area {area}, centroid ({cx:.3f}, {cy:.1f})")# -> area 6.0, centroid (1.333, 1.0)Area and centroid , confirming and .
Watch Out For
Matching with raw instead of central moments
Symptom: the same part scores as different shapes at different belt positions because and grow with absolute pixel coordinates. Raw moments bake position into every value. Translate to central moments , or normalize further to Hu moments, before any comparison across locations.
Trusting pixelated perimeters absolutely
Symptom: a diagonal edge measures about long because staircase steps count every jog, so circularity scores drift with rotation. Perimeter is resolution- and rotation-sensitive while area is comparatively stable. Compare ratios and ranks rather than absolute perimeters, or smooth and sub-sample the contour first.
The Quick Version
- is area; centroid ratios and locate the center of mass.
- Central moments remove translation; second orders yield orientation via the arctangent formula.
- Extent, solidity, and aspect ratio compress shape into sortable numbers for inspection filters.
- Perimeter needs the ordered loop and overestimates diagonal edges on pixel grids.
- Moments miss concavities, so pair them with hull analysis for notched or holed parts.