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

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

给出了 numpy.ndarray 的那种

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])

我想在数组(已标记)上使用 scikit-image 来导出一些属性。

我就是这样做的:

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

但是我得到这个错误: TypeError: Only 2-D and 3-D images supported.,指向props有什么问题?我传递给 props 的图像已经是一个二维对象。

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]])

我试过这段代码,没有发现任何错误:

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)

示例输出:

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)

我注意到当 myarray 的所有元素都具有相同的值时(如您的示例所示),labelled 是一个零数组。我还在 regionprops 文档中读到了这个:

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

也许您应该使用具有多个不同值的 myarray 以获得有意义的属性...

我遇到了同样的问题,然后在检查了 Tonechas 的回答后我意识到我是从 scipy 而不是 skimage 导入标签。

from scipy.ndimage.measurements import label

我刚把它换成

from skimage.measure import label, regionprops

一切正常:)