scipy.ndimage measurements.labels algorithmunderstand

时间:2016-01-19 03:26:35

标签: python image-processing

好吧,我正在用python学习图像处理。当我看到下面的句子时

 labels, nbr_objects = measurements.label(im)

我想知道它背后的算法,所以我去定义"标签"并查看下面显示的示例

 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

Returns
-------
labeled_array : array_like
    An array-like object where each unique feature has a unique value
num_features : int
    How many objects were found

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

If `output` is an array, 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 = 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]])

那么我如何理解这个例子并了解measurement.labels的算法

1 个答案:

答案 0 :(得分:2)

当你输入'help()'时,通常会获得函数功能的简短定义,并且重点关注代码的工作方式(不同的参数,输出......)。 为了理解函数的基础,这是一个更好的方法来查看更多的理论解释,例如here然后查看函数定义。

如果您了解标签操作,则定义非常明显。总而言之,它只是区分,然后将数字(“标记”)与二进制图像中的每个区域对齐。因此,它有2个输出:区域的数量和与输入的形状具有相同形状的阵列,不同的区域编号。