pythonnumpyscikit-learnscipy.ndimage

How to access range of indices of an array where specific conditions match


I have an array for a example:

import NumPy as np
a=np.array( [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
              [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0],
              [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0],
              [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

# it will give the transition of an array where the element value changes 1 to 0
x= np.gradient(a, axis=0)
y= np.gradient(a, axis=1)
trans_YX = np.array(list(zip(x.ravel(), y.ravel())), dtype=('f4,f4')).reshape(y.shape)
print(trans_YX)
# by np.argwhere() 0r np. nonzero() function, able to access the indices like row and column 
value

indices=np.argwhere(trans_YX)
print(indices)

the present output:

[[1 2]
 [1 3]
 [1 4]
 [2 1]
 [2 2]
 [2 3]
 [2 4]
 [2 5]
 [2 7]
 [2 8]
 [3 1]
 [3 2]
 [3 4]
 [3 5]
 [3 6]
 [3 7]
 [3 8]
 [3 9]
 [4 1]
 [4 2]
 [4 4]
 [4 5]
 [4 6]
 [4 7]
 [4 8]
 [4 9]
 [5 1]
 [5 2]
 [5 3]
 [5 4]
 [5 5]
 [5 7]
 [5 8]
 [6 2]
 [6 3]
 [6 4]]

expected output:

enter image description here

The current code finds the corresponding row and column values of element value transitions from 0 to 1 and 1 to zero.

The code is supposed to access the column and row values around squares (e.g. the indices of the red color outlined in the attached image), it's a big array and these types of varying square sizes are distributed throughout the array.

Is it possible to access these transitions using e.g. scikit-learn special functions?


Solution

  • Here is how you can grab the squares of 1s and their borders:

    In [16]: a
    Out[16]: 
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    
    In [17]: s2 = nd.generate_binary_structure(2,2)
    
    In [18]: lab, nlab = nd.label(a, s2)
    
    In [19]: lab
    Out[19]: 
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 2, 2, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 2, 2, 0, 0],
           [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
    
    In [20]: for i in range(1, nlab+1):
        ...:     region_i = lab==i
        ...:     expanded = nd.binary_dilation(region_i, iterations=1, structure=s2)
        ...:     border = np.logical_xor(region_i, expanded)
        ...: 
        ...:     print(border)
        ...:     print("")
        ...: 
    [[False False False False False False False False False False False]
     [False  True  True  True  True  True False False False False False]
     [False  True False False False  True False False False False False]
     [False  True False False False  True False False False False False]
     [False  True False False False  True False False False False False]
     [False  True False False False  True False False False False False]
     [False  True  True  True  True  True False False False False False]
     [False False False False False False False False False False False]]
    
    [[False False False False False False False False False False False]
     [False False False False False False False False False False False]
     [False False False False False False  True  True  True  True False]
     [False False False False False False  True False False  True False]
     [False False False False False False  True False False  True False]
     [False False False False False False  True  True  True  True False]
     [False False False False False False False False False False False]
     [False False False False False False False False False False False]]
    

    You can then use np.where to find the row,col indices of the border:

    In [21]: y,x = np.where(border)
    
    In [22]: y
    Out[22]: array([2, 2, 3, 3, 4, 4, 5, 5])
    
    In [23]: x
    Out[23]: array([7, 8, 6, 9, 6, 9, 7, 8])