从numpy数组中提取最大/最小标记的补丁
我有一个很大的numpy数组,并且用scipy进行了连通组件标记。现在我想从这个数组中创建一些子集,只保留最大的或最小的标签。显然,这两种情况可能会出现多次。
import numpy
from scipy import ndimage
....
# Loaded in my image file here. To big to paste
....
s = ndimage.generate_binary_structure(2,2) # iterate structure
labeled_array, numpatches = ndimage.label(array,s) # labeling
# get the area (nr. of pixels) of each labeled patch
sizes = ndimage.sum(array,labeled_array,range(1,numpatches+1))
# To get the indices of all the min/max patches. Is this the correct label id?
map = numpy.where(sizes==sizes.max())
mip = numpy.where(sizes==sizes.min())
# This here doesn't work! Now i want to create a copy of the array and fill only those cells
# inside the largest, respecitively the smallest labeled patches with values
feature = numpy.zeros_like(array, dtype=int)
feature[labeled_array == map] = 1
有人能给我一些提示,告诉我该怎么做吗?
2 个回答
3
首先,你需要一个标记好的掩膜,这个掩膜里只有0(背景)和1(前景):
labeled_mask, cc_num = ndimage.label(mask)
接下来,找出最大的连通区域:
largest_cc_mask = (labeled_mask == (np.bincount(labeled_mask.flat)[1:].argmax() + 1))
你可以通过使用argmin()来找出最小的物体。
6
这是完整的代码:
import numpy
from scipy import ndimage
array = numpy.zeros((100, 100), dtype=np.uint8)
x = np.random.randint(0, 100, 2000)
y = np.random.randint(0, 100, 2000)
array[x, y] = 1
pl.imshow(array, cmap="gray", interpolation="nearest")
s = ndimage.generate_binary_structure(2,2) # iterate structure
labeled_array, numpatches = ndimage.label(array,s) # labeling
sizes = ndimage.sum(array,labeled_array,range(1,numpatches+1))
# To get the indices of all the min/max patches. Is this the correct label id?
map = numpy.where(sizes==sizes.max())[0] + 1
mip = numpy.where(sizes==sizes.min())[0] + 1
# inside the largest, respecitively the smallest labeled patches with values
max_index = np.zeros(numpatches + 1, np.uint8)
max_index[map] = 1
max_feature = max_index[labeled_array]
min_index = np.zeros(numpatches + 1, np.uint8)
min_index[mip] = 1
min_feature = min_index[labeled_array]
注意事项:
numpy.where
返回的是一个元组- 标签1的大小是
sizes[0]
,所以你需要在numpy.where
的结果上加1 - 如果想要得到一个包含多个标签的掩码数组,可以用
labeled_array
来作为标签掩码数组的索引。
结果: