Scikit图像:计算图像对象中细胞的正确方法

时间:2017-02-22 15:44:18

标签: python numpy image-processing scikit-image

假设您有一张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,但目的是迭代对象。 怎么做?

2 个答案:

答案 0 :(得分:1)

一些测试和研究有时会有很长的路要走。

问题在于router.post('/assignSkills', function (req, res, next) { var record = req.body.data; model.users.findOne({ where: { id: record.userId } }).then(function (user) { model.skills.findOne({ where: { id: record.skillId } }).then(function (skill) { user.addSkill(skill); res.send(user); }); }); }); ,因为它没有携带不同的标签,只有blobs个值,而0,1需要用循环遍历{{1}的迭代器替换1}}。

这个解决方案似乎有效:

label

结果:

range(0,no_objects)

如果有人想检查import skimage.measure as measure import numpy from scipy import ndimage from skimage.measure import regionprops 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]]) blobs = numpy.where(vals>17, 1, 0) labels, no_objects = ndimage.label(blobs) #blobs is not in an amicable type to be processed right now, so: labelled=ndimage.label(blobs) resh_labelled=labelled[0].reshape((vals.shape[0],vals.shape[1])) #labelled is a tuple: only the first element matters #here come the props props=measure.regionprops(resh_labelled) #here come the sought-after areas size={i:props[i].area for i in range (0, no_objects)}

In[1]: size
Out[1]: {0: 2, 1: 2, 2: 1, 3: 2}

如果有人想要找到找到的4个物体:

labels

enter image description here

答案 1 :(得分:1)

regionprops会产生比每个blob区域更多的信息。因此,如果您只是想要获取blob的像素数,作为替代方案并且关注性能,我们可以使用ndimage.label np.bincount(labels.ravel())[1:] 获得In [53]: labeled_areas = np.bincount(labels.ravel())[1:] In [54]: labeled_areas Out[54]: array([2, 2, 1, 2]) ,就像这样 -

In [55]: dict(zip(range(no_objects), labeled_areas))
Out[55]: {0: 2, 1: 2, 2: 1, 3: 2}

因此,对于给定的样本 -

{{1}}

要将这些结果放在字典中,还需要另外一步 -

{{1}}