简体   繁体   中英

Advanced integer indexing with l x m x n broadcast shape

Earlier today I asked this about integer array indexing and I am having trouble taking the answer and applying it to the problem that inspired the question.

In a nutshell, p_stack1 and c_stack1 contain arrays that are derived from an image processing algorithm I am working on. p_stack1 contains probability data and c_stack1 contain integer classifications. I need to find the classification that has the highest probability for every pixel in the image with dimensions 768 x 1024. From the docs for integer array indexing , it provides a way to subset data from higher dimensional arrays using their integer indexes.

The solution of my original question works for a simplified example of nxnxn shaped arrays, but does not seem to work on lxmxn shaped arrays.

#dummy data
p_stack1 = np.reshape(np.random.uniform(0,1,2359296),(3,768,1024))
c_stack1 = np.reshape(np.random.randint(0,4,2359296),(3,768,1024))

#find where max value occurs on axis 0
ind_new=p_stack1.argmax(axis=0)

#Create assending indicies
nx, ny = 768,1024
xx = np.arange(ny)
aa= np.tile(xx,(ny,1))
bb = np.column_stack(tuple(aa))[:nx,:]
aa= np.tile(xx,(ny,1))[:nx,:]

#perform the integer array indexing
print(c_stack1[ind_new, aa,bb])

The last print statement returns the error:

IndexError: index 768 is out of bounds for axis 1 with size 768

I checked the shapes of aa and bb and both are (768, 1024)

What I am I missing?

Looks like you got your dimensions mixed up:

c_stack1.shape   # (3, 768, 1024)
aa.max()         # 1023
bb.max()         # 767

So, when you run

c_stack1[ind_new, aa, bb]

you will be trying to index axis=1 with higher values than available, hence the error

Either turn around aa and bb , or else c_stack1[ind_new, bb, aa] will also do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM