[Python] Finding local maxima in large 3D Numpy arrays

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    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]


    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]

    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)


    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?

    Continue reading...

Compartilhe esta Página