仅计算 regionprops python 中的特定 属性

Calculating just a specific property in regionprops python

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

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

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

将计算每个连通分量中的像素数。

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

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

我对 regionprops documentation 的理解是设置 cache=False 意味着属性在调用之前不会被计算。根据 Jupyter Notebook 中的 %%time,运行 上面的代码使用 cache=False 花费了 166 毫秒,使用 cache=True 花费了 247 毫秒,所以它似乎可以工作。

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

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

这花了 34.3 秒。

这是一个完整的工作示例,使用 skimage 宇航员样本图像和切片分割生成的标签来比较两种方法:

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 更快。

@乐观主义者

你的非 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]