python skimage.measure.label标记不同连通域

python 的skimage库中的measure.label可用于标记不同连通域,从而方便图像分析

skimage.measure.label(label_image, background=None, return_num=False, connectivity=None)

源码如下:

@deprecate_kwarg({"input": "label_image"}, removed_version="1.0")
def label(label_image, background=None, return_num=False, connectivity=None):
    r"""Label connected regions of an integer array.
    Two pixels are connected when they are neighbors and have the same value.
    In 2D, they can be neighbors either in a 1- or 2-connected sense.
    The value refers to the maximum number of orthogonal hops to consider a
    pixel/voxel a neighbor::
      1-connectivity     2-connectivity     diagonal connection close-up
           [ ]           [ ]  [ ]  [ ]             [ ]
            |               \  |  /                 |  <- hop 2
      [ ]--[x]--[ ]      [ ]--[x]--[ ]        [x]--[ ]
            |               /  |  \             hop 1
           [ ]           [ ]  [ ]  [ ]
    Parameters
    ----------
    label_image : ndarray of dtype int
        Image to label.
    background : int, optional
        Consider all pixels with this value as background pixels, and label
        them as 0. By default, 0-valued pixels are considered as background
        pixels.
    return_num : bool, optional
        Whether to return the number of assigned labels.
    connectivity : int, optional
        Maximum number of orthogonal hops to consider a pixel/voxel
        as a neighbor.
        Accepted values are ranging from  1 to input.ndim. If ``None``, a full
        connectivity of ``input.ndim`` is used.
    Returns
    -------
    labels : ndarray of dtype int
        Labeled array, where all connected regions are assigned the
        same integer value.
    num : int, optional
        Number of labels, which equals the maximum label index and is only
        returned if return_num is `True`.
    See Also
    --------
    regionprops
    regionprops_table
    References
    ----------
    .. [1] Christophe Fiorio and Jens Gustedt, "Two linear time Union-Find
           strategies for image processing", Theoretical Computer Science
           154 (1996), pp. 165-181.
    .. [2] Kensheng Wu, Ekow Otoo and Arie Shoshani, "Optimizing connected
           component labeling algorithms", Paper LBNL-56864, 2005,
           Lawrence Berkeley National Laboratory (University of California),
           http://repositories.cdlib.org/lbnl/LBNL-56864
    Examples
    --------
    >>> import numpy as np
    >>> x = np.eye(3).astype(int)
    >>> print(x)
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    >>> print(label(x, connectivity=1))
    [[1 0 0]
     [0 2 0]
     [0 0 3]]
    >>> print(label(x, connectivity=2))
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    >>> print(label(x, background=-1))
    [[1 2 2]
     [2 1 2]
     [2 2 1]]
    >>> x = np.array([[1, 0, 0],
    ...               [1, 1, 5],
    ...               [0, 0, 0]])
    >>> print(label(x))
    [[1 0 0]
     [1 1 2]
     [0 0 0]]
    """
    if label_image.dtype == bool:
        return _label_bool(label_image, background=background,
                           return_num=return_num, connectivity=connectivity)
    else:
        return clabel(label_image, background, return_num, connectivity)
© 2021 GitHub, Inc.

当两个像素相邻时,两个像素连接在一起并且具有相同的值。在2D模式下,它们可以是1或2连通的邻居。该值是指将像素视为邻居的最大正交跳数

1-connectivity     2-connectivity     diagonal connection close-up

     [ ]           [ ]  [ ]  [ ]             [ ]
      |               \  |  /                 |  <- hop 2
[ ]--[x]--[ ]      [ ]--[x]--[ ]        [x]--[ ]
      |               /  |  \             hop 1
     [ ]           [ ]  [ ]  [ ]

示例:

>>> import numpy as np
>>> x = np.eye(3).astype(int)
>>> print(x)
[[1 0 0]
 [0 1 0]
 [0 0 1]]
>>> print(label(x, connectivity=1))
[[1 0 0]
 [0 2 0]
 [0 0 3]]
>>> print(label(x, connectivity=2))
[[1 0 0]
 [0 1 0]
 [0 0 1]]
>>> print(label(x, background=-1))
[[1 2 2]
 [2 1 2]
 [2 2 1]]
>>> x = np.array([[1, 0, 0],
...               [1, 1, 5],
...               [0, 0, 0]])
>>> print(label(x))
[[1 0 0]
 [1 1 2]
 [0 0 0]]
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 python 中导入 scikit-imagemeasure 模块可以使用以下语句: ``` import skimage.measure ``` scikit-image 是一个用于图像处理的 Python 库,measure 模块提供了许多用于图像测量的函数,例如计算图像的尺寸、轮廓等。 如果你尚未安装 scikit-image 库,可以使用 pip 安装: ``` pip install -U scikit-image ``` ### 回答2: 在使用Python的scikit-image库时,可以使用以下语句导入skimage.measure模块: import skimage.measure skimage.measure是scikit-image库中的一个子模块,用于测量图像的不同属性和特征。通过导入这个模块,我们可以使用其中提供的函数和方法来执行各种图像测量任务。 例如,通过skimage.measure模块可以进行图像的周长测量、区域的均值计算、不同形状的拟合以及骨架化等操作。该模块还提供了其他一些用于图像特征提取和测量的函数,可用于图像分析、计算机视觉和机器学习等领域。 通过导入skimage.measure模块,我们可以轻松地访问这些图像测量功能,并使用其提供的函数和方法来完成各种图像处理任务。我们可以根据具体的需求来选择合适的函数和方法,并根据其返回的结果来进一步处理图像或提取所需的信息。 总之,通过使用语句import skimage.measure,我们可以导入scikit-image库中的skimage.measure模块,以便在图像处理中使用其中提供的各种测量功能和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值