I'm performing a image analysis and generated seeds in the form of a boolean array
:
import numpy as np
# Example output array
a = np.array([[False, False, False], [False, True, False], [False, False, False]])
>>> a
array([[False, False, False],
[False, True, False],
[False, False, False]])
As I want to do a subsequent analysis on the area surrounding the True
value, I want to expand it (by a certain number, say pixels). This would result in the following:
>>> a
array([[False, True, False],
[True, True, True],
[False, True, False]])
Is there any function
or simple way of solving my 'radial expansion' problem?
Thanks in advance, BBQuercus
Just to be clear: there's no "good" way to extend a NumPy array, as NumPy arrays are not expandable. Once the array is defined, the space it occupies in memory, a combination of the number of its elements and the size of each element, is fixed and cannot be changed.
With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.
size() function count the number of elements along a given axis.
Creating arrays with more than one dimension In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.
Solution with scipy.signal.convolve2d:
import numpy as np
from scipy.signal import convolve2d
# Example input
# [[False False False False False]
# [False False True True False]
# [False False False False False]
# [False False False False False]
# [False False False False True]]
in_array = np.zeros((5, 5), dtype=bool)
in_array[1,2] = True
in_array[1,3] = True
in_array[4,4] = True
# Kernel: here you should define how much the True "dilates"
kernel = np.asarray([[False, True, False],
[True, True, True],
[False, True, False]])
# Convolution happens here
# Convolution is not possible for bool values though, so we convert to int and
# back. That works because bool(N) == True if N != 0.
result = convolve2d(in_array.astype(int), kernel.astype(int), mode='same').astype(bool)
print(result)
# Result:
# [[False False True True False]
# [False True True True True]
# [False False True True False]
# [False False False False True]
# [False False False True True]]
Why not simply use scipy.ndimage.binary_dilation
?
import numpy as np
from scipy import ndimage
a = np.array([[False, False, False], [False, True, False], [False, False, False]])
b = ndimage.binary_dilation(a, [[False, True, False], [True, True, True], [False, True, False]])
Results:
>>> a
array([[False, False, False],
[False, True, False],
[False, False, False]])
>>> b
array([[False, True, False],
[ True, True, True],
[False, True, False]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With