Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 46 ยท Segmentation and Shapes

Connected Components and Labeling

Group binary pixels into objects with scipy.ndimage.label. Compare 4 and 8 connectivity, count objects, measure sizes and bounding boxes, and filter noise.

After thresholding you have a binary mask: ones where "something" is, zeros elsewhere. But a computer vision program rarely wants a mask. It wants to know how many objects there are, where each one is, and how big. The step that turns a mask into a list of separate objects is connected-component labeling: every group of touching foreground pixels gets its own integer label (1, 2, 3, ...), and the background stays 0.

This lesson explains what "touching" means, uses scipy.ndimage.label to label a mask, and then gets sizes, bounding boxes and centres, which is everything you need for counting coins, cells or cars in the project lessons later on. It builds on boolean masks and morphology.

What does "connected" mean?

Two foreground pixels belong to the same component if you can walk from one to the other through foreground pixels, one step at a time. The only choice is which steps are allowed:

  • 4-connectivity: you may move up, down, left or right. Diagonal neighbours do not count.
  • 8-connectivity: you may also move diagonally, so every pixel has 8 neighbours.
flowchart LR
    P["pixel"] --> N4["4-connectivity:<br/>up, down, left, right"]
    P --> N8["8-connectivity:<br/>those four plus the 4 diagonals"]

Both rules include the four straight neighbours; 8-connectivity adds the four diagonals.

The choice changes the answer for pixels that only touch at a corner. Two blobs joined by a single diagonal contact are one object under 8-connectivity and two under 4-connectivity. Neither is "right": use 8 for shapes that should hold together even when thin or diagonal (handwriting strokes, thin lines), and 4 when touching diagonally should not merge objects. The default in scipy.ndimage.label is 4-connectivity.

Python 4-connectivity versus 8-connectivity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the two printouts. The pixel at row 2, column 2 touches the pair at row 1 only diagonally: with 4-connectivity it is its own component (label 3); with 8-connectivity it joins the pair, so the count drops from 5 to 4. The lone pixel at the bottom-left corner stays alone in both, because nothing is near it.

The structure argument is a small 3 by 3 array that says which neighbours count. np.ones((3, 3)) allows all eight; the default cross shape allows four. You can even pass a custom shape, such as one that only allows horizontal links.

Labeling a real-looking mask

Now a more realistic case: five discs of different sizes, buried in Gaussian noise, like a photograph of coins. We smooth the image, threshold it and label the result. The function returns (labels, count), where labels has the same shape as the mask.

To get sizes, count how often each label appears. np.bincount(labels.ravel()) does that in one call: index 0 is the background, so slice it off with [1:]. To get bounding boxes, ndi.find_objects(labels) returns, for every label, a tuple of two slices (rows, columns), which you can use directly to crop. ndi.center_of_mass(mask, labels, index) gives the centroid.

Python Counting objects and measuring them

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first print line is a warning in numbers: thresholding the noisy image directly produces dozens of tiny "objects" (87 in this run, because every noise speck above 0.5 is its own component). After smoothing, the count is a clean five. Labeling is only as good as the mask you give it.

Labels are numbered in the order the scan meets each region, top to bottom and left to right, so they say nothing about size. Always compute a size table if you need to rank or filter objects.

Filtering by size

Real masks still contain debris. A very simple and effective rule: keep only components that are large enough (and sometimes small enough). Having sizes, you build a boolean lookup and apply it to the labels:

Python
keep_labels = np.nonzero(sizes >= 40)[0] + 1     # +1 because sizes[0] belongs to label 1
cleaned = np.isin(labels, keep_labels)

np.isin marks pixels whose label is in the keep list. You can then relabel cleaned to get consecutive numbers again. A neat alternative uses a lookup array: lookup = np.zeros(count + 1, bool); lookup[keep_labels] = True; cleaned = lookup[labels], which is fast even for millions of pixels because it is just fancy indexing.

Python Removing small regions and cropping the biggest

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fill ratio is a first shape clue: a disc fills roughly 0.8 of its bounding box (pi/4 in the smooth limit), a rectangle fills 1.0, and a thin diagonal line fills very little. The next lesson turns clues like this into proper shape measurements.

Useful per-region statistics

scipy.ndimage can compute statistics for every label in one pass, which avoids Python loops over objects:

  • ndi.sum_labels(mask, labels, index) or ndi.sum(image, labels, index) adds up values per region. With an intensity image it gives the total brightness; with the mask it gives the area.
  • ndi.mean(image, labels, index) gives mean intensity per region, and ndi.maximum, ndi.minimum and ndi.standard_deviation work the same way.
  • ndi.center_of_mass(mask, labels, index) gives centroids; index can be a list such as range(1, count + 1).
  • ndi.labeled_comprehension runs a custom function on each region's pixels.

Labeling is also a way to remove "holes". Label the background (~mask) and any background component that does not touch the border is a hole inside an object. Filling it means adding it to the mask. ndi.binary_fill_holes(mask) does exactly that.

Common mistakes

  • Labeling a non-binary image. ndi.label treats every non-zero pixel as foreground. A grayscale image with noise everywhere is all foreground and returns one huge region. Threshold first.
  • Forgetting the background label. Label 0 is the background. When you compute np.bincount or loop over regions, start from 1.
  • Merged neighbours. Touching objects get one label. Use morphology to separate them, or the distance-transform and watershed approach in a later lesson.
  • Choosing connectivity casually. If your object counts jump when you switch between 4 and 8, the mask has diagonal contacts you should think about.
  • Assuming labels are sorted by size. They are not.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Two blobs touch only at a diagonal corner. How many components do 4-connectivity and 8-connectivity find?

    4-connectivity does not allow diagonal steps, so the blobs stay separate. 8-connectivity does, so they join.
  2. 2. What does ndi.label return?

    The result is (labels, count); labels holds 0 for background and 1..count for the regions.
  3. 3. How do you get the pixel count of every labeled region in one NumPy call?

    bincount counts how often each label value appears; index 0 is the background, so slice it off.
  4. 4. Why does thresholding a noisy image directly often produce dozens of components?

    Noise pixels scattered above the threshold are small separate groups. Smoothing, morphology or a size filter removes them.

Summary

  • Connected-component labeling turns a binary mask into numbered objects; 0 is the background.
  • 4-connectivity allows only straight steps, 8-connectivity adds diagonals; ndi.label uses 4 by default and structure=np.ones((3, 3)) selects 8.
  • np.bincount(labels.ravel())[1:] gives sizes, ndi.find_objects gives bounding-box slices, and ndi.center_of_mass gives centroids.
  • Filter debris by size with a boolean lookup (lookup[labels]) and relabel if you need consecutive numbers.
  • Labeling quality depends on the mask: smooth, threshold and clean it first; touching objects still share one label.

Next, measure what makes each region special in Contours and Shape Measurements.