[Python] How to efficiently retrieve xy-coordinates from image

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have an image img with 1000 rows and columns each.
    Now I would like to consider each pixel as x- and y-coordinates and extract the respective value.

    An illustrated example of what I want to achieve:

    [​IMG]

    In theory, the code snippet below should work. But it is super slow (I stopped the execution after some time).

    img = np.random.rand(1000,1000)
    xy = np.array([(x, y) for x in range(img.shape[1]) for y in range(img.shape[0])])
    xy = np.c_[xy, np.zeros(xy.shape[0])]
    for i in range(img.shape[0]):
    for j in range(img.shape[1]):
    xy[np.logical_and(xy[:,1] == i, xy[:,0] == j),2] = img[i,j]


    Is there a faster way (e.g. some numpy magic) to go from one table to the other?

    Thanks in advance!

    Continue reading...

Compartilhe esta Página