pythonspace-filling-curve

Reshape from flattened indices in Python


I have an image of size M*N whose pixels coordinates has been flattened to a 1D array according to a space-filling curve (i.e. not a classical rasterization where I could have used reshape).

I thus process my 1D array (flattened image) and I then would like to reshape it to a M*N array (initial size).

So far, I have done this with a for-loop:

for i in range(img_flat.size):
    img_res[x[i], y[i]] = img_flat[i]

x and y being the x and y pixels coordinates according to my path scan.

However, I am wondering how to do this in a unique line of code.


Solution

  • If x and y are numpy arrays of dimension 1 and lengths n, and img_flat also has length n img_res is a numpy array of dimension 2 (h, w) such that `h*w = n, then:

    img_res[x, y] = img_flat
    

    Should suffice