[Numpy-discussion] 2D (or n-d) fancy indexing?
Wed Oct 8 20:25:55 CDT 2008
On Wed, Oct 8, 2008 at 20:00, Zachary Pincus <firstname.lastname@example.org> wrote:
> Hello all,
> I'm doing something silly with images and am unable to figure out the
> right way to express this with "fancy indexing" -- or anything other
> than a brute for-loop for that matter.
> The basic gist is that I have an array representing n images, of shape
> (n, x, y). I also have a "map" of shape (x, y), which contains indices
> in the range [0, n-1]. I then want to construct the "composite" image,
> of shape (x, y), with pixels selected from the n source images as per
> the indices in the map, i.e.:
> composite[x, y] = images[map[x, y], x, y]
> for all (x, y).
> Now, I can't figure out if there's an easy way to express this in
> numpy. For that matter, I can't even figure out a simple way to do the
> 1D version of the same:
> composite[i] = images[map[i], i]
> where composite and map have shape (m,), and images has shape (n, m).
> Can anyone assist? Surely there's something simple that I'm just not
You need to give an array for each axis. Each of these arrays will be
broadcast against each other to form three arrays of the desired shape
of composite. This is discussed in the manual here:
Conceptually, you need arrays A, B, and C such that
composite[x,y] == images[A[x,y], B[x,y], C[x,y]]
for all x,y
You already have A, you just need to construct B and C correctly.
Here is an example:
In : from numpy import *
In : Nx = 480
In : Ny = 640
In : N = 100
In : images = random.randint(0, 1000, size=(N, Nx, Ny))
In : map = random.randint(0, N, size=(Nx, Ny))
In : composite = images[map, arange(Nx)[:,newaxis], arange(Ny)]
In : for x in range(Nx):
....: for y in range(Ny):
....: assert composite[x,y] == images[map[x,y],x,y]
When arange(Nx)[:,newaxis] and arange(Ny) get broadcasted with map,
you get (480,640) arrays like you would get with mgrid[0:Nx,0:Ny].
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion