[SciPy-user] Indexing question

Alexander Borghgraef alexander.borghgraef.rma@gmail....
Wed Dec 17 03:48:02 CST 2008

Hi all,

I'm doing some image processing work in scipy, and I have a question
regarding indexing 3d arrays. My work is on particle filters (for a
simple example see the cookbook entry I wrote:
http://www.scipy.org/Cookbook/ParticleFilter), which involves
evaluating a list of points (coordinates) in an image (2d array of
shape (height, width)). In my code I represent the list of n points x
as an array of shape (2, n) and I get the corresponding array of n
image intensity values by writing:

 features = im[tuple(x)]

Now I would like to extend the code to deal with vector images of
shape (2, height, width), but I'm not sure on how to do the evaluation
of coordinates here, which should in this case return a list of
vectors in the form of an (2, n) array. I tried:

 features = im[:, tuple(x.T)]

but that was obviously naive and doesn't work. An old-fashioned
solution would be:

 features = []
 for point in x.T:
    features.append(im[:, point[0], point[1]])
 features = array(features)

But clearly I would prefer to work with the indexing features of
numpy. The following works, but it's a pretty ugly hack, and it's not
extentable to length m vectors:

 features = vstack( ( im[ tuple( vstack( (zeros(n, int), x)  ) )  ],
im[ tuple( vstack( (ones(n, int), x)  ) )  ]  ) )

So, can any of you more knowledgeable regarding numpy indexing point
me towards a more elegant solution?

Alex Borghgraef

More information about the SciPy-user mailing list