【问题标题】:skimage KeyError: 'axis_major_length'skimage KeyError:'axis_major_length'
【发布时间】:2021-12-17 19:20:07
【问题描述】:

我想测量我的二进制分割图像的属性。在我要测量的区域旁边 axis_major_length 和 axis_minor_length。

在创建我的 DataFrame 时,会引发 KeyError。 这是代码:

label_image = measure.label(image > threshold, connectivity=image.ndim)
props = measure.regionprops_table(label_image, image, properties=['area', 'axis_major_length'])

df = pd.DataFrame(props)

这是错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-30-9c2383d91b51> in <module>()
     42 label_image = measure.label(image > threshold, connectivity=image.ndim)
     43 image_label_overlay = label2rgb(label_image, image=image)
---> 44 props = measure.regionprops_table(label_image, image, properties=['area', 'axis_major_length'])
     45 
     46 df = pd.DataFrame(props)

1 frames
/usr/local/lib/python3.7/dist-packages/skimage/measure/_regionprops.py in _props_to_dict(regions, properties, separator)
    500     n = len(regions)
    501     for prop in properties:
--> 502         dtype = COL_DTYPES[prop]
    503         column_buffer = np.zeros(n, dtype=dtype)
    504         r = regions[0][prop]

KeyError: 'axis_major_length'

在图像中应该只有一个被分割的元素,但是有另一个元素被分割为 2 个像素的区域(print(df.area) 的输出):

0       2
1    2789

据我了解,此错误发生在第一个元素上(因为它太小而无法获得主要长度?)。对于面积测量,我使用以下行过滤掉所有小于 25 像素的元素:df = df[df['area'] &gt; 25],但我只能在错误发生后执行此操作。

如何在测量这些属性时过滤掉小元素?

【问题讨论】:

    标签: python image-segmentation scikit-image


    【解决方案1】:

    问题是您正在查看文档的开发版本,其中major_axis_length 已重命名为axis_major_length。这将在 0.19 中改变。但是在最新发布版本的 scikit-image (0.18) 中,它仍然被称为major_axis_length,所以这就是你应该使用的:

    https://scikit-image.org/docs/stable/api/skimage.measure.html#regionprops

    注意网址中的“稳定”。不幸的是,Google 经常返回开发文档而不是稳定版本,但如果您从 scikit-image.org 访问文档,它应该会将您带到最新发布的版本。

    还请注意,旧名称将继续有效,因此您应该使用它们而不必担心它们很快就会损坏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-17
      • 2018-09-16
      • 2019-05-01
      • 2015-04-12
      • 2021-03-12
      • 2020-03-17
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多