Connected-Components Labeling
Turn a binary mask into object IDs so you can count, measure, filter, and track separate regions instead of raw foreground pixels.
Segmentation & edges is nested under Foundations , so the broader pipeline usually still applies here.
Interactive playground
Tweak the operator or scene live so the article connects to an immediate visual result.
Connected components
Walk the mask and assign a distinct label to each separately reachable foreground island.
Binary mask
Region labels
96 × 96 live previewSwitch connectivity to decide whether diagonal touches merge into the same component or stay separate.
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
2 next topics
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
Partition first, then measure
Connected-component labeling is the natural next step once a binary mask exists and you need object-level reasoning.
Connected-Components Labeling
Choose this when: You need region IDs, counts, bounding boxes, or per-object measurements.
Choose something else when: Only one region from a single seed matters.
Flood Fill
Choose this when: A single seeded region should be grown interactively or locally.
Choose something else when: Every connected region across the whole image needs labeling.
Skeletonization
Choose this when: The goal is not just finding objects, but reducing them to centerlines after they are labeled or cleaned.
Choose something else when: Object identity is still the main missing step.
Problem
A binary mask says which pixels are foreground, but it does not yet say how many distinct objects exist or which foreground pixels belong together.
Intuition
Connected-components labeling groups foreground pixels by connectivity. Once every region gets an ID, you can count objects, compute areas, extract bounding boxes, or ignore tiny fragments.
Core idea
- Define connectivity, usually 4-neighbor or 8-neighbor.
- Traverse every unvisited foreground pixel with BFS, DFS, or a two-pass union-find style algorithm.
- Assign one label to all pixels reached in that traversal.
- Repeat until every foreground region has a label.
Worked example
If a thresholded image contains three disconnected bright blobs, component labeling converts one foreground mask into three object IDs, which is exactly what later measurement code needs.
Complexity
A straightforward traversal-based implementation is O(WH) because every pixel is visited a constant number of times.
When to choose it
- Choose it when object counting, measurement, or per-region filtering matters.
- Choose flood fill when you only care about one seeded region rather than every object in the image.
- Choose distance transform when geometry relative to boundaries matters more than region IDs.
Key takeaways
- Connected-components labeling upgrades a mask into object-level structure.
- The key design choice is the connectivity rule: 4-neighbor and 8-neighbor can disagree.
- It is linear-time and often one of the first post-threshold analysis steps.
- Many later shape tools assume labeled regions rather than unlabeled masks.
Practice ideas
- Threshold an image, then remove every connected component smaller than a chosen area.
- Compare 4-connectivity and 8-connectivity on a diagonally touching pattern.
- Compute a bounding box and area for each labeled component.
Relation to other topics
- Flood fill grows one chosen region; component labeling partitions all regions.
- Opening and closing often clean the mask before labeling so tiny junk components disappear.
- Distance transform and skeletonization often operate on regions that have already been isolated or filtered.
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.
Distance Transform
For every foreground pixel, compute how far it sits from the background or boundary so the mask gains geometric thickness information.
Skeletonization
Reduce a thick binary shape to a thin centerline while trying to preserve its topology.
Related directions
These topics live nearby conceptually, even if they are not strict prerequisites.
Canny Edge Detection
Build a cleaner final edge map by chaining smoothing, gradients, non-maximum suppression, and hysteresis thresholding.
Distance Transform
For every foreground pixel, compute how far it sits from the background or boundary so the mask gains geometric thickness information.
Opening and Closing
Compose erosion and dilation to remove bright specks or fill small holes without manually editing the mask.
Skeletonization
Reduce a thick binary shape to a thin centerline while trying to preserve its topology.
More from Segmentation & edges
Stay in the same family when you want parallel operators built from the same mental model.
Flood Fill
Grow one connected region from a seed pixel, making it the canonical seeded region-growing primitive.
Thresholding
Convert a grayscale image into a binary mask by splitting values into foreground and background.
Laplacian and Laplacian of Gaussian
Use second-derivative style filters to emphasize rapid intensity change and zero crossings, often after smoothing first.
Sobel Edge Detection
Estimate local intensity gradients so boundaries become visible as bright responses.
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.