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[]) ?
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?posbe[[2,4,0],[3,6,6]]instead?