2

I have a problem with a combination of index arrays and slices. I have an image (A) and a vector with positions/indexes (pos). Now, I want to select slices (here 3x) from A at different positions. Instead of looping over the positions array, I tried to use the indexer functions (o1), but I does not work. Finally, all slices should be in one array (o2). Can you help me with this problem ?

A = np.array([[0,0,0,0,0,0,3,3],
             [0,0,0,0,0,0,3,3],
             [0,0,0,1,1,0,0,0],
             [0,0,0,1,1,0,0,0],
             [0,0,0,0,0,0,2,2],
             [0,0,0,0,0,0,2,2],
             [0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0]])

# positions to select
# EDIT:
# pos = np.array([[2,6,7],
#                 [3,4,0]])
pos = np.array([[2,4,0],
                [3,6,6]])

# array with all selections
o1 = np.zeros((3,2,2)).astype(np.int)
# EDIT:
#o1 = A[pos[0]:pos[0]+1,pos[1]:pos[1]+1] ## this gives just one of the values in one area
o1 = A[pos[0]:pos[0] + 2,pos[1]:pos[1] + 2]
print(o1.shape)
print(o1)

# model result
o2 = np.array([[[1,1],[1,1]],[[2,2],[2,2]],[[3,3],[3,3]]])
print(o2.shape)
print(o2)

Another example: With the following line I get the area with the ones. Starting from the position [2,3] choose the next two rows and cols:

print( A[pos[0,0] : pos[0,0] + 2 , pos[1,0] : pos[1,0] + 2]  )

Isn't it possible to extend this to several position pairs (over the whole array pos[]) ?

6
  • If np.array([[[1,1],[1,1]],[[2,2],[2,2]],[[3,3],[3,3]]]) is the expected o/p, could you explain how you got that using the inputs? Commented Jun 8, 2017 at 13:36
  • Your array is in 2 dimensions, why do you use np.zeros with 3 arguments ? Commented Jun 8, 2017 at 13:40
  • o2 is the expected result. As is mentioned, I have several positions given in pos[] and then I want to use them as index positions in A[] and extract 2x2 parts. In this case [[1,1],[1,1]] , [[2,2],[2,2]] and [[3,3],[3,3]]. All of them should be stored in a singe array (o2). Commented Jun 8, 2017 at 13:52
  • Shouldn't pos be [[2,4,0],[3,6,6]] instead? Commented Jun 8, 2017 at 13:57
  • Yes, you are right. Commented Jun 8, 2017 at 14:54

2 Answers 2

1

You could use scikit's sliding window utility to extract such windows given the start indices from pos in a pretty straight forward way -

from skimage.util.shape import view_as_windows

out = view_as_windows(A, (2,2))[pos[0], pos[1]]

Sample run -

In [225]: A
Out[225]: 
array([[0, 0, 0, 0, 0, 0, 3, 3],
       [0, 0, 0, 0, 0, 0, 3, 3],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 1, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 2, 2],
       [0, 0, 0, 0, 0, 0, 2, 2],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]])

In [226]: pos = np.array([[2,4,0],[3,6,6]])

In [227]: from skimage.util.shape import view_as_windows

In [228]: view_as_windows(A, (2,2))[pos[0], pos[1]]
Out[228]: 
array([[[1, 1],
        [1, 1]],

       [[2, 2],
        [2, 2]],

       [[3, 3],
        [3, 3]]])
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you want this:

A[pos.tolist()]

That is, fancy indexing using pairs of points.

2 Comments

Sry, I do not want just the singe values. I am looking for the whole areas. I changed the line in my first post.
@Jackster182: Your question is totally unclear. Your use of example input data which is overly large and full of zeros does not help at all. I have no idea what you're asking now.

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.