Relationships between pixels and component labelling
How pixels are related by neighbourhoods and connectivity, and how connected components are found and labelled (two-pass, union-find, and RLE methods).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Component labelling converts a binary image into a set of labelled regions (connected components). Engineers use it to count objects, measure region properties (area, centroid, bounding box), and feed region labels into higher-level tasks such as segmentation, shape analysis and object recognition in machine vision systems.
Key ideas
Start with basic terms and then build to algorithms and proofs.
Pixel: the smallest addressable element of a digital image. We give it coordinates (x,y) where x is row and y is column. Pixel values are intensities or colours; in component labelling we normally work on a binary image where a pixel is either foreground (value 1) or background (value 0).
Neighbourhood (neighborhood): the set of pixels considered adjacent to a given pixel. Two common neighbourhoods in a 2D grid are 4-neighbourhood and 8-neighbourhood. Draw a small 3×3 grid and label the centre pixel as P. For 4-neighbourhood, draw arrows to the up, down, left, right pixels. For 8-neighbourhood include also the four diagonal pixels.
Adjacency: two foreground pixels p and q are adjacent under 4-connectivity if q is in the 4-neighbourhood of p; they are adjacent under 8-connectivity if q is in the 8-neighbourhood of p. A formal statement: under 4-connectivity, Adj4(p) = {(p.x±1,p.y),(p.x,p.y±1)}; under 8-connectivity, Adj8(p) = {(p.x+i,p.y+j) | i,j ∈ {-1,0,1}, (i,j) ≠ (0,0)}. At image borders, neighbours outside the image bounds are ignored.
Path and connectivity: a path between two foreground pixels p and q is a sequence of foreground pixels p = r0, r1, ..., rn = q such that each consecutive pair is adjacent (using the chosen adjacency rule). Two pixels are connected if there exists a path between them. Connectivity depends on the chosen adjacency (4 or 8).
Connected component: A maximal set of foreground pixels all pairwise connected. Formally: a set C of foreground pixels is a connected component if (1) every pair in C is connected, and (2) no superset of C of foreground pixels satisfies (1).
Component labelling (connected-component labelling, CCL): assigning an integer label to each foreground pixel so that two pixels share the same label iff they belong to the same connected component. Conventionally, background is label 0 and foreground component labels start from 1.
Equivalence relation between provisional labels: during the first pass of the two-pass algorithm (and in one-pass variants) we assign provisional labels and record equivalence pairs (a ~ b) meaning labels a and b belong to the same final component. The equivalence relation is reflexive, symmetric and transitive; component labelling resolves equivalence classes to a canonical label.
Algorithms and their assumptions:
Two-pass algorithm (common university algorithm) — assumes binary image, fixed adjacency (4 or 8), raster scan (rows top-to-bottom, columns left-to-right). Steps:
- First pass: for each foreground pixel p, examine already-scanned neighbours (commonly: left, top-left, top, top-right for 8-connectivity; left and top for 4-connectivity). If no neighbour labelled, assign new label. If one neighbour labelled, copy that label. If multiple different labels present, assign the minimum and record equivalences between those labels.
- Resolve equivalences (compute connected components in label graph, e.g., using union-find) to produce a mapping from provisional labels to final canonical labels.
- Second pass: relabel every foreground pixel with the canonical label.
Correctness sketch: any two connected foreground pixels have a path; the scanning plus equivalence recording ensures all provisional labels along the path are linked by equivalence pairs, so after equivalence resolution they map to the same canonical label. See worked example for an explicit trace.
Union-find (disjoint-set) data structure — used to record and resolve label equivalences efficiently. Each provisional label is an element; when you detect labels a and b are equivalent, perform UNION(a,b). After scanning, a FIND operation gives the canonical representative.
One-pass and run-length approaches — scan rows and store runs of consecutive foreground pixels (RLE). Merge runs that touch between adjacent rows. This reduces memory and can be faster on sparse images. The same equivalence-resolution idea applies to run labels.
Choice of connectivity matters (4 vs 8 or m-connectivity) — counts and shapes of components change. Always specify the connectivity in answers. To avoid topological paradoxes when reasoning about both objects and background, pair 4-connected foreground with 8-connected background (or vice versa).
Diagram to draw for an exam answer when explaining two-pass algorithm: draw a small 3×5 grid with some foreground pixels (1). Mark the scan order by numbering cells in scan order. For each foreground pixel show (a) which neighbours are inspected and (b) provisional label assigned. Then draw the equivalence graph (nodes are provisional labels, edges are equivalence pairs) and show the final representative for each node.
Formulas
Each formula on its own line with symbol definitions and applying conditions.
Adj4(p) = {(x-1,y),(x+1,y),(x,y-1),(x,y+1)} — Adj4 is the 4-neighbourhood of pixel p at (x,y); applies on a rectangular grid using 4-connectivity.
Adj8(p) = {(x+i,y+j) | i,j ∈ {-1,0,1}, (i,j) ≠ (0,0)} — Adj8 is the 8-neighbourhood; diagonal neighbours included.
Adjm(V,p) = N4(p) ∪ { q ∈ ND(p) | N4(p) ∩ N4(q) ∩ V = ∅ } — m-adjacency (mixed) of p within a foreground set V: N4 are 4-neighbours, ND are the four diagonal neighbours; include diagonal q only if p and q do not share a common 4-neighbour in V.
Max_components ≤ N_fg — maximum number of components is at most the number of foreground pixels N_fg (every pixel separate).
N_comp(8) ≤ N_comp(4) — for the same foreground set, 8-connectivity can only merge components found under 4-connectivity.
Time_two-pass = O(M·N · α(k)) ≈ O(M·N) — M×N image; k provisional labels; α is the inverse-Ackermann function from union-find operations; for practical image sizes α(k) ≤ 4, so complexity is effectively linear in the number of pixels.
Memory ≈ O(M·N) — storing labels per pixel requires one integer per pixel (can be reduced by RLE methods).
Worked examples
Example 1 — Connectivity counts (short numerical / diagram answer). Problem: For the binary 4×4 image with ones on the main diagonal: Row1: 1 0 0 0 Row2: 0 1 0 0 Row3: 0 0 1 0 Row4: 0 0 0 1 Question: How many connected components under 4-connectivity and under 8-connectivity? Model answer (draw the 4×4 with ones on diagonal):
- Under 4-connectivity, diagonal pixels are not adjacent. Each 1 is isolated, so number of components = 4. Final answer: 4.
- Under 8-connectivity, each diagonal neighbour is adjacent to the next via a diagonal step, so all four 1s form a single path 1→2→3→4 and there is one connected component. Final answer: 1.
Example 2 — Two-pass labelling with union-find (model answer, exam style). Use 3×5 image (rows listed) where scanning creates equivalences. Image (rows): R1: 1 0 1 0 1 R2: 0 1 0 1 0 R3: 0 0 1 0 0 Assume 8-connectivity and raster scan left-to-right, top-to-bottom. Steps (record provisional labels and equivalences):
- R1C1 = 1 → no prior neighbours → assign label 1.
- R1C3 = 1 → assign label 2.
- R1C5 = 1 → assign label 3.
- R2C2 = 1 → neighbours include R1C1 (label1) and R1C3 (label2). Assign min label 1 and record equivalence 1 ~ 2.
- R2C4 = 1 → neighbours include R1C3 (label2) and R1C5 (label3). Assign min label 2 and record equivalence 2 ~ 3.
- R3C3 = 1 → neighbours include R2C2 (label1) and R2C4 (label2). Assign min label 1 and record equivalence 1 ~ 2 (already known). Equivalence graph edges: 1–2 and 2–3 ⇒ equivalence class {1,2,3}. Using union-find we pick representative label 1 for all. Second pass: replace all provisional labels 1,2,3 by final label 1. Final result: all foreground pixels share label 1 → one connected component. Provide provisional label image (rows) as 1 0 2 0 3 / 0 1 0 2 0 / 0 0 1 0 0 after the first pass; after resolving equivalences, the second pass maps all foreground pixels to the canonical label 1.
Example 3 — Short proof sketch (theory question) — Why two-pass is correct. Model answer: Let p and q be any two foreground pixels in the same connected component. There is a path p = r0, r1, ..., rn = q. When scanning in raster order, for each adjacency ri→ri+1 the first time the later pixel is processed it either copies the earlier pixel's label (if earlier has been processed) or creates a new provisional label and an equivalence will be recorded later when a connecting pixel is processed. By transitivity of recorded equivalences (using union-find) all provisional labels along the path are merged into a single equivalence class, hence in the second pass all pixels on the path get the same final label. Thus two-pass yields one label per connected component.
Common mistakes
- Forgetting to record equivalences when multiple different provisional labels are seen.
- Thinking that equivalence pairs are only between adjacent provisional labels; equivalence must be transitively closed (use union-find or BFS on label graph).
- Confusing adjacency (local relation) with connectivity (global relation via paths).
- For RLE labelling, failing to merge runs that touch at a corner when using 8-connectivity.
For your exam
Types of questions you will see and what full-marks answers include:
- Definitions: define pixel, neighbourhood, adjacency, connectivity, connected component. Always state connectivity (4 or 8).
- Short notes: describe two-pass algorithm or union-find use; include neighbour set choices, equivalence recording and two passes.
- Derivations / proofs: prove correctness of two-pass (sketch using path and transitivity) or derive time complexity of union-find operations (mention inverse-Ackermann α function qualitatively).
- Numericals: given a small binary image, perform first pass labelling (show provisional labels and equivalence pairs), resolve equivalences, show second pass final labels; draw the image and label matrix; show final component count.
- Diagrams: draw the scan order on a small grid, show which neighbours are checked, and draw equivalence graph with edges between provisional labels.
Full marks answers always: state connectivity, show all intermediate provisional labels and equivalence pairs, show final label mapping and final labelled image or counts, and justify correctness briefly.
Quick check
- Define 4-connectivity and 8-connectivity in one sentence each.
- In two-pass CCL what do you do when a foreground pixel has two different labelled neighbours?
- True or false: using 8-connectivity can never increase the number of components compared to 4-connectivity.
Answers: 1) 4-connectivity: neighbours are up, down, left, right; 8-connectivity: neighbours include also the four diagonals. 2) Assign a provisional label (commonly the minimum) and record an equivalence between the different neighbour labels (use union-find). 3) True — 8-connectivity can only merge components that were separate under 4-connectivity, not split them.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?