Connected components
Give every connected foreground region a distinct integer label.
All foreground pixels initially have the same value.
Start a flood from the first unvisited foreground pixel.
After the speck, the larger L-shaped component receives a different label. Connectivity is eight-neighbour here.
Colours represent integer labels. Numbered centroids come from each component’s actual pixel coordinates.
Accumulate per-label statistics and centroids.
Computed teaching example on a 24 × 16 image. Small kernels and simplified settings keep each change visible; use the image laboratory for native OpenCV.
Try it on an image
Experiment at pixel level
Label connected white regions. Colours encode label IDs; inspect the numeric ID underneath.
The engine loads on your first run. Your images stay in this browser.
Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.
Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Select a pixel
Select a pixel
When to use it
Count objects, filter tiny regions, or measure areas, bounding boxes and centroids.
How it works
- 01Visit each foreground pixel and its selected neighbours.
- 02Merge labels that refer to the same connected region.
- 03Accumulate per-label statistics and centroids.
component = maximal connected set of nonzero pixels
What to tune
Connectivity 4 uses shared edges; 8 also joins diagonals. Label zero is the background.
Where it breaks down
Touching objects become one component. Label numbers are identifiers; they do not necessarily follow size or position.
TypeScript API
Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.
All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.
Related methods
Global thresholding
Divide pixels using one intensity threshold, or choose it automatically with Otsu or the triangle method.
Adaptive thresholding
Compute a different intensity threshold for each local neighbourhood.
Contour tracing
Trace the boundary of each binary region as an ordered sequence of points.
Marker-based watershed
Grow labelled regions across an image until competing regions meet at boundaries.