# mmlabel
Label a binary image.

## Synopsis

y = mmlabel( f, Bc = None )

Implemented in Python.

### Input

f | Image Binary image.
Bc | Structuring Element ( connectivity). Default: `None` (3x3 elementary cross)

### Output

y | Image If number of labels is less than 65535, the data type is uint16, otherwise it is int32.

## Description

mmlabel creates the image `y` by labeling the connect components of a binary image `f`, according to the connectivity defined by the structuring element `Bc`. The background pixels (with value 0) are not labeled. The maximum label value in the output image gives the number of its connected components.

## Examples

Numerical Example

    >>> f=mmbinary([
       [0,1,0,1,1],
       [1,0,0,1,0]])

    >>> g=mmlabel(f)

    >>> print g

    [[0 1 0 2 2]
     [3 0 0 2 0]]

Image Example
The maximum label value gives the number of connected blobs in the image.

    >>> f = mmreadgray('blob3.tif')

    >>> g=mmlabel(f)

    >>> nblobs=mmstats(g,'max')

    >>> print nblobs

    18.0

    >>> mmshow(f)

    >>> mmlblshow(g)

f | g |

## Equation

Where H is the number of image rows, and y1 and y2 are the row and column coordinates respectively.

## Source Code

    def mmlabel(f, Bc=None):
        from Numeric import allclose, ravel, nonzero, array
        if Bc is None: Bc = mmsecross()
        assert mmisbinary,'Can only label binary image'
        zero = mmsubm(f,f)               # zero image
        faux=f
        r = array(zero)
        label = 1
        y = mmgray( f,'uint16',0)        # zero image (output)
        while not allclose(faux,0):
            x=nonzero(ravel(faux))[0]      # get first unlabeled pixel
            fmark = array(zero)
            fmark.flat[x] = 1              # get the first unlabeled pixel
            r = mminfrec( fmark, faux, Bc) # detects all pixels connected to it
            faux = mmsubm( faux, r)        # remove them from faux
            r = mmgray( r,'uint16',label)  # label them with the value label
            y = mmunion( y, r)             # merge them with the labeled image
            label = label + 1
        return y

## See also

mmlabelflat | Label the flat zones of gray-scale images.
mmfreedom | Control automatic data type conversion.
mmsebox | Create a box structuring element.
mmsecross | Diamond structuring element and elementary 3x3 cross.
mmareaopen | Area opening
mmblob | Blob measurements from a labeled image.
mmgrain | Gray-scale statistics for each labeled region.

[`mmhistogram`] [Up] [`mmblob`] |
Copyright (c) 2003, Roberto A. Lotufo, UNICAMP-University of Campinas; Rubens C. Machado, CenPRA-Renato Archer Research Center.