Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand / dilate a numpy array?

Tags:

python

numpy

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

like image 614
BBQuercus Avatar asked Jun 24 '19 11:06

BBQuercus


People also ask

How do I enlarge a NumPy array?

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.

How do you enlarge an array in Python?

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.

What does size () do in NumPy?

size() function count the number of elements along a given axis.

Can NumPy arrays have more than 2 dimensions?

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.


2 Answers

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]]
like image 99
Leporello Avatar answered Oct 02 '22 17:10

Leporello


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]])
like image 33
LemmeTestThat Avatar answered Oct 02 '22 17:10

LemmeTestThat