简体   繁体   English

具有lxmxn广播形状的高级整数索引

[英]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_stack1c_stack1包含从我正在处理的图像处理算法派生的数组。 p_stack1 contains probability data and c_stack1 contain integer classifications. p_stack1包含概率数据,而c_stack1包含整数分类。 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. 我需要找到尺寸为768 x 1024的图像中每个像素的概率最高的分类。从用于整数数组索引的文档中,它提供了一种使用其整数索引对较高维数组的数据进行子集化的方法。

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. 我最初提出的问题的解决方案适用于nxnxn形状的数组的简化示例,但似乎不适用于lxmxn形状的数组。

#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) 我检查了aabb的形状,它们都是(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 您将尝试使用比可用值高的值来索引axis=1 ,因此出现错误

Either turn around aa and bb , or else c_stack1[ind_new, bb, aa] will also do the trick 要么绕开aabb ,要么c_stack1[ind_new, bb, aa]也会解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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