0

I have a numpy array as follows.

a = np.random.rand(5,6)
a
Out[52]: 
array([[0.08649968, 0.24360955, 0.27972609, 0.21566217, 0.00194021,
        0.69750779],
       [0.09327379, 0.7579194 , 0.34634515, 0.78285156, 0.50981823,
        0.17256468],
       [0.7386456 , 0.78608358, 0.80615647, 0.72471626, 0.14825363,
        0.62044455],
       [0.32171325, 0.10889609, 0.56453828, 0.41675939, 0.09400235,
        0.32373844],
       [0.52850344, 0.0783796 , 0.74144658, 0.2363739 , 0.24535204,
        0.9930051 ]])

Then I used the following function to obtain non overlapped patches from the original array a. I used the code from the previously asked question.

def select_random_windows(arr, number_of_windows, window_size):
    # Get sliding windows
    w = view_as_windows(arr,window_size, 3)

    # Store shape info
    m,n =  w.shape[:2]

    # Get random row, col indices for indexing into windows array
    lidx = np.random.choice(m*n,number_of_windows,replace=False)
    r,c = np.unravel_index(lidx,(m,n))
    # If duplicate windows are allowed, use replace=True or np.random.randint

    # Finally index into windows and return output
    return w[r,c]

Then I called select_random_windows to obtain the following two non overlapped patches, each of size 3x3.

select_random_windows(a, number_of_windows=2, window_size=(3,3))
Out[54]: 
array([[[0.08649968, 0.24360955, 0.27972609],
        [0.09327379, 0.7579194 , 0.34634515],
        [0.7386456 , 0.78608358, 0.80615647]],

       [[0.21566217, 0.00194021, 0.69750779],
        [0.78285156, 0.50981823, 0.17256468],
        [0.72471626, 0.14825363, 0.62044455]]])

Now, how can I get the index of each of the two patches with respect to the main array a. For instance, the first patch should have index of (1x1) and second patch should have index of (1x4). Is there any way I can extract these center indices of patches with respect to original array a.

Stupid420
  • 1,347
  • 3
  • 19
  • 44

1 Answers1

-1

You can just use np.where(X == [value1, value2]) to get the index of values

w_sz
  • 332
  • 1
  • 8