[英]Indexing numpy indices like array with list of 2D points
I am using python 2.7 我正在使用python 2.7
I have an array of indices created by 我有一个由创建的索引数组
ids=np.indices((20,20))
ids[0] is filled with all the vertical coordinates and ids 1 is filled with all the horizontal coordinates ids has a shape of (2,20,20) ids [0]填充所有垂直坐标,ids 1填充所有水平坐标,ids的形状为(2,20,20)
I have a boolean mask of shape (20,20) 我有一个布尔形状的面具(20,20)
I need to have a list of ids that correspond to the ones marked as true in the mask. 我需要具有与掩码中标记为true的ID对应的ID列表。
I am trying to do this by mid=ids[:,mask].T which gives me a list of this sort 我正在尝试通过mid = ids [:,mask] .T做到这一点,这给了我这样的列表
[2,17] [4,6] [1,19] [18,4] [2,17] [4,6] [1,19] [18,4]
and so on. 等等。 They are saved in an array called mid
它们保存在名为mid的数组中
Then, I need all those coordinates in mid to find the values in another array. 然后,我需要所有中间的坐标才能在另一个数组中找到值。 Meaning I need
意思是我需要
anotherarray([2,17]) anotherarray([2,17])
I have not managed to take the list of mid to use them in a fancy indexing way, can someone help me? 我还没有设法以中意的索引方式使用它们,有人可以帮助我吗?
I have 我有
anotherarray[mid[0],mid[1]]
and it doesnt work. 它不起作用。 I also have
我也有
anotherarray[tuple(mid)]
and it doesn't work 而且不起作用
Edit (read only if you care about context) : I wanted to add context to show why I think I need the extra indices. 编辑(仅在您关心上下文时才读) :我想添加上下文以显示为什么我认为我需要额外的索引。 Maybe I don't, that is what I want to fin out to make this efficient.
也许我不是,那就是我想要找出来提高效率的原因。
This is a registration problem, a ver simple one. 这是一个注册问题,非常简单。 I have two images.
我有两个图像。 A reference and a floating as seen below.
一个参考和一个浮动,如下所示。 Reference to the left, and floating to the right.
引用左侧,并浮动到右侧。
The reference image and the floating image are in different coordinate spaces. 参考图像和浮动图像位于不同的坐标空间中。 I have points marked as you can see in the images.
如您在图片中所见,我已标记了点。 I find an affine transformation between each other.
我发现彼此之间有仿射变换。
The region delimited by the line is my region of interest. 用线划界的区域是我感兴趣的区域。 I send the coordinates of that region in the floating space to the reference space.
我将浮动空间中该区域的坐标发送到参考空间。
There in the reference space, I find what pixels are found inside the region and they become the mask
array, containing the information of both in and outer pixels. 在参考空间中,我发现在该区域内找到了哪些像素,它们成为
mask
数组,其中包含内部和外部像素的信息。
But I only care about those inside, so I want only the indices of those pixels inside the mask in the reference space and save them using mid=ids[:,mask]
. 但是我只关心内部的像素,因此我只希望参考空间中遮罩内的那些像素的索引,并使用
mid=ids[:,mask]
保存它们。
Once I have those points, I transform them back to the floating space, and in those new indices I need to look for the intensity. 一旦有了这些点,就将它们转换回浮动空间,在这些新索引中,我需要寻找强度。 Those intensities are the ones who will be written back in the reference in their corresponding indices.
这些强度是将在其相应索引中写回参考的强度。 That is why I think I need to have the indices of those points in both reference and floating space, and the intensities of the image.
这就是为什么我认为我需要在参考空间和浮动空间中都具有这些点的索引,以及图像的强度。 That other image is the
anotherarray
from which I want only the transformed masked pixels. 另一个图像是
anotherarray
,我只希望anotherarray
获得经过变换的蒙版像素。
So there you go, that is the explanation if you care about it. 因此,如果您关心它,那就是解释。 Thank you for reading and answering.
感谢您的阅读和回答。
A few tips: You can get mid
directly from mask using np.argwhere(mask)
. 一些提示:你可以
mid
直接从面膜使用np.argwhere(mask)
。 Probably more convenient for your purpose is np.where
which you can use like mi, mj = np.where(mask)
and then anotherarray[mi, mj]
. 为了您的目的,可能更方便的是
np.where
,您可以像mi, mj = np.where(mask)
一样使用它mi, mj = np.where(mask)
然后再使用anotherarray[mi, mj]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.