Skip to content

Connected-component labeling

Fact Value
Also called Component labeling, Blob labeling, Connected components
Status Implemented and tested
Family Segmentation and regions
Engine Browser
Type Nonlinear, spatial, connected-component labeling
Output Image
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/segmentation/regions.ts, cclLabelFilter
Operator code web/src/core/filters/segmentation/regions.ts, componentLabels

Overview

Connected-component labeling turns a black and white mask into separate objects. Every group of white pixels that hang together gets its own number, so the objects can be counted and measured one by one. The result shows each component in its own color.

Two panels: white disks of several sizes and four single dots on black, and the same shapes, each in a different color. Two panels: white disks of several sizes and four single dots on black, and the same shapes, each in a different color.
The test disks and their components, each in its own color.

How it works

  1. Mark the bright pixels as foreground.
  2. Scan the image row by row. At each foreground pixel that has no label yet, start a new label.
  3. Give that label to every foreground pixel connected to it.

Mathematical foundation

\[ L(p) = \begin{cases} k & p \text{ in the } k\text{th component of } F \\ 0 & p \notin F \end{cases}, \qquad F = \{\, p : Y(p) \ge 128 \,\} \]

The formula shows the default parameter values.

where

  • \(F\): The foreground pixels
  • \(L(p)\): The label of pixel p
  • \(Y(p)\): The brightness of pixel p

Two foreground pixels belong to the same component when a path of foreground pixels joins them, each step to one of 4 or 8 neighbors. The components are numbered by their first pixel in a row by row scan, as scipy.ndimage.label and scikit-image number them. Rosenfeld and Pfaltz describe labeling in two passes over the image. PixelNexus follows each component from its first pixel instead, which gives the same labels. In the color view, label k gets the hue k times 137.5 degrees, the golden angle, so neighboring numbers get very different colors. The gray view shows label k as 255 k / n for n components.

Connectivity decides what touching means. With 4 neighbors, a pixel touches the pixels left, right, above and below it. With 8 neighbors, the four corners count too. A diagonal line one pixel wide is one component with 8 neighbors, and a row of single pixels with 4.

Kernel or operator

The Inspector shows the foreground rule and the labeling with the current settings.

Algorithm

  1. Mark the foreground, brightness at or above 128.
  2. Scan the image row by row. At each foreground pixel without a label, start a new label and give it to every foreground pixel connected to it.
  3. Color each label, and paint the background black.

Parameters

Parameter Default Values Meaning
Neighbors connectivity 8 (sides and corners) 8 (sides and corners), 4 (sides only) Which neighbors join two foreground pixels into one component.
Invert invert off On or off Treat the dark pixels, below 128, as foreground instead.
Show output Colors Colors, Gray levels Show the components in distinct colors, or as gray levels that rise with the label number.

Implementation

cclLabelFilter in web/src/core/filters/segmentation/regions.ts uses labelComponents, the code that connected-component filtering also uses.

Worked example

Neighbors Components Largest area Smallest area Single pixels
8 10 1576 1 4
4 10 1576 1 4

The test disks of the figure, 160 by 160 pixels: bright disks of several sizes and single bright dots on a dark background. Areas are in pixels. The disks are solid, so their count does not depend on the neighbors. A difference between the rows would come from shapes that touch only at a corner.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. Each pixel is visited a few times.

Artifacts and limitations

Limitations

  • Touching objects become one component. Separate them first, for example with erosion or a watershed.
  • Noise gives many tiny components. Remove them with connected-component filtering.

Artifacts

  • Thin gaps split one object into several components.

Tests

The fixture cases compare every pixel with scipy.ndimage.label on the color test image, with 4 and 8 neighbors, with Invert, and in both views. Unit tests check that corners join pixels only with 8 neighbors, the numbering order, and the colors.

References