I have a binary image as follows:
data = np.array([[0, 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 0 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 0 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 0 , 0],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0]])
For pixels having 1s values, I want to make buffer zone of two pixels with value 1s surrounded in every four directions. The expected result would be:
result=np.array([[1, 1 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 1]])
How can I do it?
If you only have ones and zeros on the input and output array, you can do it with a 2D convolution, which is simple and works.
from scipy.signal import convolve2d
data = np.array([[0, 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 0 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 0 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 0 , 0],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0],
[0, 0 , 0 , 0 , 0 , 0 , 0 , 0]])
# the kernel doesn't need to be ones, it just needs to be positive and
# non-zero.
kernel = np.ones((5, 5))
result = np.int64(convolve2d(data, kernel, mode='same') > 0)
Which gives you the output you want. You need to define what you want to happen at the edges - in this version, the output array is the same size as the input array.
It might be possible you can do something faster if you have a sparse array.
If you have other values than one and zero in your array, more thought would be needed.
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