pythonnumpyindexingimage-registration

Indexing numpy indices like array with list of 2D points


I am using 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 ids1 is filled with all the horizontal coordinates ids has a shape of (2,20,20)

I have a boolean mask of shape (20,20)

I need to have a list of ids that correspond to the ones marked as true in the mask.

I am trying to do this by mid=ids[:,mask].T which gives me a list of this sort

[2,17] [4,6] [1,19] [18,4]

and so on. They are saved in an array called mid

Then, I need all those coordinates in mid to find the values in another array. Meaning I need

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.

Reference image Floating image

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.

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] .

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.

So there you go, that is the explanation if you care about it. Thank you for reading and answering.


Solution

  • A few tips: You can get mid directly from mask using 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].