返回图像的特定numpy标签索引

时间:2014-02-05 18:28:13

标签: numpy label idl indices

我想使用numpy的标签来分割我的图像,然后根据每个标签中找到的索引数量删除那些符合我标准的索引。例如,如果我创建了一个包含我分割区域的图像,并使用scipy的label进行细分:

from numpy import ones, zeros
from numpy.random import random_integers
from scipy.ndimage import label

image = zeros((512, 512), dtype='int')
regionator = ones((11, 11), dtype='int')
xs = random_integers(5, 506, size=500)
ys = random_integers(5, 506, size=500)

for x, y in zip(xs, ys):
    image[x-5:x+6, y-5:y+6] = regionator

labels, n_labels = label(image)

现在,我想检索每个区域的索引,这些索引的大小大于121像素(或一个区域化器大小)。然后,我想取这些索引并将它们设置为零,这样它们就不再是标记图像的一部分。完成此任务的最有效方法是什么?

基本上类似于MATLAB的regionprops或利用其直方图函数的IDL reverse_indices输出。

2 个答案:

答案 0 :(得分:2)

我会使用bincount和阈值来生成查找表:

import numpy as np

threshold = 121

size = np.bincount(labels.ravel())
keep_labels = size <= threshold
# Make sure the background is left as 0/False
keep_labels[0] = 0
filtered_labels = keep_labels[labels]

在上面的最后一部分,我使用数组keep_labels索引数组labels。这在numpy中称为advanced indexing,它要求labels为整数数组。 Numpy然后使用labels的元素作为keep_labels的索引,并生成与labels形状相同的数组。

答案 1 :(得分:1)

到目前为止,我发现这对我有用,即使对于大型数据集也有良好的表现。

使用从here获取的获取索引流程我来到这里:

from numpy import argsort, histogram, reshape, where
import bisect

h = histogram(labels, bins=n_labels)
h_inds = where(h[0] > 121)[0]

labels_f = labels.flatten()
sortedind = argsort(labels_f)
sorted_labels_f = labels_f[sortedind]
inds = []
for i in range(1, len(h_inds)):
    i1 = bisect.bisect_left(sorted_labels_f, h[1][h_inds[i]])
    i2 = bisect.bisect_right(sorted_labels_f, h[1][h_inds[i]])
    inds.extend(sortedind[i1:i2])

# Now get rid of all of those indices that were part of a label
# larger than 121 pixels
labels_f[inds] = 0
filtered_labels = reshape(labels_f, (512, 512))