使用 skimage regionprops 过滤区域并使用过滤组件创建掩码

问题描述

我已经通过 Otsu 方法生成一个掩码,现在我必须从中消除某些不满足面积或偏心率条件的封闭区域。 我使用 skimage.measure.label 函数标记蒙版,然后对其应用 skimage.measure.regionprops获取有关区域面积和离心率的信息。代码如下:

lab_img = label(mask)
regions = regionprops(lab_img)

for region in regions:
    if (condition):
        # Remove this region

# final_mask = mask (but without the discarded regions)

有谁知道一种有效的方法来做到这一点? 谢谢!

解决方法

这是获得所需内容的一种方法。关键函数是 map_array,它允许您根据输入和输出值重新映射数组中的值,就像使用 Python 字典一样。因此,您可以使用 regionprops_table 创建一个属性表,然后使用 NumPy 快速过滤这些列,最后使用标签列重新映射。举个例子:

from skimage import measure,util

...

lab_image = measure.label(mask)

# table is a dictionary mapping column names to data columns
# (NumPy arrays)
table = regionprops_table(
    lab_image,properties=('label','area','eccentricity'),)
condition = (table['area'] > 10) & (table['eccentricity'] < 0.5)

# zero out labels not meeting condition
input_labels = table['label']
output_labels = input_labels * condition

filtered_lab_image = util.map_array(
    lab_image,input_labels,output_labels
)