问题描述
我正在处理三维numpy数组中存在的一些大体积图像数据.我将用两个小的一维数组来说明我的任务.我有一张图片:
I'm processing some large volumetric image data that are present in three dimensional numpy arrays. I'll explain my task with two small 1D arrays. I have one image:
img = [5, 6, 70, 80, 3, 4, 80, 90]
以及该图像的一个分段和标记版本:
and one segmented and labeled version of that image:
labels = [0, 0, 1, 1, 0, 0, 2, 2]
labels
中的每个数字代表img
中的一个对象.两个阵列具有相同的尺寸.因此,在此示例中,img
中有两个对象:
Each number in labels
represents an object in img
. Both arrays have the same dimensions. So in this example there's two objects in img
:
[5,6, 70 , 80 ,3,4, 80 , 90 ]
[5, 6, 70, 80, 3, 4, 80, 90]
,现在我要尝试的是找到每个对象的最大值的位置,在这种情况下为3
和7
.目前,我遍历所有标签,创建一个img
版本,该版本仅包含与当前标签对应的对象,然后寻找最大值:
and what I'm trying to do now is finding the location of the maximum value of each object, which in this case would be the 3
and 7
. Currently I loop over all labels, create a version of img
which contains only the object corresponding to the current label, and look for the maximum value:
for label in range(1, num_labels + 1):
imgcp = np.copy(img)
imgcp[labels != label] = 0
max_pos = np.argmax(imgcp)
max_coords = np.unravel_index(pos, imgcp.shape)
此方法的一个问题是,在每个步骤中复制img
都容易产生内存错误.我觉得内存管理应该阻止这种情况,但是有没有更高的内存效率并且可能更快的方式来完成此任务?
One problem with this approach is that copying img
in every step tends to create memory errors. I feel like memory management should prevent this, but is there a more memory efficient and possibly faster way to to this task?
推荐答案
这是使用argpartition
的方法.
# small 2d example
>>> data = np.array([[0,1,4,0,0,2,1,0],[0,4,1,3,0,0,0,0]])
>>> segments = np.array([[0,1,1,0,0,2,2,0],[0,1,1,1,0,0,0,0]])
>>>
# discard zeros
>>> nz = np.where(segments)
>>> segc = segments[nz]
>>> dac = data[nz]
# count object sizes
>>> cnts = np.bincount(segc)
>>> bnds = np.cumsum(cnts)
# use counts to partition into objects
>>> idx = segc.argpartition(bnds[1:-1])
>>> dai = dac[idx]
# find maxima per object
>>> mx = np.maximum.reduceat(dai, bnds[:-1])
# find their positions
>>> am, = np.where(dai==mx.repeat(cnts[1:]))
# translate positions back to coordinate space
>>> im = idx[am]
>>> am = *(n[im] for n in nz),
>>>
>>>
# result
# coordinates, note that there are more points than objects because
# the maximum 4 occurs twice in object 1
>>> am
(array([1, 0, 0]), array([1, 2, 5]))
# maxima
>>> data[am]
array([4, 4, 2])
# labels
>>> segments[am]
array([1, 1, 2])
这篇关于在大型3D Numpy数组中查找局部最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!