Wednesday, 15 August 2012

python - numpy get 2d array where last dimension is indexed according to a 2d array -


I had read on numpy sequence, but I did not find what I had searched for.

I is a 288 * 384 image, where there may be labeling in [0,15] in each pixel. It is stored in a 3d (288,384,16) numpy array im .

with im [:,:, 1] , I can, for example, get the picture where the label is 1 in all the pixels.

I have another 2d array labeling , (288 * 384) -speed, which contains a label for each pixel.

How do I get this image where each pixel has pixels related to the use of some clever pieces?

Using a loop, it will:

  result = x [x, y] = im [x, y, labeling [x, y]]  

but it is absolutely disabled.

new result

concise results

  np.choose (labeling, im.transpose (2,0,1))  

Older results

Try it out < / P>

  im [np.arange (288) [:, none], np.arange (384) [any ,:], labeling]  

The following Works for the written position:

  import nump Y import numpy.random import itertools a = numpy.random.randint (5, size = (2,3,4)) array ([ [4, 4, 0, 0], [0, 4, 1, 1], [3, 4, 4, 2]], [[4, 0, 0, 2], [1, 4, 2, 2] ], [4, 2, 4, 4]]] b = numpy.random (0, 1, 2, 2). Ranges (3) [Any ,:], B] array ([[4, 4, 3], [0, 2, 4]] # Note that the zip is not doing what you want to result in = np x (x), x, y, b [x, y]] array (results) [ X, y] = (zero (2), range (3) [[4., 4, 3.], [0., 2., 4.]]  

meditation Please do not zip you are not

  zip (category (2), category (3)) [(0, 0), (1, 1)] Maybe  

P means something like (itertools.product (Category (2), Category (3)), [(0, 0), ([0, 0), ( 0, 1)), (0, 2), (1, 0), (1, 1), (1, 2)]

View badly [: , None]] etc. numpy.ix_

  xx, yy = np.ix_ (np.arange (2 ), Np.arange (3)) res = a [xx, yy, b]  

No comments:

Post a Comment