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.

lesolorzanov
  • 3,536
  • 8
  • 35
  • 53

1 Answers1

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

Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • 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 Jan 18 '18 at 17:55
  • 1
    @user2357112 for example `anontherarray` may be larger than `mask`. Or if `mask` is sparse and you want to index multiple `anotherarray`s – Paul Panzer Jan 18 '18 at 17:57
  • 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 Jan 18 '18 at 18:05
  • @user2357112 In this particular case seeing that OP is masking `ids` not entirely without skill I assume they know how to use masks. – Paul Panzer Jan 18 '18 at 18:14
  • Thank you all. How can I make the ´mi´ and ´mj´ into one (2,n) shaped array? – lesolorzanov Jan 19 '18 at 11:27
  • 1
    @ZloySmiertniy `np.array([mi, mj])`. – Paul Panzer Jan 19 '18 at 11:30