for a example: I have array a
import numpy as np
import scipy.ndimage as nd
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, 0, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]])
my current code
def border_selection(inp_array):
s2 = nd.generate_binary_structure(rank=2,connectivity=2)
lab, nlab = nd.label(input=inp_array,structure= s2)
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)
l=border_selection(a)
Current output:
[[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 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 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]]
Required output: 1)If i return the variable border in the above function it onlx gives the value of first array, so its returns both the arrays with print function. 2)How to combine both the array like mentioned below expected ouput
[[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 True True True True ]
[False True False False False True False True False False True]
[False True False False False True False True False False True]
[False True True True True True 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]]
CodePudding user response:
You could use an array to collect the True values:
def border_selection(inp_array):
s2 = nd.generate_binary_structure(rank=2,connectivity=2)
lab, nlab = nd.label(input=inp_array,structure= s2)
# set up output array initialized with False
out = np.zeros_like(inp_array, dtype=bool)
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)
out |= border # append new True values to the output
return out
print(border_selection(a))
output:
[[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 True True True True]
[False True False False False True False True False False True]
[False True False False False True False True False False True]
[False True True True True True 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]]