pythonscikit-imagekeyerror

"num_pixels" gives keyError for regionprops_table in scikit-image as property


import skimage
import numpy as np
array = np.array(([1,1],[1,0]))
skimage.measure.regionprops_table(array, properties=("num_pixels", "area_filled"))

gives keyError for "num_pixels", even though it should be available as property. Is it a bug or am I doing something wrong? Thanks!


Solution

  • No it's not you, it's indeed a bug:

    https://github.com/scikit-image/scikit-image/issues/7038

    It was fixed here but the fix is not yet released.

    Until it's released, if you are brave, you can modify your copy of the skimage/measure/_regionprops.py file to match the fix.

    Alternatively, as a workaround, you can use a custom property:

    def num_pixels(binary_image):
        return np.sum(binary_image.astype(int))
    
    table = skimage.measure.regionprops_table(
            array,
            properties=("area_filled",),
            extra_properties=(num_pixels,),
            )