Skip to content

Pixel Connectivity and Connected Components

A neighborhood tells us which pixels are nearby. Connectivity goes one step further: it tells us whether foreground pixels can form a path to one another and therefore belong to the same connected object.

1. From Neighborhood to Connectivity

A neighborhood defines which positions are nearby. Connectivity uses those local relationships to determine whether foreground pixels can reach one another.

From Local Neighborhoods to Connected Objects
Choose a Neighborhood
↓
Define Which Foreground Pixels Are Adjacent
↓
Form Foreground Paths
↓
Determine Which Pixels Are Connected
↓
Separate the Image into Connected Components
Adjacency is a local relationship. Connectivity can extend through many foreground pixels along a path.

2. Binary Foreground and Background

Connectivity is easiest to introduce using a binary image.

\[ 1=\text{foreground}, \qquad 0=\text{background}. \]
Example Binary Image
Dark cells are foreground pixels. White cells are background pixels. The connectivity rule determines which foreground pixels belong to the same connected object.

3. 4-Connectivity

Under 4-connectivity, a pixel can be adjacent to its immediate horizontal and vertical neighbors.

\[ N_4(p) = \{ (x-1,y), (x+1,y), (x,y-1), (x,y+1) \}. \]
4-Neighborhood Around \(p\)

If \(q\) is one of these four foreground neighbors, we can write

\[ p\sim_4 q. \]
Diagonal touching alone does not create a 4-connected relationship.

4. 8-Connectivity

Under 8-connectivity, the four diagonal neighbors are included as well.

\[ N_8(p) = N_4(p) \cup \{ (x-1,y-1), (x-1,y+1), (x+1,y-1), (x+1,y+1) \}. \]
8-Neighborhood Around \(p\)
\[ p\sim_8 q. \]
4-connectivity allows horizontal and vertical adjacency. 8-connectivity additionally allows diagonal adjacency.

5. Same Image, Different Connectivity

The foreground pixels can remain exactly the same while the number of connected components changes.

4-Connectivity

5 connected components

8-Connectivity

1 connected component
Every outer foreground pixel touches the center only diagonally. Therefore the pixels remain separate under 4-connectivity but become one component under 8-connectivity.

6. Connected Paths

Two pixels do not have to be immediate neighbors to be connected. They can be joined through a sequence of adjacent foreground pixels.

\[ p_0,p_1,p_2,\ldots,p_n \]

where consecutive pixels satisfy

\[ p_i\sim p_{i+1}, \qquad i=0,\ldots,n-1. \]
Example 4-Connected Foreground Path
Starting at \(p_0\), every step reaches another foreground pixel that is adjacent under the chosen rule.
\[ p\leftrightarrow q \]

means that a valid foreground path exists from \(p\) to \(q\).

7. Connected Components

A connected component is a maximal group of foreground pixels connected under the selected adjacency rule.

Three Connected Components
Every foreground pixel inside one component can be reached from every other foreground pixel in that component using valid paths.

8. Connected-Component Labeling

Connected-component labeling gives each connected foreground component a unique integer identifier.

\[ L(x,y) = \begin{cases} 0, & \text{background} \\[4pt] k, & \text{component }k \end{cases} \]

For \(K\) components,

\[ k\in\{1,2,\ldots,K\}. \]
Example Label Image
These numbers are component identifiers. They are not grayscale intensities.

9. Why Does Connectivity Matter?

#
Object Counting
Count separate foreground objects after segmentation.
D
Diagonal Touching
Decide whether corner-touching regions should remain separate or become one object.
S
Segmentation
Extract, measure, classify, or remove connected regions independently.
M
Morphology
Connectivity matters when studying holes, thinning, skeletons, and object topology.
Digital topology sometimes uses different connectivity choices for foreground and background to avoid ambiguous configurations. This page focuses first on foreground connectivity.

10. Boundary Pixels

A foreground pixel is a boundary pixel if its selected neighborhood contains at least one background position.

\[ \partial X = \{ p\in X \mid N(p)\cap X^c \neq \varnothing \}. \]
Boundary and Interior Pixels
Purple = boundary pixels
Green = interior pixels
The exact boundary can depend on the chosen neighborhood. A boundary based on \(N_4\) can differ from one based on \(N_8\).

11. Interactive Example

The visualizer below lets you edit a binary image and directly compare 4-connectivity with 8-connectivity.

Explore Connectivity

Edit the binary image, switch to Select Component, and click a foreground pixel.

Every foreground pixel connected to that seed is highlighted.

Change between 4- and 8-connectivity to see how the component changes.

Label Components

Run connected-component labeling on the same binary image.

Use Step to reveal one pixel at a time or Auto to watch the labels appear.

Each connected component receives its own integer identifier.

Try the default diagonal-touching example under both rules. The component count changes even though the image itself does not.

Summary

Concept Meaning
\(N_4(p)\) Immediate horizontal and vertical neighbors.
\(N_8(p)\) Immediate horizontal, vertical, and diagonal neighbors.
\(p\sim_4q\) \(p\) and \(q\) are 4-adjacent.
\(p\sim_8q\) \(p\) and \(q\) are 8-adjacent.
Path A sequence of foreground pixels whose consecutive members are adjacent.
Connected A valid foreground path exists between the pixels.
Connected Component A maximal connected group of foreground pixels.
Connected-Component Labeling Assign a unique integer identifier to each connected foreground component.
Boundary Pixel A foreground pixel whose selected neighborhood contains at least one background position.

Binary Image
Edit the binary image by clicking pixels.
Foreground Selected Component Seed Boundary
Connectivity
Choose which neighboring foreground pixels may connect.
Grid Interaction
Selected Seed
None
Component Size
—
Total Components
0
Labeling Controls
Current Component
—
Labeled Pixels
0
Total Components
—
Shape
Switch between 4- and 8-connectivity to compare the result.

Published inDigital Image Processing

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *