我正在处理一些大体积的图像数据,这些数据以三维纽比阵列的形式出现。我将用两个小的一维阵列来解释我的任务。我有一个形象:
img = [5, 6, 70, 80, 3, 4, 80, 90]
还有一个经过分割和标记的图像:
^{pr2}$labels
中的每个数字代表img
中的一个对象。两个数组的维数相同。所以在这个例子中,img
中有两个对象:
[5,6,70,80,3,4,80,90]
我现在要做的是找到每个对象的最大值的位置,在本例中是3
和{img
版本,该版本只包含与当前标签对应的对象,并查找最大值:
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
会导致内存错误。我觉得内存管理应该可以防止这种情况发生,但是有没有一种内存效率更高、速度更快的方法来完成这项任务呢?在
下面是一个使用
argpartition
的方法。在相关问题 更多 >
编程相关推荐