【发布时间】:2017-02-22 15:44:18
【问题描述】:
假设您有一个numpy.array 形式的图像:
vals=numpy.array([[3,24,25,6,2],[8,7,6,3,2],[1,4,23,23,1],[45,4,6,7,8],[17,11,2,86,84]])
如果阈值为17(示例),您想计算每个对象内部有多少个单元格:
from scipy import ndimage
from skimage.measure import regionprops
blobs = numpy.where(vals>17, 1, 0)
labels, no_objects = ndimage.label(blobs)
props = regionprops(blobs)
如果你检查,这会给出一个超过阈值的 4 个不同对象的图像:
In[1]: blobs
Out[1]:
array([[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 1, 1]])
事实上:
In[2]: no_objects
Out[2]: 4
我想计算每个对象的单元格(或面积)数。预期的结果是具有object ID: number of cells 格式的字典:
size={0:2,1:2,2:1,3:2}
我的尝试:
size={}
for label in props:
size[label]=props[label].area
返回错误:
Traceback (most recent call last):
File "<ipython-input-76-e7744547aa17>", line 3, in <module>
size[label]=props[label].area
TypeError: list indices must be integers, not _RegionProperties
我知道我错误地使用了label,但其目的是迭代对象。 如何做到这一点?
【问题讨论】:
标签: python numpy image-processing scikit-image