如何在Python中的二进制图像上使用skiliage.measure.regionprops按面积或偏心率进行过滤

2024-04-19 12:09:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个二值图像的路面,我试图隔离坑只。使用skipage.measure.regionprops和skipage.measure.label,我可以为图像中的不同标签生成一个属性表

然后如何使用这些值进行过滤例如,使用面积、轴长度或偏心率关闭某些标签。 Input, labled Image and properties table

使用Python3


Tags: and图像imageinput属性标签labelmeasure
1条回答
网友
1楼 · 发布于 2024-04-19 12:09:55

我会用熊猫和^{}来得到你想要的:

import pandas as pd
import imageio as iio
from skimage.measure import regionprops_table, label

image = np.asarray(iio.imread('path/to/image.png'))
labeled = label(image > 0)  # ensure input is binary
data = regionprops_table(
        labeled,
        properties=('label', 'eccentricity'),
        )
table = pd.DataFrame(data)
table_sorted_by_ecc = table.sort_values(
        by='eccentricity', ascending=False
        )

# print e.g. the 10 most eccentric labels
print(table_sorted.iloc[:10])

例如,如果您希望仅使用最偏心的标签生成标签图像,您可以执行以下操作:

eccentric_label = table['labels'].iloc[np.argmax(table['eccentricity'])]
labeled_ecc = np.where(labeled == eccentric_label, eccentric_label, 0)

您还可以做更复杂的事情,例如,仅使用高于某个偏心率的标签制作标签图像。下面,我们使用NumPy元素乘法生成一个数组,如果该数组的原始标签具有高偏心率,则该数组为原始标签,否则为0。然后,我们使用^{}函数将原始标签映射到自身或0,这取决于偏心率

from skimage.util import map_array

ecc_threshold = 0.3
eccentric_labels = table['labels'] * (table['eccentricity'] > ecc_threshold)
new_labels = map_array(
        labeled,
        np.asarray(table['labels']),
        np.asarray(eccentric_labels),
        )

相关问题 更多 >