opt*_*ist 6 python image-processing scikit-image connected-components
我使用scikit-image中提供的measure.regionprops方法来测量连接组件的属性.它计算了一堆属性(Python-regionprops).但是,我只需要每个连接组件的区域.有没有办法只计算一个属性并节省计算?
似乎是用来做同样的事情更直接的方式regionprops用cache=False.我使用skimage.segmentation.slicwith 生成标签n_segments=10000.然后:
rps = regionprops(labels, cache=False)
[r.area for r in rps]
Run Code Online (Sandbox Code Playgroud)
我对regionprops文档的理解是,设置cache=False意味着在调用属性之前不会计算属性.根据%%timeJupyter笔记本中的说法,运行上面的代码需要166ms而cache=Falsevs 247ms cache=True,所以它似乎有效.
我尝试了相当于另一个答案,发现它慢得多.
%%time
ard = np.empty(10000, dtype=int)
for i in range(10000):
ard[i] = size(np.where(labels==0)[1])
Run Code Online (Sandbox Code Playgroud)
花了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)
Run Code Online (Sandbox Code Playgroud)
使用OP建议的方法计算区域,并调整索引值以避免标签为零:
%%time
area = {}
for i in range(1,num + 1):
area[i + 1] = np.size(np.where(labels==i)[1])
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
CPU times: user 16.6 ms, sys: 0 ns, total: 16.6 ms
Wall time: 16.2 ms
验证结果是否完全相同:
np.equal(area.values(), area2).all()
Run Code Online (Sandbox Code Playgroud)
True
因此,只要考虑零标签和索引的差异,两种方法都会给出相同的结果,但没有缓存的regionprops会更快.