machinevisiontoolbox.Image.labels_binary

Image.labels_binary(connectivity=4, ltype='int32')

Blob labelling

Parameters:
  • connectivity (int, optional) – number of neighbours used for connectivity: 4 [default] or 8

  • ltype (string, optional) – output image type: ‘int32’ [default], ‘uint16’

Returns:

label image, number of regions

Return type:

Image, int

Compute labels of connected components in the input greyscale or binary image. Regions are sets of contiguous pixels with the same value.

The method returns the label image and the number of labels N, so labels lie in the range [0, N-1].The value in the label image in an integer indicating which region the corresponding input pixel belongs to. The background has label 0.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image.Squares(2, 15)
>>> img.print()
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> labels, N = img.labels_binary()
>>> N
5
>>> labels.print()
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 1 1 1 1 1 0 2 2 2 2 2 0 0
 0 0 1 1 1 1 1 0 2 2 2 2 2 0 0
 0 0 1 1 1 1 1 0 2 2 2 2 2 0 0
 0 0 1 1 1 1 1 0 2 2 2 2 2 0 0
 0 0 1 1 1 1 1 0 2 2 2 2 2 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 3 3 3 3 3 0 4 4 4 4 4 0 0
 0 0 3 3 3 3 3 0 4 4 4 4 4 0 0
 0 0 3 3 3 3 3 0 4 4 4 4 4 0 0
 0 0 3 3 3 3 3 0 4 4 4 4 4 0 0
 0 0 3 3 3 3 3 0 4 4 4 4 4 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Note:
  • This algorithm is variously known as region labelling, connectivity analysis, region coloring, connected component analysis, blob labelling.

  • The output image is the same size as the input image.

  • The input image can be binary or greyscale.

  • Connectivity is performed using 4 nearest neighbours by default.

  • 8-way connectivity introduces ambiguities, a chequerboard is two blobs.

References:
  • Robotics, Vision & Control for Python, Section 12.1.2.1, P. Corke, Springer 2023.

Seealso:

blobs cv2.connectedComponents labels_graphseg labels_MSER