Skip to main content
Back to the graphics atlas
FoundationsSegmentation & edges Algorithm Advanced

Distance Transform

For every foreground pixel, compute how far it sits from the background or boundary so the mask gains geometric thickness information.

Segmentation & edges is nested under Foundations , so the broader pipeline usually still applies here.

graphicsimage-processingdistancemask

Interactive playground

Tweak the operator or scene live so the article connects to an immediate visual result.

Distance transform

Start from every source pixel at once and spread outward so each cell knows how far it is from the nearest target set.

Binary mask

Distance heatmap

96 × 96 live preview

Toggle whether distance should grow away from foreground or away from the background holes.

Family

Foundations -> Segmentation & edges

Turn raw pixels into masks, boundaries, and simpler structural signals.

Builds on

2 topics

Read these first if you want the surrounding pipeline context.

Unlocks

1 next topic

Use these follow-ups when you want to keep turning the image-processing pipeline forward.

Learning paths

1

This topic appears in curated graphics progressions so the next step is obvious.

Choose this over that

Distance, region ID, or skeleton?

These mask-analysis operators all use binary regions differently: one measures distance, one partitions, and one collapses shape to a centerline.

Current topic

Distance Transform

Choose this when: You need per-pixel distance to background or boundaries.

Choose something else when: The main goal is object IDs or contour thinning.

Segmentation & edges Open topic

Connected-Components Labeling

Choose this when: Per-object labeling matters more than per-pixel geometry.

Choose something else when: Distance to the nearest boundary is the actual feature you need.

Morphology Open topic

Skeletonization

Choose this when: A thin centerline representation is the end product.

Choose something else when: Distance values still matter directly.

Problem

A binary mask tells you inside versus outside, but not how deep a pixel lies inside the shape. Many geometric tasks need that notion of thickness or clearance.

Intuition

The distance transform replaces each foreground pixel with the distance to the nearest background pixel. Pixels near the boundary get small values, while deep interior pixels get larger ones.

Core idea

  • Start from the boundary or all background pixels as distance zero.
  • Propagate nearest-boundary information inward, using a metric such as Manhattan, chessboard, or Euclidean distance.
  • Store the smallest distance found for each foreground pixel.

Worked example

On a thick bar shape, the centerline pixels receive the largest distances because they are farthest from either edge. That is why distance transforms often reveal the medial structure of a shape.

Complexity

The naive all-pairs view is expensive, but practical transforms can be computed in linear or near-linear time depending on the metric and algorithm.

When to choose it

  • Choose it when interior thickness, clearance, or nearest-boundary information matters.
  • Choose component labeling when object IDs are the missing structure rather than geometric depth.
  • Choose skeletonization when the final goal is a thin centerline rather than a full distance field.

Key takeaways

  • Distance transforms turn a binary mask into a geometric field.
  • They are a natural bridge from segmentation into measurement and medial-axis reasoning.
  • The chosen distance metric changes the shape of the result.
  • They are frequently used before skeleton extraction or path planning on masks.

Practice ideas

  • Compute a distance transform on a simple shape and identify the pixels with maximal distance.
  • Compare Manhattan and Euclidean distance fields on the same mask.
  • Use the distance field to estimate local thickness across a segmented object.

Relation to other topics

  • Connected-components labeling identifies objects; distance transforms describe their geometry internally.
  • Erosion can be understood as repeatedly peeling away boundary layers, which is closely related to distance depth.
  • Skeletonization often keeps points that are structurally central with respect to the distance field.

Build on these first

These topics supply the mental model or preceding stage that this page assumes.

What this enables

Once the current operator feels natural, these are the most useful follow-up jumps.

Related directions

These topics live nearby conceptually, even if they are not strict prerequisites.

More from Segmentation & edges

Stay in the same family when you want parallel operators built from the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

Mask analysis and cleanup

Build a binary mask, explore connectivity, then repair or simplify it with morphology-aware tools.

From the blog

Pair the graphics atlas with recent writing from the broader site whenever you want a wider engineering lens.