CF84 CF84 - 4 months ago 22
Python Question

Python: TypeError: Only 2-D and 3-D images supported with scikit-image regionprops

Given a

numpy.ndarray
of the kind

myarray=
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1])


I want to use
scikit-image
on the array (which is already labelled) to derive some properties.

This is what I do:

myarray.reshape((11,11))
labelled=label(myarray)
props=sk.measure.regionprops(labelled)


But then I get this error:
TypeError: Only 2-D and 3-D images supported.
, pointing at
props
. What is the problem? The image I am passing to
props
is already a 2D object.


Shape of
myarray
:

In [17]: myarray
Out[17]:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

Answer

I tried this code and I got no errors:

import numpy as np
from skimage.measure import label, regionprops

myarray = np.random.randint(1, 4, (11,11), dtype=np.int64)
labelled = label(myarray)
props = regionprops(labelled)

Sample output:

In [714]: myarray
Out[714]: 
array([[1, 2, 1, 1, 3, 3, 1, 1, 3, 3, 3],
       [1, 1, 3, 1, 3, 2, 2, 2, 3, 3, 2],
       [3, 3, 3, 1, 3, 3, 1, 1, 2, 3, 1],
       [1, 3, 1, 1, 1, 2, 1, 3, 1, 3, 3],
       [3, 2, 3, 3, 1, 1, 2, 1, 3, 2, 3],
       [3, 2, 1, 3, 1, 1, 3, 1, 1, 2, 2],
       [1, 3, 1, 1, 1, 1, 3, 3, 1, 2, 2],
       [3, 3, 1, 1, 3, 2, 1, 2, 2, 2, 1],
       [1, 1, 1, 3, 3, 2, 2, 3, 3, 3, 1],
       [1, 2, 2, 2, 2, 2, 1, 3, 3, 2, 2],
       [3, 2, 2, 3, 1, 3, 3, 1, 3, 3, 2]], dtype=int64)

In [715]: labelled
Out[715]: 
array([[ 0,  1,  0,  0,  2,  2,  3,  3,  4,  4,  4],
       [ 0,  0,  5,  0,  2,  6,  6,  6,  4,  4,  7],
       [ 5,  5,  5,  0,  2,  2,  0,  0,  6,  4,  8],
       [ 9,  5,  0,  0,  0, 10,  0,  4,  0,  4,  4],
       [ 5, 11,  5,  5,  0,  0, 10,  0,  4, 12,  4],
       [ 5, 11,  0,  5,  0,  0, 13,  0,  0, 12, 12],
       [14,  5,  0,  0,  0,  0, 13, 13,  0, 12, 12],
       [ 5,  5,  0,  0, 15, 12,  0, 12, 12, 12, 16],
       [ 0,  0,  0, 15, 15, 12, 12, 17, 17, 17, 16],
       [ 0, 12, 12, 12, 12, 12, 18, 17, 17, 19, 19],
       [20, 12, 12, 21, 22, 17, 17, 18, 17, 17, 19]], dtype=int64)

In [716]: props[0].area
Out[716]: 1.0

In [717]: props[1].centroid
Out[717]: (1.0, 4.4000000000000004)

I noticed that when all the elements of myarray have the same value (as in your example), labelled is an array of zeros. I also read this in the regionprops documentation:

Parameters:     label_image : (N, M) ndarray
                               Labeled input image. Labels with value 0 are ignored.

Perhaps you should use a myarray with more than one distinct value in order to get meaningful properties...

Comments