def label(input, structure=None, output=None):
input = numpy.asarray(input)<EOL>if numpy.iscomplexobj(input):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if structure is None:<EOL><INDENT>structure = morphology.generate_binary_structure(input.ndim, <NUM_LIT:1>)<EOL><DEDENT>structure = numpy.asarray(structure, dtype=bool)<EOL>if structure.ndim != input.ndim:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>for ii in structure.shape:<EOL><INDENT>if ii != <NUM_LIT:3>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>need_64bits = input.size >= (<NUM_LIT:2>**<NUM_LIT> - <NUM_LIT:2>)<EOL>if isinstance(output, numpy.ndarray):<EOL><INDENT>if output.shape != input.shape:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>caller_provided_output = True<EOL><DEDENT>else:<EOL><INDENT>caller_provided_output = False<EOL>if output is None:<EOL><INDENT>output = np.empty(input.shape, np.intp if need_64bits else np.int32)<EOL><DEDENT>else:<EOL><INDENT>output = np.empty(input.shape, output)<EOL><DEDENT><DEDENT>if input.ndim == <NUM_LIT:0> or input.size == <NUM_LIT:0>:<EOL><INDENT>if input.ndim == <NUM_LIT:0>:<EOL><INDENT>maxlabel = <NUM_LIT:1> if (input != <NUM_LIT:0>) else <NUM_LIT:0><EOL>output[...] = maxlabel<EOL><DEDENT>else:<EOL><INDENT>maxlabel = <NUM_LIT:0><EOL><DEDENT>if caller_provided_output:<EOL><INDENT>return maxlabel<EOL><DEDENT>else:<EOL><INDENT>return output, maxlabel<EOL><DEDENT><DEDENT>try:<EOL><INDENT>max_label = _ni_label._label(input, structure, output)<EOL><DEDENT>except _ni_label.NeedMoreBits:<EOL><INDENT>tmp_output = np.empty(input.shape, np.intp if need_64bits else np.int32)<EOL>max_label = _ni_label._label(input, structure, tmp_output)<EOL>output[...] = tmp_output[...]<EOL>if not np.all(output == tmp_output):<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT><DEDENT>if caller_provided_output:<EOL><INDENT>return max_label<EOL><DEDENT>else:<EOL><INDENT>return output, max_label<EOL><DEDENT>
Label features in an array.

Parameters
----------
input : array_like
    An array-like object to be labeled.  Any non-zero values in `input` are
    counted as features and zero values are considered the background.
structure : array_like, optional
    A structuring element that defines feature connections.
    `structure` must be symmetric.  If no structuring element is provided,
    one is automatically generated with a squared connectivity equal to
    one.  That is, for a 2-D `input` array, the default structuring element
    is::

        [[0,1,0],
         [1,1,1],
         [0,1,0]]

output : (None, data-type, array_like), optional
    If `output` is a data type, it specifies the type of the resulting
    labeled feature array
    If `output` is an array-like object, then `output` will be updated
    with the labeled features from this function.  This function can
    operate in-place, by passing output=input.
    Note that the output must be able to store the largest label, or this
    function will raise an Exception.

Returns
-------
label : ndarray or int
    An integer ndarray where each unique feature in `input` has a unique
    label in the returned array.
num_features : int
    How many objects were found.

    If `output` is None, this function returns a tuple of
    (`labeled_array`, `num_features`).

    If `output` is a ndarray, then it will be updated with values in
    `labeled_array` and only `num_features` will be returned by this
    function.

See Also
--------
find_objects : generate a list of slices for the labeled features (or
               objects); useful for finding features' position or
               dimensions

Examples
--------
Create an image with some features, then label it using the default
(cross-shaped) structuring element:

>>> a = np.array([[0,0,1,1,0,0],
...               [0,0,0,1,0,0],
...               [1,1,0,0,1,0],
...               [0,0,0,1,0,0]])
>>> labeled_array, num_features = label(a)

Each of the 4 features are labeled with a different integer:

>>> print(num_features)
4
>>> print(labeled_array)
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [2, 2, 0, 0, 3, 0],
       [0, 0, 0, 4, 0, 0]])

Generate a structuring element that will consider features connected even
if they touch diagonally:

>>> s = generate_binary_structure(2,2)

or,

>>> s = [[1,1,1],
         [1,1,1],
         [1,1,1]]

Label the image using the new structuring element:

>>> labeled_array, num_features = label(a, structure=s)

Show the 2 labeled features (note that features 1, 3, and 4 from above are
now considered a single feature):

>>> print(num_features)
2
>>> print(labeled_array)
array([[0, 0, 1, 1, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [2, 2, 0, 0, 1, 0],
       [0, 0, 0, 1, 0, 0]])