使用 skimage regionprops 过滤区域并创建具有过滤组件的蒙版

Filter regions using skimage regionprops and create a mask with filtered components

我已经通过 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, which lets you remap the values in an array based on input and output values, like with a Python dictionary. So you could create a table of properties using 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
)