Given is a inp_arr
of shape (m, n)
with 0
s and 255
s as values. I want to pad the 0
s with k zeros in a linear_ramp mode.
0
s are not necessarily next to each other, so there could be two groups of 0
s. (m, n)
I already tried this: np.where(inp_arr== 0, np.pad(inp_arr, 2, pad_with), inp_arr)
, giving me a ValueError
that shapes cannot be broadcast together. pad_width
is taken from here .
Example
import numpy as np
inp_arr = np.array([[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 0, 0, 255, 255, 255, 255, 255],
[255, 0, 0, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255],
[255, 255, 255, 255, 0, 0, 255, 255],
[255, 255, 255, 255, 0, 0, 255, 255],
[255, 255, 255, 255, 255, 255, 255, 255]])
out_arr = np.array([[255, 255, 255, 255, 255, 255, 255, 255],
[0, 0, 0, 0, 255, 255, 255, 255],
[0, 0, 0, 0, 255, 255, 255, 255],
[0, 0, 0, 0, 255, 255, 255, 255],
[0, 0, 0, 0, 255, 255, 255, 255],
[255, 255, 255, 0, 0, 0, 0, 255],
[255, 255, 255, 0, 0, 0, 0, 255],
[255, 255, 255, 0, 0, 0, 0, 255],
[255, 255, 255, 0, 0, 0, 0, 255]])
Thanks for your suggestions: :)
That looks like an erosion :
>>> from scipy.ndimage import grey_erosion
>>> grey_erosion(inp_arr, size=(3,3))
array([[255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255, 255, 255, 255],
[ 0, 0, 0, 0, 255, 255, 255, 255],
[255, 255, 255, 0, 0, 0, 0, 255],
[255, 255, 255, 0, 0, 0, 0, 255],
[255, 255, 255, 0, 0, 0, 0, 255],
[255, 255, 255, 0, 0, 0, 0, 255]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.