AI Glossary / Computer vision and multimodal AI / Beginner

Connected-component labeling

Definition

Connected-component labeling assigns a distinct label to each connected group of foreground pixels in an image. Pixels belong to the same component when a path of neighboring foreground pixels links them under a specified connectivity rule, allowing separate regions to be counted and measured.

What it means

Imagine a binary image in which foreground pixels are white and background pixels are black. Labeling identifies each separate white region and gives its pixels the same integer identifier.

The identifiers distinguish regions; they are not semantic categories such as “coin,” “letter,” or “defect.” A component may correspond to one object, part of an object, or several touching objects, depending on how the image was segmented.

Connectivity must be explicit. In two dimensions, 4-connectivity uses horizontal and vertical neighbors. 8-connectivity also includes diagonal neighbors. A change in this rule can change the number of components without changing any pixels.

How it works

First produce a foreground mask appropriate to the task, perhaps by thresholding or another segmentation method. Then select the neighborhood rule and run a labeling algorithm.

One common strategy scans the image, assigns provisional labels, records when neighboring labels belong together, and resolves those equivalences. Other implementations use different efficient traversal or block-processing methods.

The output is a label image. Region statistics can include pixel area, bounding box, and centroid. These can support filtering or further analysis.

Evaluate the mask as well as the labeling. A small bridge of foreground pixels can merge two objects, while a gap can split one object. The algorithm follows the given connectivity; it does not know which real-world objects the user intended to separate.

Three practical examples

1. Document processing: A system groups foreground pixels into candidate character or mark regions before later recognition stages.

2. Product inspection: A controlled image mask is labeled to measure candidate spots or surface anomalies for review.

3. Object counting: A prototype counts separated items on a contrasting background, then flags touching regions that may contain multiple items.

Think of it this way

Think of islands on a map, where every piece of connected land receives one name. A narrow land bridge makes two areas part of one island under that definition.

The analogy is limited because pixels lie on a discrete grid and diagonal contact is a design choice. A physical object can also be split by shadows or occlusion, so a labeled pixel island does not necessarily equal one real object.

Worked example

Consider a three-by-three binary image with foreground pixels only at coordinates (1, 1), (2, 2), and (3, 3). The other six pixels are background.

Under 4-connectivity, none of the foreground pixels share a horizontal or vertical edge. There are three foreground components, each with area one pixel.

Under 8-connectivity, the first pixel touches the second diagonally and the second touches the third diagonally. There is one component with area three pixels and centroid (2, 2), using these coordinate conventions.

Some software returns a total label count that includes background. In that convention, the totals would be four labels and two labels respectively, even though the foreground-component counts are three and one. Always check the library's output definition before using a count in a report.

Illustrative case study

Fictional case: Willow Packaging counts small seals on a tray using a thresholded image. Most seals form separate components, but two touching seals become one connected region.

The team reviews component areas and flags unusually large regions rather than assuming every label represents one seal. Shadows create another problem by splitting some seals into multiple regions.

Engineers improve lighting and segmentation, then compare counts with manually checked trays. The fictional case demonstrates that correct labeling cannot compensate for every input-mask problem. No counting accuracy or production improvement is claimed without measurement.

Common misconceptions and mistakes

A component label is not an object class. Label 7 does not inherently mean “defect” or “person.”

Component counts depend on connectivity and preprocessing. Results from different settings are not automatically comparable.

The background label may be included in a function's returned count, so subtracting or retaining it must follow the documented convention.

Do not assume label numbers remain stable across images or implementations. If persistent object identities are needed across frames, a tracking or matching step is required.

Business use

Connected-component labeling is a useful, inexpensive building block for counting and measuring segmented regions. It often provides a clear baseline for constrained inspection tasks.

Define foreground selection, connectivity, minimum region size, and treatment of touching or border-clipped regions. Convert pixel measurements to physical units only after appropriate calibration and geometric consideration. Validate with difficult examples, including overlap, shadows, tiny noise regions, and incomplete objects. Keep the distinction between image regions and actual items visible in operational reporting.

People also ask

Does the algorithm require AI training?

No. Standard connected-component labeling is a deterministic image-processing operation for a given mask and settings.

Can it separate touching objects?

Not by connectivity alone when they form one foreground region. Additional segmentation or shape analysis may be needed.

Can it work in three dimensions?

Related labeling methods operate on voxel volumes using an appropriate three-dimensional neighborhood definition.

Check your understanding

Two foreground pixels touch only at a corner. Must they receive the same component label?

Answer: It depends on the connectivity rule. They connect under 8-connectivity but not under 4-connectivity if no other foreground path joins them.

Related terms

  • Binary image
  • Segmentation
  • Centroid
  • foreground mask
  • 4-connectivity
  • 8-connectivity
  • region area
  • contour extraction

Learn next

Learn thresholding and then label a small hand-drawn pixel grid under both connectivity rules. Add a one-pixel bridge and observe how the count changes. Finally compare component regions with contours and with real-world object identities to understand their different purposes.

More in Computer vision and multimodal AI

  • Computer vision - Computer vision is the field of developing methods that extract useful information from images, video and other visual measurements.
  • Computer vision dazzle - Computer vision dazzle is a visual camouflage approach that explores whether changes in appearance can disrupt particular computer vision systems while...
  • Condensation algorithm - The Condensation algorithm is a probabilistic visual-tracking method that represents uncertainty with a collection of weighted samples.
  • Conservative vector field - A conservative vector field is a vector field that can be expressed as the gradient of a scalar potential on its domain.
  • Constrained least squares - Constrained least squares finds model parameters that minimize a sum of squared residuals while obeying specified restrictions.
  • Constraint satisfaction - Constraint satisfaction is the task of assigning values to variables so that all specified restrictions are met.

Sources

OpenCV official documentation, “Structural Analysis and Shape Descriptors,” consulted for connected-component outputs, connectivity options, and region statistics: https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html

The three-pixel calculation and seal-counting case are original teaching examples. Actual function signatures and supported output types should be checked against the software version used in an implementation.

Previous: Conjunctive normal form Next: Connection string

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer