Connected Components: Four or Eight Neighbors in a Mask
2026-11-07

Connected Components: Four or Eight Neighbors in a Mask

Connected Components labeling groups the foreground pixels of a binary image into regions. The same mask can produce different groups because the result depends on how pixels are allowed to connect. In OpenCV, four-neighbor connectivity joins pixels across edges, while eight-neighbor connectivity also joins pixels across corners. A simple diagonal example makes that choice visible before reading a component count or a bounding box.

The mask decides which pixels are in the foreground

OpenCV's 4.12 API describes the input as an eight-bit, single-channel image. For a binary mask, zero values are the background and nonzero values are foreground. The labeling step accepts that decision as its input. If a threshold or another image operation produced the mask, that earlier operation determined which pixels are available to be grouped.

Consider an inspection image after a separate segmentation step. A gap of background pixels can divide the foreground into several regions. A corner contact can also occur between two regions. A connected-components result tells how those pixels group under the selected neighborhood. It describes their arrangement in the current mask, which may or may not match the number of physical objects visible in the scene.

Four and eight neighbors define different allowable steps

For a pixel away from the image border, four-neighbor connectivity allows a step left, right, up, or down. Eight-neighbor connectivity adds the four diagonals. A region contains foreground pixels that can be joined through a chain of allowed steps. The two endpoints of a chain can be far apart. The labeling depends on whether a path exists through foreground pixels, rather than on their direct distance alone.

In a four-by-four mask, put foreground at coordinates (1, 1) and (2, 2), using zero-based x and y. The two pixels share only a corner. With four neighbors they form two components, since there is no foreground route between them using edge steps. With eight neighbors one diagonal step joins them, so they form one component. The input mask is identical in the two cases.

Two 4 by 4 label grids show the same two diagonal foreground pixels labeled 1 and 2 under four-neighbor, and both 1 under eight-neighbor.
The pixel values in the input mask stay unchanged while the grouping rule changes. Source: Zhilianhai editorial desk, original AI-assisted concept; no real product, event or measured data.

The returned label count includes the background label

The 4.12 header reserves zero for background and describes the returned number as the total count of labels. In this example, four-connectivity returns three labels: background and two foreground groups. Eight-connectivity returns two labels: background and one foreground group. A report that needs the number of foreground components can therefore separate label zero from the count.

The label image associates each pixel with a group identifier. Two equal nonzero labels inside that result indicate membership in the same component. The value of the identifier has no physical meaning on its own. OpenCV documents algorithm differences in label ordering, so numeric labels should be treated as names within one result rather than identities across frames or processing variants.

All background pixels receive the reserved label even when the zero-valued cells occupy several disconnected patches. A label image is therefore asymmetric in this respect. Nonzero labels identify connected foreground regions under the selected rule, while the single zero value simply identifies the background set. Treating every returned label as a distinct foreground object would add a spurious entry.

Component statistics describe the assigned pixels

The connectedComponentsWithStats function returns a bounding box, pixel area and centroid for each label. The bounding box includes every column and row between the extreme member pixels. It can cover holes or corners that do not belong to the region, which matters when extracting a rectangular crop. In the eight-neighbor example, the joined foreground has area two pixels. Its bounding box is two pixels wide and two pixels tall, because it must cover both occupied coordinates. The other two cells inside the box are background. Box size therefore gives an enclosing rectangle, and area gives the count of actual member pixels.

The centroid of those two pixels is at (1.5, 1.5), midway between the two occupied coordinates. It can be fractional because it summarizes positions. The API also returns a statistics row and centroid for the background label. A downstream report about foreground features can select the rows with nonzero labels while keeping their interpretation tied to the mask and connectivity.

The two diagonal member pixels lie in a two by two bounding box; a centroid cross is at coordinate 1.5, 1.5.
Bounding box, pixel area and centroid describe different properties of one component. Source: Zhilianhai editorial desk, original AI-assisted concept; no real product, event or measured data.

Save the grouping rule with the reported measurements

A technical record can preserve the input mask or its generating rule, the connectivity choice, the label data type, and whether reported counts include background. For example, a change from two isolated regions to one larger bounding box may come from switching the neighborhood while leaving the pixels unchanged. Comparing masks and connectivity together helps locate the cause.

The OpenCV 4.12 API header documents labels and statistics. Its tagged connected-components tests exercise a diagonal chessboard case with both choices. See also the robot vision reference and technical article directory.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

咨询 在线留言 电话:18028484626
微信 微信扫码添加我