Scikit-image: regionprops_table behaves differently than regionprops list on no-regions found

Created on 5 Nov 2019  路  4Comments  路  Source: scikit-image/scikit-image

Description

Another quirk with moving to regionprops_table from the regionprops list generation.

When passing it in data that ends up having zero labels, regionprops_table will raise an IndexError as props_to_dict in _regionprops.py looks like it assumes non-zero regions found:

r = regions[0][prop]
is where the exception gets thrown, where we were more expecting empty lists/dicts as per the original regionprops behavior.

Way to reproduce

from skimage.data import binary_blobs
from skimage.measure import regionprops, regionprops_table, label
import numpy as np

blobs = binary_blobs()

rprops = regionprops(label(blobs))
print("regionprops iter result")
print(len(rprops)) 

blobs_table = regionprops_table(label(blobs), properties=['centroid', 'local_centroid'])
print("regionprops table result")
print(blobs_table)

# No regions, exceptions
noblobs = np.zeros((64, 64, 64))
no_rprops = regionprops(label(noblobs))
print(f'regionprops blank: {no_rprops}')
blank_table = regionprops_table(label(noblobs), properties=['centroid', 'local_centroid'])

Output

regionprops iter result
9
regionprops table result
{'centroid-0': array([119,  33,  17, 122, 278, 184, 347, 362, 449]), 'centroid-1': array([ 39, 240, 492, 441, 178, 155, 451, 325, 195]), 'local_centroid-0': array([119,  33,  17,  69, 154,  29, 127,  30,  75]), 'local_centroid-1': array([ 39, 153,  22, 108, 178,  29,  66,  28,  88])}
regionprops blank []
Traceback (most recent call last):
  File "bblob.py", line 20, in <module>
    notable = regionprops_table(label(noblobs), properties=['centroid', 'local_centroid'])
  File "/opt/anaconda/envs/pipeline_env/lib/python3.7/site-packages/skimage/measure/_regionprops.py", line 625, in regionprops_table
    return _props_to_dict(regions, properties=properties, separator=separator)
  File "/opt/anaconda/envs/pipeline_env/lib/python3.7/site-packages/skimage/measure/_regionprops.py", line 504, in _props_to_dict
    r = regions[0][prop]
IndexError: list index out of range

Version information

```python
3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21)
[GCC 7.3.0]
Linux-4.15.0-66-generic-x86_64-with-debian-buster-sid
scikit-image version: 0.16.2
numpy version: 1.16.4

Most helpful comment

@alexdesiqueira you just linked back to this issue! :joy: Anyway, regarding the other issue, no, they are independent. This one is simple enough and we should fix it quickly: if regionprops is empty then we should return a dictionary with all the right keys but mapping to empty arrays. The trick is that for some properties we use the first result to get the right number of columns. Now we'll need to make a synthetic image to find them.

All 4 comments

Hi @dmyung,
do you think this is related to #4280?

@alexdesiqueira you just linked back to this issue! :joy: Anyway, regarding the other issue, no, they are independent. This one is simple enough and we should fix it quickly: if regionprops is empty then we should return a dictionary with all the right keys but mapping to empty arrays. The trick is that for some properties we use the first result to get the right number of columns. Now we'll need to make a synthetic image to find them.

@jni that's it. I'm going mad, no return from here. :sweat_smile:

I think this has been solved since the code above does not error any more. I'm closing but of course feel free to reopen the issue if the problem persists.

Was this page helpful?
0 / 5 - 0 ratings