【问题标题】:Calculating just a specific property in regionprops python仅计算 regionprops python 中的特定属性
【发布时间】:2015-03-23 17:10:07
【问题描述】:

我正在使用 scikit-image 中可用的 measure.regionprops 方法来测量连接组件的属性。它计算一堆属性(Python-regionprops)。但是,我只需要每个连接组件的面积。有没有办法只计算一个属性并节省计算量?

【问题讨论】:

    标签: python image-processing scikit-image connected-components


    【解决方案1】:

    似乎有一种更直接的方法可以使用regionprops 和cache=False 来做同样的事情。我使用skimage.segmentation.slic 和n_segments=10000 生成标签。那么:

    rps = regionprops(labels, cache=False)
    [r.area for r in rps]
    

    我对@9​​87654321@ 的理解是设置cache=False 意味着属性在被调用之前不会被计算。根据 Jupyter notebook 中的%%time,运行上面的代码需要 166 毫秒 cache=False 与 247 毫秒 cache=True,所以它似乎工作。

    我尝试了与其他答案等效的方法,但发现速度要慢得多。

    %%time
    ard = np.empty(10000, dtype=int)
    for i in range(10000):
       ard[i] = size(np.where(labels==0)[1])
    

    耗时 34.3 秒。

    这是一个完整的工作示例,使用skimage astronaut 样本图像和通过 slic 分割生成的标签来比较这两种方法:

    import numpy as np
    import skimage
    from skimage.segmentation import slic
    from skimage.data import astronaut
    
    img = astronaut()
    # `+ 1` is added to avoid a region with the label of `0`
    # zero is considered unlabeled so isn't counted by regionprops
    # but would be counted by the other method.
    segments = slic(img, n_segments=1000, compactness=10) + 1
    
    # This is just to make it more like the original poster's 
    # question.
    labels, num = skimage.measure.label(segments, return_num=True)
    

    使用 OP 建议的方法计算面积,调整索引值以避免标签为零:

    %%time
    area = {}
    for i in range(1,num + 1):
        area[i + 1] = np.size(np.where(labels==i)[1])
    

    CPU times: user 512 ms, sys: 0 ns, total: 512 ms Wall time: 506 ms

    使用 regionprops 进行相同的计算:

    %%time
    rps = skimage.measure.regionprops(labels, cache=False)
    area2 = [r.area for r in rps]
    

    CPU times: user 16.6 ms, sys: 0 ns, total: 16.6 ms Wall time: 16.2 ms

    验证结果在元素方面都是相等的:

    np.equal(area.values(), area2).all()
    

    True

    因此,只要考虑零标签和索引差异,两种方法都会给出相同的结果,但没有缓存的 regionprops 更快。

    【讨论】:

    • 只要考虑零标签,结果是一样的。我添加了一个带有结果比较的扩展示例。干杯。
    • 这个答案不正确;缓存只是确定,一旦请求一个属性,它是否保留在内存中。无论标志的值如何,都不会预先计算任何属性。
    【解决方案2】:

    当我们只需要连接组件的面积时,我找到了一种避免使用 regionprops 并计算所有属性的方法。当使用 label 命令完成连接组件的标记时,我们可以通过计算具有给定标签的像素数来计算每个组件的大小。所以,基本上

    labels,num=label(image, return_num=True)
    for i in range(num):
        area[i]=size(np.where(labels==i)[1])
    

    将计算每个连接组件中的像素数。

    【讨论】:

    • 请看我的回答
    【解决方案3】:

    @乐观主义者

    您的 non-regionprops 方法对我来说效率低下。它拾取了一些不需要的噪音并错误地计算了其中一个形状

    import numpy as np
    from skimage.measure import label, regionprops
    import matplotlib.pyplot as plt
    
    arr = np.array([[1,0,1,0,0,0,1],
                    [1,1,1,0,0,0,1],
                    [0,1,1,0,0,0,1],
                    [0,1,1,0,0,1,1],
                    [0,0,0,0,1,1,1],
                    [0,0,0,1,1,1,1],
                    [1,0,0,1,1,1,1],
                    [1,0,0,1,1,1,1],
                    [1,0,0,1,1,1,1]])
    
    area = {}
    labels, num = label(arr, return_num=True)
    for i in range(num):
        print(i)
        area[i]=np.size(np.where(labels==i)[1])
        print(area[i])
    
    plt.imshow(labels)
    plt.show();
    

    rps = regionprops(labels, cache=False)
    [r.area for r in rps]
    
    Out: [9, 24, 3]
    

    【讨论】:

      猜你喜欢
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2017-07-24
      相关资源
      最近更新 更多