0

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.

1
  • anotherarray[tuple(mid.T)]? Commented Jan 18, 2018 at 17:21

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

But if you're going to use the results for indexing, you might as well just do anotherarray[mask]. (I don't get why so many people think they need to convert their masks to numeric indices first.)
@user2357112 for example anontherarray may be larger than mask. Or if mask is sparse and you want to index multiple anotherarrays
Sure, there are use cases for it, but most of the time, it's unnecessary. A lot of people seem to think they need to do it when they don't.
@user2357112 In this particular case seeing that OP is masking ids not entirely without skill I assume they know how to use masks.
@ZloySmiertniy np.array([mi, mj]).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.