Skip to content
AI360Xpert
Beta

Contours and Shape Analysis

Once objects become outlines, every question about shape, size, corners, and identity reduces to math on ordered boundary points. This page maps the family.

Ordered boundary loops turn masks into measurable geometry: area, centroid, hull, and fitted boxes.
Ordered boundary loops turn masks into measurable geometry: area, centroid, hull, and fitted boxes.

Why Does This Exist?

Segmentation answers which pixels belong together, but robots and inspectors ask geometric questions: how big, centered where, which way is it pointing, is this the same part as before? Contours are the bridge from pixel blobs to those answers, compressing thousands of mask pixels into an ordered loop of boundary points that geometry can grip. Everything downstream, moments, hulls, boxes, Hough shapes, and matching, reads that loop.

Edge pixels are the raw material, so basic gradient filtering from spatial filtering is the prerequisite. This page is the family map: detection finds the loops, properties measures them, convex hulls and bounding shapes simplify them, Hough transforms find parametric shapes, and shape matching identifies them.

Think of It Like This

Cookie cutters in a bakery

A bakery identifies every cookie by its cutter: star, heart, round. The cutter keeps the outline and throws away the frosting, sprinkles, and filling, because shape alone settles identity, size, and orientation. Contours are the cutter; the dough inside is the pixel mask.

It stops holding where outlines go blind. Two cookies from different dough taste different, the same way a contour cannot tell a red apple from a green one of identical shape. Texture, color, and interior stay behind with the mask.

How It Actually Works

From edges to ordered loops

Binary mask in, ordered point list out: edge operators find discontinuities, then a border-following pass links edge pixels into loops in visit order. Order matters because perimeter, orientation, and polygon simplification all walk the loop sequentially. A contour is therefore stored as an N×2N \times 2 array of (x,y)(x, y) points, not a set, and direction (clockwise or not) can even encode whether the loop is an outer boundary or a hole.

The five readings of one loop

Each child page performs one reading. Detection extracts loops and their nesting hierarchy. Properties integrate over the loop for area, centroid, and orientation via moments. The convex hull shrink-wraps the loop to expose concavities as defects. Bounding shapes fit the cheapest box, rotated box, or circle for detectors and grasping. Hough methods vote for lines and circles when edges are broken, and shape matching compares moment signatures across poses.

Approximation before measurement

Raw loops carry hundreds of redundant collinear points, so polygon approximation (Douglas-Peucker, OpenCV approxPolyDP) drops points within ϵ\epsilon of the simplified edge. An ϵ\epsilon around 11-2%2\% of perimeter keeps corners while cutting storage tenfold. Measure after approximating: noise points inflate perimeters and invent false corners, and every downstream reading inherits the error.

Code

The standard OpenCV contour pipeline. Values below are the API contract, not measured output:

import cv2
gray = cv2.imread("parts.jpg", cv2.IMREAD_GRAYSCALE)_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)big = max(contours, key=cv2.contourArea)approx = cv2.approxPolyDP(big, 0.02 * cv2.arcLength(big, True), True)area = cv2.contourArea(approx)# area now measures the largest part; hierarchy records its holes

Threshold, extract with full hierarchy, simplify to 2%2\% of perimeter, then measure the largest part.

Watch Out For

Measuring contours traced on raw noisy edges

Symptom: perimeters come back far too large, corner counts explode, and matching scores scatter across identical parts. Every noise spur becomes boundary. Fix the image before tracing: blur, threshold cleanly, and run a morphological open to drop specks, then approximate before any measurement.

Reading the wrong hierarchy level

Symptom: a washer's hole gets measured as a separate part, doubling counts and halving mean area. Retrieval modes decide which loops you see, and the default is rarely what counting needs. Choose the mode deliberately (external for counting, tree for parts with holes) and filter by hierarchy depth or area before aggregating.

The Quick Version

  • A contour is an ordered loop of boundary points, and the ordering is what makes geometry possible.
  • Detection, properties, hulls, bounding shapes, Hough voting, and matching form one pipeline on that loop.
  • Polygon approximation with ϵ\epsilon near 2%2\% of perimeter removes noise points before measurement.
  • Hierarchy modes decide whether holes and nested shapes appear, so counting needs the right mode.
  • Contours capture shape only; color and texture questions need other features alongside.