Scikit-image:为什么多个对象的主轴长度为零?

Scikit-image: why major axis length is zero with multiple objects?

具有多个对象的图像被标记为:

image=
[[ 1  0  0  0  2  2  0  3  3  3  3]
 [ 3  0  0  0  0  0  0  0  4  4  4]
 [ 4  0  5  5  0  0  0  0  6  6  6]
 [ 6  0  0  0  0  0  0  0  0  7  7]
 [ 7  0  0  0  0  0  0  0  0  8  8]
 [ 0  0  0  0  0  0  0  9  9  9  9]
 [ 9  0  0  0  0  0 10 10 10 10 10]
 [ 0  0  0  0  0  0  0  0  0  0 11]
 [ 0  0  0  0  0  0  0  0  0  0 12]
 [12 12  0  0 13 13  0  0  0  0 14]
 [14 14  0  0  0  0  0  0  0  0 15]]

因为想知道等效椭圆的长轴长度,所以用到图像处理的这个函数:

import skimage as sk
from skimage import measure
props=sk.measure.regionprops(image)
maj_ax_le=round(props[0].major_axis_length,3)

但是当我询问结果时,我得到:

In [1]: maj_ax_le
Out[1]: 0.0

这是因为存在多个对象(在本例中为 15 个)吗?如果是这样,我如何计算所有对象的个体 maj_ax_le

可以阅读 regionprops 文档:

Returns:     properties : list of RegionProperties
                            Each item describes one labeled region, and can be accessed using the
                            attributes listed below.

因此,要获得图像中所有对象的长轴长度,您只需迭代 props:

import numpy as np
from skimage.measure import regionprops

img = np.array([[ 1,  0,  0,  0,  2,  2,  0,  3,  3,  3,  3],
                [ 3,  0,  0,  0,  0,  0,  0,  0,  4,  4,  4],
                [ 4,  0,  5,  5,  0,  0,  0,  0,  6,  6,  6],
                [ 6,  0,  0,  0,  0,  0,  0,  0,  0,  7,  7],
                [ 7,  0,  0,  0,  0,  0,  0,  0,  0,  8,  8],
                [ 0,  0,  0,  0,  0,  0,  0,  9,  9,  9,  9],
                [ 9,  0,  0,  0,  0,  0, 10, 10, 10, 10, 10],
                [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 11],
                [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12],
                [12, 12,  0,  0, 13, 13,  0,  0,  0,  0, 14],
                [14, 14,  0,  0,  0,  0,  0,  0,  0,  0, 15]])

props = regionprops(img)

print 'Label \tMajor axis'
for p in props:
    print '%5d %12.3f' % (p.label, p.major_axis_length)

这就是你得到的:

Label   Major axis
    1        0.000
    2        2.000
    3       14.259
    4       15.934
    5        2.000
    6       15.934
    7       18.085
    8        2.000
    9       14.259
   10        5.657
   11        0.000
   12       18.085
   13        2.000
   14       18.085
   15        0.000