Search code examples
pythonimage-segmentationscikit-image

skimage KeyError: 'axis_major_length'


I want to measure the properties of my binary segmented image. Next to area i want to measure axis_major_length and axis_minor_length.

While creating my DataFrame a KeyError is thrown. This is the code:

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)

This is the error:

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'

In the image there should be only one segmented element, however there is another element segmented with an area of 2 pixels (output of print(df.area)):

0       2
1    2789

In my understanding this error occurs at the first element (because it is too small to get a major length?). For Area measurement i filter out all elements smaller than 25 pixels with following line: df = df[df['area'] > 25] but i can do this operation only after the error occurs.

How can i filter out small elements while measuring those properties?


Solution

  • The problem is that you are looking at the development version of the docs, where major_axis_length has been renamed to axis_major_length. This will change in 0.19. But in the latest released version of scikit-image (0.18), it is still called major_axis_length, so that is what you should use:

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

    Note the "stable" in the URL. Unfortunately Google often returns the development docs instead of the stable version, but if you access the docs from scikit-image.org, it should take you to the latest released version.

    Also note that the old names will continue to work, so you should use them without fear that they will break shortly.